【发布时间】:2012-02-16 17:35:56
【问题描述】:
我正在尝试同时将数据保存到两个模型(一个模型和相关的 hasMany),并且在我的应用程序的许多地方都取得了一些成功,但是这种方法似乎不适用于表/模型带有下划线的名称,即
//View Code
echo $this->Form->create('Product');
echo $this->Form->input('name',array('between' => '<br />'));
echo $this->Form->input('Component.0.text',array('between' => '<br />'));
echo $this->Form->end(__('Submit'));
//Controller Code
if ($this->Product->saveAssociated($this->request->data)) {
$this->Session->setFlash(__('The product has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The product could not be saved. Please, try again.'));
}
以上工作正常,但是我有一个模型 ProductComponent (db table product_components)
//View Code
echo $this->Form->create('Product');
echo $this->Form->input('name',array('between' => '<br />'));
echo $this->Form->input('ProductComponent.0.text',array('between' => '<br />'));
echo $this->Form->end(__('Submit'));
//Controller Code
if ($this->Product->saveAssociated($this->request->data)) {
$this->Session->setFlash(__('The product has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The product could not be saved. Please, try again.'));
}
表单显示的是 Textarea 而不是标准输入,因此可以正确选择它,但是当运行 saveAssociated 时,我得到了响应:
找不到模型 ProductComponent 的数据库表 product__components。
为什么 cake 会寻找带有双下划线名称的表?
有什么想法吗?
Issem Danny 更新:
public $hasMany = array(
'ProductComponent' => array(
'className' => 'Product_Component',
'foreignKey' => 'product_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
【问题讨论】:
-
您可以发布您的模型以便我们查看您的关系设置吗?我的猜测是那里出了点问题。您是否在 ProductComponent 模型中设置了 $useTable 属性?
-
我认为你是对的,我刚刚发现我在产品的 hasMany 定义中输入了错误的类名,这可以解释为什么它在单独使用时会起作用。如果您发布了正确的答案,我会感谢您。
标签: php cakephp cakephp-2.0