Home » Interview Question » Cake Php » Understanding CakePHP Associations

Understanding CakePHP Associations

Understanding CakePHP Associations

Relationships are the established associate between two or more tables, when two column are usually same name in both tables. Relationship match the primary key from one table, which provides a unique identifier for each row is associated with an entry in the foreign key in the second table.

Primary key: primary key is a column that is uniquely identify each row in the table. Primary key can not’s be null and must be uniquely identify each row in the table, and there can be only one defined per table.
Forein key: A foreign key is a field in one table that matches a field in to another table. The data in the fields from both tables is exactly same and the table with the primary key record must have an existing records in the second table.

There are three types of relationships between tables :


One-to-One Relationships : A row in table A can have only single matching rows in table B, or vice versa.


One-to-Many Relationship : A row in table A can have many matching rows in table B, but a row in table B can have only one matching row in table A.


Many-to-Many Relationship : A row in table A can have many matching rows in table B, and also table B can have many matching row in table A.

We'll start by outlining our models:
Posts
1.  Belongs To  Author
2. hasMany      Comments
3. hasAndBelongsToMany      Tags


Author:      hasMany  Posts
Comments: belongsTo     Posts
Tags:  hasAndBelongsToMany  Posts

, , , , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">