Php 无法通过(联接模型)保存与hasMany关联的数据

Php 无法通过(联接模型)保存与hasMany关联的数据,php,cakephp,cakephp-2.3,Php,Cakephp,Cakephp 2.3,您好,我是cakephp新手,正在cakephp 2.3.4上做一个项目。 我要通过联想联想产品金属类有很多。但它似乎不起作用 型号代码 class Metal extends AppModel { public $hasMany = array( 'MetalProduct' ); } class Product extends AppModel { public $hasMany = array( 'MetalProduct' ); } App::uses('AppModel

您好,我是cakephp新手,正在cakephp 2.3.4上做一个项目。
我要通过联想联想产品金属类有很多。但它似乎不起作用

型号代码

class Metal extends AppModel {
public $hasMany = array(
  'MetalProduct'
 );
}

class Product extends AppModel {
public $hasMany = array(
  'MetalProduct'
 );
}

App::uses('AppModel', 'Model');

class MetalProduct extends AppModel {

public $belongsTo = array(
    'Metal' => array(
        'className'    => 'Metal',
        'foreignKey'   => 'metal_id'
    ),
   'Product' => array(
        'className'    => 'Product',
        'foreignKey'   => 'product_id'
    )
);}
我的数据库表名是金属产品金属(u)产品

我有多个选择选项可选择多个金属类型。
这就是我得到金属清单的方法

     $metals=$this->Metal->find('list');
    $this->set(compact('metals'));
listbox的FormHelper代码为

    <?php echo $this->Form->input('Metal',array('type' => 'select', 
                                                'multiple' => true)); ?>
我已经把我的头穿过了墙,但不知道为什么协会没有得到拯救。 他们在教程中说的方式似乎很简单,但为什么不起作用呢

我怀疑它不会保存,因为金属阵列是这样传递的

    'Metal' => array(
        (int) 0 => '5183cb65-bf90-459c-b22e-0b748620d4f0',
        (int) 1 => '5183ce25-c744-433e-b035-0b748620d4f0'
    ),
class Product extends AppModel {

 /**
 * hasAndBelongsToMany associations
 */
public $hasAndBelongsToMany = array(
    'Metal' => array(
        'className' => 'Metal',
        'joinTable' => 'metals_products',
        'foreignKey' => 'product_id',
        'associationForeignKey' => 'metal_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    ));
}

 class Metal extends AppModel {
 /** hasAndBelongsToMany associations */
public $hasAndBelongsToMany = array(
    'Product' => array(
        'className' => 'Product',
        'joinTable' => 'metals_products',
        'foreignKey' => 'metal_id',
        'associationForeignKey' => 'product_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    ));
  }

 /**
  * MetalsProduct Model
  * @property Product $Product
  * @property Metal $Metal
  */
  class MetalsProduct extends AppModel {
  /* belongsTo associations */
public $belongsTo = array(
    'Product' => array(
        'className' => 'Product',
        'foreignKey' => 'product_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Metal' => array(
        'className' => 'Metal',
        'foreignKey' => 'metal_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ));
   }
它应该提到“id”,而不是(int)0之类的东西

另外,我手动创建的metal_产品数据库表

  id(primary key)
  metal_id(foreign key to Metal.id)
  product_id(foreign key to Product.id)
我是否在命名约定或创建数据库的方式上犯了错误? 请给我正确的答案,因为我从其他人那里尝试的任何答案都不起作用

我正在通过

     $this->Product->saveAll($this->request->data, array('deep' => true))

在您的模型中,所有这些都在您的MetalProduct模型中吗?如果是这样,你需要搬家

class Metal extends AppModel {
public $hasMany = array(
  'MetalProduct'
 );
}
金属模型和

class Product extends AppModel {
public $hasMany = array(
  'MetalProduct'
 );
}
对产品模型的改进

同时将您的物品添加到每个型号

我看到你有一个连接表。联接表通常用于HABTM关联。这是一个很好的例子。您需要使用其他可用选项来定义每个模型中的外键关系,如上所述

请参见Cake文档中的示例

此外,如果您不确定如何正确设置模型,您可以始终使用Cakephp的烘焙功能生成模型和代码

book.cakephp.org/2.0/en/console-and-shells/code-generation-with-bake.html

如果你更多的是一个视觉学习者,这个视频图坦卡门应该帮助你通过基础知识


我使用Bake生成的关系如下所示

    'Metal' => array(
        (int) 0 => '5183cb65-bf90-459c-b22e-0b748620d4f0',
        (int) 1 => '5183ce25-c744-433e-b035-0b748620d4f0'
    ),
class Product extends AppModel {

 /**
 * hasAndBelongsToMany associations
 */
public $hasAndBelongsToMany = array(
    'Metal' => array(
        'className' => 'Metal',
        'joinTable' => 'metals_products',
        'foreignKey' => 'product_id',
        'associationForeignKey' => 'metal_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    ));
}

 class Metal extends AppModel {
 /** hasAndBelongsToMany associations */
public $hasAndBelongsToMany = array(
    'Product' => array(
        'className' => 'Product',
        'joinTable' => 'metals_products',
        'foreignKey' => 'metal_id',
        'associationForeignKey' => 'product_id',
        'unique' => 'keepExisting',
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'deleteQuery' => '',
        'insertQuery' => ''
    ));
  }

 /**
  * MetalsProduct Model
  * @property Product $Product
  * @property Metal $Metal
  */
  class MetalsProduct extends AppModel {
  /* belongsTo associations */
public $belongsTo = array(
    'Product' => array(
        'className' => 'Product',
        'foreignKey' => 'product_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ),
    'Metal' => array(
        'className' => 'Metal',
        'foreignKey' => 'metal_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    ));
   }
这对我来说完美无瑕。
希望它对所有人都有效

你是说2.3.4?CakePHP 2.4甚至不是beta/RC Yet对不起,我糟糕的2.3.4是我会纠正的你为什么使用hasMany belongs来代替HABTM进行hasMany关联有什么特别的原因吗?据我所知,hasMany关系需要在$this->request->data中准备一个数组,你能调试($this->request->data)吗然后检查?你能发布html代码吗?
$this->Form->input('Metal',array('type'=>'select','multiple'=>true))正在生产?我的模型已完全按照您的建议进行设置,而且我希望hasMany通过关联开始工作。根据它,我们只需要创建第三个模型MetalProductUct,正如示例中所解释的,即必须创建CourseMembership模型才能通过工作获得多个。它看起来很简单,我对我的模型也做了同样的操作,并相应地创建了数据库。但由于某些原因,它不起作用。我已经实现了你提到的链接,但似乎没有任何效果。plz需要帮助,终于修好了。谢谢你,约翰逊。我首先建立数据库关系,然后使用bake烘焙模型。它定义了模型中的所有关系。虽然这和我在烘焙模型后做的一样,但是烘焙使它工作起来了。我所做的唯一改变就是产品和类别的关系。类别模型有一个树形结构,所以我必须自己定义类别之间的hasmany关系,以使generateTreeList工作。我建议所有cakephp初学者使用bake生成模型,然后继续开发。谢谢