未找到CakePHP 3.0列:1054未知列

时间:2015-03-22 13:57:40

标签: cakephp has-and-belongs-to-many cakephp-3.0

我使用CakePHP 3.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' 

其他查找调用工作正常,因此数据库连接正常。 我的.sql脚本创建DB:

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

所有* Table类都是由cake bake工具生成的。例如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;
    }
}

0 个答案:

没有答案