2014-11-04 49 views
0

我是Cake php的新手。 我有使用問題烘烤CakePHP烘烤所有未知問題

我設置了用戶表遷移

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

和遷移的數據庫文件,然後我試圖做的命令「蛋糕烘烤所有」 的問題是,用戶用belongsTo做了一個參考,並且hasMany 是使用烤的默認值?

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

} 

有什麼我在這裏失蹤? 我應該離開它並手動修改它,或者有我錯過的配置。

回答

1

有三個簡單的解決辦法,作爲最後的答案陳述蛋糕烤全是下面的默認蛋糕公約和事實創建關聯,你在表中有user_id的

  1. 要麼重新命名數據庫字段從user_id改爲id並再次運行該shell。

  2. 運行cake bake並手動選擇模型和外殼幫手

  3. 手動刪除模型的關聯,並在控制器和視圖中的引用手動配置數據庫的關聯。
1

這是正確的。

當您使用cake bake all時,它會按照慣例自動添加對錶的引用。當你的表格提到自己時,他確定了'hasMany'和'belongsTo'關係。您應該刪除不存在的鏈接或更改默認生成的鏈接。參考:baking custom projects