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.
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;
}
Relationship | Association Type | Example |
---|---|---|
one to one | hasOne | A user has one profile. |
one to many | hasMany | A user can have multiple recipes. |
many to one | belongsTo | Many recipes belong to a user. |
many to many | hasAndBelongsToMany | Recipes have, and belong to many tags. |
if one table is has fields like this:-
Table name- User
..........................................
user_id(primary key)
name
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