Skip to content

Instantly share code, notes, and snippets.

@mailopl
Created September 18, 2012 14:41
Show Gist options
  • Save mailopl/3743496 to your computer and use it in GitHub Desktop.
Save mailopl/3743496 to your computer and use it in GitHub Desktop.
In the framework jungle: CakePHP Basic Model
class User extends AppModel {
public $validate = array(
'email' => array(
'isUnique' => array(
'rule' => 'isUnique',
'message' => 'This email is already used.'
),
'email' => array(
'rule' => 'email',
'message' => 'Enter a valid email.'
)
),
'password' => array(
'rule' => array('minLength', '8'),
'message' => 'Minimum 8 characters long'
),
);
public $hasMany = array(
'Post' => array(
'className' => 'Post',
'foreignKey' => 'user_id',
)
);
}
class Post extends AppModel {
//TODO: check us:
public $validate = array(
'name' => array(
rule' => array('minLength', '3'),
),
'content' => array(
'rule' => array('minLength', '3'),
'message' => 'Minimum 3 characters long'
),
);
public $hasAndBelongsToMany = array(
'Tag' => array(
'className' => 'Tag',
'joinTable' => 'post_tags',
'foreignKey' => 'post_id',
'associationForeignKey' => 'tag_id',
'unique' => true,
)
);
//or simply
//public $hasAndBelongsToMany = array('Tag');
public $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
)
);
}
class Tag extends AppModel {
public $hasAndBelongsToMany = array(
'Post' => array(
'className' => 'Post',
'joinTable' => 'post_tags',
'foreignKey' => 'tag_id',
'associationForeignKey' => 'post_id',
'unique' => true,
)
);
//or simply
//public $hasAndBelongsToMany = array('Post');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment