Saturday, 2 June 2012

Relationships in cakephp

when you are using cakephp,the most worst thing you have to face is how to join two tables which has a relation with foreign key.


RelationshipAssociation TypeExample
one to onehasOneA user has one profile.
one to manyhasManyA user can have multiple recipes.
many to onebelongsToMany recipes belong to a user.
many to manyhasAndBelongsToManyRecipes have, and belong to many tags.
if you have two tables like:-
if one table is has fields like this:-

Table name- User
..........................................
user_id(primary key)
name
email
password
ip
date

Table name- property
.........................................
property_id
user_id
propertyname
propdescription
price
nobedrooms
parking
reference
date
published


so here in the property view page if u have to view the user details also ,then you will want to use these relatioship types.
here the code to use is:-

function getPropertydetails(){
$this->belongsTo=array('User'=>array('className'=>'User','foreignKey'=>'user_id'));
$rec = $this->find('all',array('conditions'=>array('Userproperty.property_id !=""','Userproperty.property_id =\''.$id.'\' ')));
return $rec;
}



No comments:

Post a Comment