关于php:CakePHP烘烤所有未知问题

CakePHP bake all unknown issue

我是Cake php的新手。
我在使用bake

时遇到问题

我使用用户表设置了迁移

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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"
问题是用户使用belongsTo和hasMany对其自身进行了引用
是使用bake的默认设置吗?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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' => ''
        )
    );

}

我在这里想念什么吗?
我应该保留并手动修改它,还是错过了一个配置。


有三种简单的解决方案,因为最后一个回答说"蛋糕烘焙"是遵循默认的蛋糕约定并通过表中有一个user_id的事实来创建关联

  • 将数据库字段从user_id重命名为id,然后再次运行Shell。

  • 运行cake bake并手动选择模型,并使用Shell helper

    手动配置数据库关联

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

  • 没错。

    使用cake bake all时,它会按照约定自动将引用添加到表中。当您的桌子指向自己时,他确定了" hasMany"和" belongsTo"的关系。您应该删除不存在的链接或默认情况下生成的更改。参考:烘焙自定义项目