CakePHP 2.0 添加相关数据失败,缺少数据库表
我试图同时将数据保存到两个模型(一个模型和相关的 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 (数据库表 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 时,我得到响应:
Database未找到模型 ProductComponent 的表 Product__components。
为什么cake要寻找名称带有双下划线的表?
有什么想法吗?
伊塞姆·丹尼的更新:
public $hasMany = array(
'ProductComponent' => array(
'className' => 'Product_Component',
'foreignKey' => 'product_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
I'm trying to save data to two models (a model and related hasMany) simultaneously, and have had some success in a number of places in my app, however this approach doesn't seem to be working with tables/models which have underscored names, i.e.
//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.'));
}
The above works fine, however I have a model 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.'));
}
The form is picking this up correctly as it's showing a Textarea rather than a standard input, however when saveAssociated is run, I get the response:
Database table product__components for model ProductComponent was not found.
Why is cake looking for a table with a double underscored name?
Any ideas?
Update for Issem Danny:
public $hasMany = array(
'ProductComponent' => array(
'className' => 'Product_Component',
'foreignKey' => 'product_id',
'dependent' => false,
'conditions' => '',
'fields' => '',
'order' => '',
'limit' => '',
'offset' => '',
'exclusive' => '',
'finderQuery' => '',
'counterQuery' => ''
)
);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一些事情:
如果您有这三个模型:Product、Component、ProductComponent,在我看来您正在尝试设置 hasAndBelongsToMany 关系。 查看 cakephp 文档。
使用 Component 作为模型名称或在模型名称中使用似乎令我感到困惑。
如果您有模型对于表product_components:您的模型应命名为“ProductComponent”,不带下划线(cake 会自动添加该下划线)。如果您不遵循 cake 的约定,您可以声明“useTable”属性。您的模型应如下所示:
代码:
希望有帮助,祝您好运。
A few things:
If you have these three models: Product, Component, ProductComponent, it seems to me you're trying to setup hasAndBelongsToMany relations. Check out the cakephp docs.
Using Component as or in a model name seems confusing to me..
If you have a model for a table product_components: your model should be named "ProductComponent" without an underscore (cake will add that automatically). If you dont follow cake's conventions you can declare the "useTable" property. Your model should look like this:
Code:
Hope that helps, good luck.
您是否尝试过使用
尝试此链接SaveAll
Have you tried to use
Try this link SaveAll