Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/232.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CakePHP 2.0添加相关数据失败,缺少数据库表_Php_Cakephp_Cakephp 2.0 - Fatal编程技术网

CakePHP 2.0添加相关数据失败,缺少数据库表

CakePHP 2.0添加相关数据失败,缺少数据库表,php,cakephp,cakephp-2.0,Php,Cakephp,Cakephp 2.0,我试图同时将数据保存到两个模型(一个模型和相关的hasMany),并在我的应用程序中的许多地方取得了一些成功,但是这种方法似乎不适用于带有下划线名称的表/模型,即 //View Code echo $this->Form->create('Product'); echo $this->Form->input('name',array('between' => '<br />')); echo $this->Form->input('C

我试图同时将数据保存到两个模型(一个模型和相关的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.'));
}
你试过使用吗

$this->Product->saveAll($this->data))
请尝试以下链接:

  • 如果你有这三种模型:产品、组件、产品组件,在我看来,你似乎在试图建立HasandBelongTomany关系

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

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

代码:


希望这会有帮助,祝你好运。

你能发布你的模型,让我们看看你的关系设置吗?我猜那里出了点问题。您在ProductComponent模型中设置了$useTable属性了吗?我想您是对的,我刚刚发现我在产品的hasMany定义中输入了错误的类名,这可以解释为什么它在单独使用时工作。如果你给出了一个正确的答案,我会相信你。嗨,Alex,是的,我使用了saveAll和saveAssociated,问题似乎出在关系中,因为直接访问ProductComponent很好谢谢Danny。。。第一个示例中的组件只是一个用于保存从另一个控制器复制代码的示例,因此不需要HABTM。尽管有很多问题,谢谢你们
public $hasMany = array(
    'ProductComponent' => array(
        'className' => 'Product_Component',
        'foreignKey' => 'product_id',
        'dependent' => false,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'exclusive' => '',
        'finderQuery' => '',
        'counterQuery' => ''
    )
          );
$this->Product->saveAll($this->data))
// 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'
     )
);