dougai0138 2013-05-08 14:43
浏览 97
已采纳

无法通过hasMany保存关联数据(加入模型)

Hi, I am new to cakephp and doing a project on cakephp 2.3.4.
I have to associate product metal class through has many through association . But it doesn't seem to be working.

Model code

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'
    )
);}

My database table names are metal, products and metal_products

I have multiple select option for selecting more than one metal type.
This is how I get the the list of metals

     $metals=$this->Metal->find('list');
    $this->set(compact('metals'));

FormHelper code for listbox is

    <?php echo $this->Form->input('Metal',array('type' => 'select', 
                                                'multiple' => true)); ?>

The product is getting saved successfully but the associations are not.
The debug array give me this

    $message = array(
'Product' => array(
    'category_id' => '517a514b-0eb0-4ec9-b018-0b948620d4f0',
    'name' => 'mangalsutra Diamond',
    'slug' => 'mangalsutra_diamond',
    'description' => '1212',
    'Metal' => array(
        (int) 0 => '5183cb65-bf90-459c-b22e-0b748620d4f0',
        (int) 1 => '5183ce25-c744-433e-b035-0b748620d4f0'
    ),
    'image' => '121212',
    'price' => '12121',
    'weight' => '12',
    'active' => '1',
    'category' => 'Mangalsutra'
)
 )

I had put my head through walls but no clue why the associations are not getting saved. The way they say in tutorials it seems easy, but why its not working?

I have doubts that its not saving because the metal array is passed like this

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

It should mention 'id''rather than (int) 0 or something.

Also, my database table for metal_products which I have created manually has

  id(primary key)
  metal_id(foreign key to Metal.id)
  product_id(foreign key to Product.id)

Am I doing something wrong with naming conventions or the way database is created? Please give me correct ans cause anything I tried from others answer is not working

I am saving it via

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

2条回答 默认 最新

  • dsfgdsjfd78773 2013-05-15 14:32
    关注

    The Relation i generated using Bake looks something like this

    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' => ''
        ));
       }
    

    This worked for me flawlessly. Hope it works for all.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿