无法在 cakephp 3 中关联产品和类别 table
cannot associate products and categories table in cakephp 3
我有一个table结构:
products:
- products_id
- product_name
- product_price
- product_description
- category_id
categories:
- category_id
- category_name
有两种不同的表格可以分别添加和编辑产品和类别。我想关联两个 table,这样我就不必根据编辑表单下拉列表中的 category_id 搜索 category_name。
我尝试关联它,但出现以下错误:
Table "Cake\ORM\Table" is not associated with "Categories"
class ProductsTable extends Table {
public function initialize(array $config){
$this->table('Products');
$this->primaryKey('product_id');
$this->belongsTo('Categories', ['foreignKey' => 'product_id']);
}
}
class CategoriesTable extends Table {
public function initialize(array $config){
$this->table('Categories');
$this->primaryKey('category_id');
$this->hasMany('Products', ['foreignKey' => 'category_id']);
}
}
查看:
public function index()
{
$this->set('categories', $this->Products->Categories->find('list', array('fields' => array('Categories.category_id'),'value'=>array('Categories.category_name'))));
}
请帮助解决我的问题。
改变
$this->primaryKey('product_id');
到
$this->primaryKey('products_id');
并添加
$this->displayField('category_name');
我有一个table结构:
products:
- products_id
- product_name
- product_price
- product_description
- category_id
categories:
- category_id
- category_name
有两种不同的表格可以分别添加和编辑产品和类别。我想关联两个 table,这样我就不必根据编辑表单下拉列表中的 category_id 搜索 category_name。 我尝试关联它,但出现以下错误:
Table "Cake\ORM\Table" is not associated with "Categories"
class ProductsTable extends Table {
public function initialize(array $config){
$this->table('Products');
$this->primaryKey('product_id');
$this->belongsTo('Categories', ['foreignKey' => 'product_id']);
}
}
class CategoriesTable extends Table {
public function initialize(array $config){
$this->table('Categories');
$this->primaryKey('category_id');
$this->hasMany('Products', ['foreignKey' => 'category_id']);
}
}
查看:
public function index()
{
$this->set('categories', $this->Products->Categories->find('list', array('fields' => array('Categories.category_id'),'value'=>array('Categories.category_name'))));
}
请帮助解决我的问题。
改变 $this->primaryKey('product_id'); 到 $this->primaryKey('products_id');
并添加 $this->displayField('category_name');