【问题标题】:CakePHP 2.0 Adding Related Data fails with missing database tableCakePHP 2.0 添加相关数据失败,缺少数据库表
【发布时间】: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


【解决方案1】:

一些事情:

  • 如果您有这三个模型:Product、Component、ProductComponent,在我看来,您正在尝试设置 hasAndBelongsToMany 关系。 Check out the cakephp docs.

  • 使用组件作为模型名称或在模型名称中似乎让我感到困惑..

  • 如果您有一个表 product_components 的模型:您的模型应命名为“ProductComponent”,不带下划线(cake 会自动添加该下划线)。如果您不遵循 cake 的约定,您可以声明“useTable”属性。您的模型应如下所示:

代码:

// model

class ProductComponent extends AppModel {
    public $name = "ProductComponent";

    // Only needed when not following cake's conventions
    public $useTable = "product_components"; 

    // rest of model code  ...
}

// And the hasmany relation

public $hasMany = array(
    'ProductComponent' => array(
        'className' => 'ProductComponent',
        'foreignKey' => 'product_id'
     )
);

希望有帮助,祝你好运。

【讨论】:

  • 非常感谢 Danny... 第一个示例中的组件只是保存从另一个控制器复制代码的示例,因此不需要 HABTM。不过,请注意 hasMany 的问题,谢谢
【解决方案2】:

你试过用吗

$this->Product->saveAll($this->data))

试试这个链接SaveAll

【讨论】:

  • 嗨,Alex,是的,我同时使用了 saveAll 和 saveAssociated,问题似乎出在关系上,因为直接访问 ProductComponent 就可以了
猜你喜欢
  • 2010-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-09
  • 2019-03-26
  • 1970-01-01
  • 2013-07-14
  • 1970-01-01
相关资源
最近更新 更多