找不到CakePHP 3.0列:1054未知列

找不到CakePHP 3.0列:1054未知列,cakephp,has-and-belongs-to-many,cakephp-3.0,Cakephp,Has And Belongs To Many,Cakephp 3.0,我有一个使用CakePHP3.0和MySQL的简单博客。 当我尝试在多对多表上执行find方法时: public function category($cat_id = null) { $this->paginate = ['contain' => ['Users', 'Comments', 'Categories'], 'conditions' => [ 'Categories.id' => $cat_id]]; $this-&

我有一个使用CakePHP3.0和MySQL的简单博客。 当我尝试在多对多表上执行find方法时:

public function category($cat_id = null) {     
        $this->paginate = ['contain' => ['Users', 'Comments', 'Categories'], 'conditions' => [ 'Categories.id' => $cat_id]];
        $this->set('posts', $this->paginate($this->Posts));
        $this->render('main');
    }
我有个错误:

Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Categories.id' in 'where clause' 
其他查找调用工作正常,因此DB连接正常。 用于创建数据库的My.sql脚本:

CREATE TABLE users (
    id          INT             AUTO_INCREMENT PRIMARY KEY,
    username    VARCHAR(255)    NOT NULL,
    password    VARCHAR(255)    NOT NULL,
    email       VARCHAR(255)    NOT NULL,
    created     DATETIME
);  

CREATE TABLE posts (
    id          INT             AUTO_INCREMENT PRIMARY KEY,
    title       VARCHAR(50)     NOT NULL,
    content     TEXT            NOT NULL,
    created     DATETIME,
    updated     DATETIME,
    user_id     INT             NOT NULL,
    FOREIGN KEY user_key (user_id) REFERENCES users(id)
);

CREATE TABLE comments (
    id          INT             AUTO_INCREMENT PRIMARY KEY,
    author      VARCHAR(50)     NOT NULL,
    email       VARCHAR(255)    NOT NULL,
    contenet    TEXT            NOT NULL,
    post_id     INT             NOT NULL,
    FOREIGN KEY post_key (post_id) REFERENCES posts(id)
);

CREATE TABLE categories (
    id          INT             AUTO_INCREMENT PRIMARY KEY,
    name        VARCHAR(50)     NOT NULL,
    UNIQUE KEY (name)
);

CREATE TABLE categories_posts (
    category_id INT             NOT NULL,
    post_id     INT             NOT NULL,
    PRIMARY KEY (category_id, post_id),
    INDEX post_idx (post_id, category_id),
    FOREIGN KEY category_key (category_id) REFERENCES categories(id),
    FOREIGN KEY post_key (post_id) REFERENCES posts(id)
);  
所有*表类均由蛋糕烘焙工具生成。例如PostsTable:

<?php
namespace App\Model\Table;

use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;

/**
 * Posts Model
 */
class PostsTable extends Table
{

    /**
     * Initialize method
     *
     * @param array $config The configuration for the Table.
     * @return void
     */
    public function initialize(array $config)
    {
        $this->table('posts');
        $this->displayField('title');
        $this->primaryKey('id');
        $this->addBehavior('Timestamp');
        $this->belongsTo('Users', [
            'foreignKey' => 'user_id'
        ]);
        $this->hasMany('Comments', [
            'foreignKey' => 'post_id'
        ]);
        $this->belongsToMany('Categories', [
            'foreignKey' => 'post_id',
            'targetForeignKey' => 'category_id',
            'joinTable' => 'categories_posts'
        ]);
    }

    /**
     * Default validation rules.
     *
     * @param \Cake\Validation\Validator $validator Validator instance.
     * @return \Cake\Validation\Validator
     */
    public function validationDefault(Validator $validator)
    {
        $validator
            ->add('id', 'valid', ['rule' => 'numeric'])
            ->allowEmpty('id', 'create')
            ->requirePresence('title', 'create')
            ->notEmpty('title')
            ->requirePresence('content', 'create')
            ->notEmpty('content')
            ->add('user_id', 'valid', ['rule' => 'numeric'])
            ->requirePresence('user_id', 'create')
            ->notEmpty('user_id');

        return $validator;
    }

    /**
     * Returns a rules checker object that will be used for validating
     * application integrity.
     *
     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
     * @return \Cake\ORM\RulesChecker
     */
    public function buildRules(RulesChecker $rules)
    {
        $rules->add($rules->existsIn(['user_id'], 'Users'));
        return $rules;
    }
}

您从何处调用类别操作?php?是的,确切地说:D我稍后会检查您的链接,但它可能会有所帮助