CakePHP烘焙所有未知问题

时间:2014-11-04 16:20:06

标签: php mysql cakephp

我是Cake php的新手。 我在使用烘焙时遇到问题

我使用用户表

设置了迁移
public $migration = array(
        'up' => array(
            'create_table' => array(
                'users' => array(
                    'user_id' => array('type' => 'integer', 'null' => false, 'key' => 'primary'),
                    'username' => array('type' => 'string', 'null' => false, 'length' => 250),
                    'password' => array('type' => 'text', 'null' => false),
                    'created' => array('type' => 'string', 'null' => false, 'length' => 14),
                    'modified' => array('type' => 'string', 'null' => true, 'default' => null, 'length' => 14),
                    'indexes' => array(
                        'PRIMARY' => array('column' => 'user_id', 'unique' => 1)
                    )
                )
            )
        ),
        'down' => array(
            'drop_table' => array(
                'users'
            )
        )
    );

并在db上迁移此文件然后我尝试执行命令“cake bake all” 问题是User用belongsTo和hasMany引用了它自己 这是使用bake的默认值吗?

class User extends AppModel {

/**
 * Validation rules
 *
 * @var array
 */
    public $validate = array(
        'user_id' => array(
            'numeric' => array(
                'rule' => array('numeric'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'username' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
        'date_created' => array(
            'notEmpty' => array(
                'rule' => array('notEmpty'),
                //'message' => 'Your custom message here',
                //'allowEmpty' => false,
                //'required' => false,
                //'last' => false, // Stop validation after this rule
                //'on' => 'create', // Limit validation to 'create' or 'update' operations
            ),
        ),
    );

    //The Associations below have been created with all possible keys, those that are not needed can be removed

/**
 * belongsTo associations
 *
 * @var array
 */
    public $belongsTo = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'conditions' => '',
            'fields' => '',
            'order' => ''
        )
    );

/**
 * hasMany associations
 *
 * @var array
 */
    public $hasMany = array(
        'User' => array(
            'className' => 'User',
            'foreignKey' => 'user_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

}

这里有什么我想念的吗? 我应该离开并手动修改它,或者有一个我错过的配置。

2 个答案:

答案 0 :(得分:1)

没错。

当您使用cake bake all时,它会按照惯例自动添加对您的表的引用。当你的桌子引用自己时,他确定了'hasMany'和'belongsTo'的关系。您应该删除不存在的链接或默认生成的更改。参考:baking custom projects

答案 1 :(得分:1)

有三个简单的解决方案,因为最后的答案表明,蛋糕烘焙都遵循默认的蛋糕约定,并通过表中有user_id来创建关联

  1. 将数据库字段从user_id重命名为id并再次运行shell。

  2. 运行cake bake并手动选择模型并使用shell帮助程序手动配置数据库关联

  3. 手动删除模型中的关联以及控制器和视图中的引用。