CakePHP framework Model associated object usage analysis, cakephpmodel

Source: Internet
Author: User

CakePHP framework Model associated object usage analysis, cakephpmodel

This example describes the object associated with the Model of the CakePHP framework. We will share this with you for your reference. The details are as follows:

CakePHP provides ing between associated data tables. There are four types of associations:

hasOne,hasMany,belongTo,hasAndBelongsToMany.

CakePHP maps the data of a relational database to an object-based relational Model after the definition of the associations between models is set.

However, make sure that you follow the naming rules of CakePHP.

In naming rules, the three items to consider are foreign keys, model names, and table names.

Foreign key: modelName_id in the singular form
Table Name: model name in the plural form
Model name: the singular form of the camper method (see the file inflector. php ).

Definition and query of hasOne Association: Add an array to the model.

class User extends AppModel{  var $name = 'User';  var $hasOne = array(    'UserInfos' => array(      'className' => 'UserInfos',      'conditions' => '',      'order'=> '',      'dependent' => true,      'foreignKey' => 'user_id'    )  );}

The $ hasOne variable is an array. CakePHP uses this variable to construct associations between blogs and users.

ClassName: The Class Name of the associated object.
Conditions: Select conditions for correlated objects.
Order: The arrangement of correlated objects.
Dependent: This is a Boolean value. If it is true, the associated sub-objects will be deleted cascade when the parent object is deleted.
ForeignKey: Specifies the name of the foreign key field associated with the Model. It must be set only when the Cake naming conventions are not followed.

Definition and use of belonsto Association

class Blog extends AppModel{  var $name = 'Blog';  var $belongsTo = array(    'User' => array(      'className' => 'User',      'conditions' => '',      'order' => '',      'foreignKey' => 'user_id'    )  );}

ClassName: The Class Name of the associated object.
Conditions: SQL Condition Clause to limit associated objects.
Order: Sort clause of correlated objects.
ForeignKey: name of the foreign key field corresponding to the associated object.

Definition and query of hasMany Association

class User extends AppModel{  var $name = 'User';  var $hasMany = array(    'Blog' => array(      'className' => 'Blog',      'conditions' => 'Blog.status = 1',      'order' => 'Blog.created DESC',      'limit' => '5',      'foreignKey' => 'user_id',      'dependent' => true,      'exclusive' => false, 'finderQuery' => ''    )  );}

$ HasMany array is used to define the association between a User and Multiple blogs.

ClassName: name of the associated object class.
Conditions: conditions for associating objects.
Order: Join Object arrangement clause.

Limit: Use limit to limit the number of associated objects to be retrieved.

ForeignKey: the name of the foreign key field.
Dependent: whether to cascade deletion.
Exclusive: if TRUE, all associated objects will be deleted in an SQL statement, and the beforeDelete callback function of the model will not be executed.
FinderQuery: defines a complete SQL statement to retrieve associated objects. It can control association rules to the maximum extent.

You can also add the belongTo Association of the User object to the Blog.

Definitions and queries associated with hasAndBelongsToMany.

class Blog extends AppModel{  var $name = 'Blog';  var $hasAndBelongsToMany = array('Tag' =>    array('className'  => 'Tag',       'joinTable'  => 'blogs_tags',       'foreignKey'  => 'blog_id',       'associationForeignKey'=> 'tag_id',       'conditions'  => '',       'order'    => '',       'limit'    => '',       'uniq'     => true,       'finderQuery' => '',       'deleteQuery' => '',    )    );}

$ Hasandbelongstow.array is a variable that defines the association of HABTM.

ClassName: name of the associated object class.
JoinTable: if you do not follow the naming conventions of Cake to create an association table, you need to set this key to specify the association table.
ForeignKey: defines the foreign key field of this mode in the associated table.
AssociationForeignKey: name of the foreign key field pointing to the associated object in the joined table.
Conditions: conditions for associating objects.
Order: Sort clause of correlated objects.
Limit: limit on the number of associated objects.
Uniq: if it is set to true, duplicate associated objects will be filtered out.
FinderQuery: a complete association Object Search statement.
DeleteQuery: complete SQL statement for deleting associations.

Save the associated object:

When neither of the associated objects is persistent, you must first persist the primary object.

When saving the sub-object, you must keep the ID of the parent object in the sub-object.

Save the associated object hasAndBelongsToMany:

UsebindModel()AndunbindModel()Change associations in Real Time:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.