嗨,我是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'
)
);}
我的数据库表名是metal, products和metal_products
我有多个选择选项来选择一个以上的金属类型。
这就是我得到金属列表的方法
$metals=$this->Metal->find('list');
$this->set(compact('metals'));
列表框的FormHelper代码为
<?php echo $this->Form->input('Metal',array('type' => 'select',
'multiple' => true)); ?>
产品已成功保存,但关联未保存。
调试数组给了我这个
$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'
)
)
我把我的头穿过墙,但不知道为什么协会没有得到保存。他们在教程中说的方式似乎很简单,但为什么它不起作用?
我怀疑它没有保存,因为金属数组是这样传递的
'Metal' => array(
(int) 0 => '5183cb65-bf90-459c-b22e-0b748620d4f0',
(int) 1 => '5183ce25-c744-433e-b035-0b748620d4f0'
),
应该提到'id "而不是(int) 0之类的。
另外,我手工创建的metal_products数据库表有
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'
);
}
到Metal模型和
class Product extends AppModel {
public $hasMany = array(
'MetalProduct'
);
}
到产品模型
还将belongsTo添加到每个Model
我看到你有一个连接表。连接表通常用于HABTM关联。这是一个HasMany。您需要使用其他可用的选项来定义每个模型中的外键关系,如上所述。
请参阅Cake文档中的示例。
http://book.cakephp.org/2.0/en/models/associations-linking-models-together.html同样,如果您不确定如何正确地设置模型,您可以始终使用Cakephp的Bake特性来为您生成模型和代码。
book.cakephp.org/2.0/en/console-and-shells/code-generation-with-bake.html
如果你是一个视觉学习者,这个视频应该可以帮助你通过基本的
http://www.youtube.com/watch?v=kJAMifqF5s8使用Bake生成的关系看起来像这样
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' => ''
));
}
这对我来说是完美的。希望它对所有人都有效。