无法在 cakephp 3 中关联产品和类别 table

cannot associate products and categories table in cakephp 3

我有一个table结构:

products:

  1. products_id
  2. product_name
  3. product_price
  4. product_description
  5. category_id

categories:

  1. category_id
  2. 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');