0
votes

I'm new to Cake php. I have a problem using bake

I set up a migration with user table

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

and migrate this file on the db then I tried to do the command "cake bake all" the problem is that User made a reference to itself with belongsTo and hasMany is that the default of using 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' => ''
        )
    );

}

is there something I missing here? Should I leave that and modify it manually or there's a config that I missed.

2

2 Answers

1
votes

There are three easy solutions, as the last answer stated cake bake all is following default cake conventions and creating associations by the fact that you have a user_id in the table

  1. Either rename the database field from user_id to id and run the shell again.

  2. Run cake bake and manually select model and configure the database associations manually with the shell helper

  3. Manually remove the associations in the model, and the references in the controller and views.
1
votes

It's correct.

When you use the cake bake all it automatically adds references to your table by convention. As your table refers to herself he identified the 'hasMany' and 'belongsTo' relationships. You should remove the link that doesn't exist or change that is generated by default. Reference: baking custom projects