cakephp 如何将下拉列表中的数据保存到数据库中 table
cakephp how to save data from dropdownlist into database table
我创建了两个数据库表:
sellers_tbls
id
business_name
businesstype_tbls_business_type
mobile
businesstype_tbls
id
business_type
其中 businesstype_tbls
用于在注册时显示下拉类型。
提交表单后,register()
正在 businesstype_tbls.id
中保存 sellers_tbls.businesstype_tbls_business_type
。
如何在我的 sellers_tbls
中得到 business_type
而不是 id
?
我的代码如下:
register.ctp
<tr>
<td>Business type</td>
<td><?php echo $this->Form->select('businesstype_tbls_business_type',$drop);?></td>
</tr>
卖家控制器
public function register()
{
$this->loadModel("Seller");
if($this->Seller->save($this->request->data))
{
$this->Session->setFlash('Successully save your information!');
}
$this->loadModel("Businesstype");
$dt=$this->Businesstype->find('list',array('fields'=>array('id','business_type')));
$this->set('drop',$dt);
}
型号Businesstype.php
class Businesstype extends AppModel
{
public $name = 'businesstype_tbls';
public $hashMany = 'sellers_tbls';
}
型号Seller.php
class Seller extends AppModel
{
public $name = 'sellers_tbls';
public $belongsTo = 'businesstype_tbls';
}
在 sellers_tbls.businesstype_tbls_business_type
中保存 businesstype_tbls.id
是预期的行为。这就是建立模型之间关系的全部目的。
如果您希望以纯文本形式存储 business_type
:
- 删除关系
- 将
sellers_tbls.businesstype_tbls_business_type
的类型设置为可以容纳 businesstype_tbls.business_type
内容的类型
将$dt
设置为关联数组,每个键都设置为相应的值:
$dt=$this->Businesstype->find('list',array(
'fields'=>array('business_type','business_type')
));
话虽如此,如果您要构建新应用并从头开始设计数据模型,我建议您缩短 table 和字段名称。你的命名策略不仅复杂,而且不遵循 CakePHP 的约定,这会让你的日子越来越难过 运行.
为什么不只是以下内容?
卖家
id
business_name
businesstype
(或者businesstype_id
如果你想存储businesstypes.id
)
mobile
业务类型
id
type
我创建了两个数据库表:
sellers_tbls
id
business_name
businesstype_tbls_business_type
mobile
businesstype_tbls
id
business_type
其中 businesstype_tbls
用于在注册时显示下拉类型。
提交表单后,register()
正在 businesstype_tbls.id
中保存 sellers_tbls.businesstype_tbls_business_type
。
如何在我的 sellers_tbls
中得到 business_type
而不是 id
?
我的代码如下:
register.ctp
<tr>
<td>Business type</td>
<td><?php echo $this->Form->select('businesstype_tbls_business_type',$drop);?></td>
</tr>
卖家控制器
public function register()
{
$this->loadModel("Seller");
if($this->Seller->save($this->request->data))
{
$this->Session->setFlash('Successully save your information!');
}
$this->loadModel("Businesstype");
$dt=$this->Businesstype->find('list',array('fields'=>array('id','business_type')));
$this->set('drop',$dt);
}
型号Businesstype.php
class Businesstype extends AppModel
{
public $name = 'businesstype_tbls';
public $hashMany = 'sellers_tbls';
}
型号Seller.php
class Seller extends AppModel
{
public $name = 'sellers_tbls';
public $belongsTo = 'businesstype_tbls';
}
在 sellers_tbls.businesstype_tbls_business_type
中保存 businesstype_tbls.id
是预期的行为。这就是建立模型之间关系的全部目的。
如果您希望以纯文本形式存储 business_type
:
- 删除关系
- 将
sellers_tbls.businesstype_tbls_business_type
的类型设置为可以容纳businesstype_tbls.business_type
内容的类型
将
$dt
设置为关联数组,每个键都设置为相应的值:$dt=$this->Businesstype->find('list',array( 'fields'=>array('business_type','business_type') ));
话虽如此,如果您要构建新应用并从头开始设计数据模型,我建议您缩短 table 和字段名称。你的命名策略不仅复杂,而且不遵循 CakePHP 的约定,这会让你的日子越来越难过 运行.
为什么不只是以下内容?
卖家
id
business_name
businesstype
(或者businesstype_id
如果你想存储businesstypes.id
)mobile
业务类型
id
type