Yii Framework Forum: Join ? - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Join ?

#1 User is offline   n-r 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 153
  • Joined: 19-April 13

Posted 01 May 2013 - 06:04 AM

hi
i have 2 table that relation together: tl_user(sid is Primary key) $ tl_tel(id is Primary key , sid is Foreign key)
i want to Join 2table, but i can't.
thank you
0

#2 User is offline   Aashis 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 18-March 12

Posted 01 May 2013 - 06:56 AM

Inside the model find this function and write down their relationships:
inside both the models
let the below be inside student model:
public function relations()
	{
		return array(
                    
                   'studentTel'=>array(self::HAS_MANY, 'Tel','sid'),
                   //'name of relationship'=>(self::type of relationship,'the table name that u r trying to join with','foreign key attribute') 
		
                   );
		
		
	}


And
let the below be inside The foreign key table model:
public function relations()
	{
		
		return array(
                    
                   'studentTel'=>array(self::BELONGS_TO, 'Student','sid'),
//'name of relationship'=>(self::type of relationship,'the table name that u r trying to join with','foreign key attribute')
                );
		
	}


This is one way of defining relationship...
0

#3 User is offline   n-r 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 153
  • Joined: 19-April 13

Posted 01 May 2013 - 11:06 PM

View PostAashis, on 01 May 2013 - 06:56 AM, said:

Inside the model find this function and write down their relationships:
inside both the models
let the below be inside student model:
public function relations()
	{
		return array(
                    
                   'studentTel'=>array(self::HAS_MANY, 'Tel','sid'),
                   //'name of relationship'=>(self::type of relationship,'the table name that u r trying to join with','foreign key attribute') 
		
                   );
		
		
	}


And
let the below be inside The foreign key table model:
public function relations()
	{
		
		return array(
                    
                   'studentTel'=>array(self::BELONGS_TO, 'Student','sid'),
//'name of relationship'=>(self::type of relationship,'the table name that u r trying to join with','foreign key attribute')
                );
		
	}


This is one way of defining relationship...




thank you , but want that join in Query! After you have defined relationships
0

#4 User is offline   Aashis 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 19
  • Joined: 18-March 12

Posted 02 May 2013 - 07:59 PM

View Postn-r, on 01 May 2013 - 11:06 PM, said:

thank you , but want that join in Query! After you have defined relationships


suppose u have two tables: Student and Tel.
$model=Student::model()->findAll(array(
                'condition' => "stuid=1",
                'with'=>array('studentTel'), // the name of relationship that u write in model.
                'select'=> array("*"),
                ));



if i am making relationship between two tables, then i usually use same relationship name....
By default "t." points to the first table and if u want to use the second table also then write "t2."
Ex: 'condition' => "t.stuid=1 AND t2.name='ABC'",


suppose u want to display student name and Tel name in ur view. for displaying student u will use
foreach($model As $mod)
{
  echo $mod['stuname'];// since this field is directly coming from student table, no need to use relationship here
  echo $mod->studentTel['telname']; // since this field is in another table, so u use relationship to display it.
}

0

#5 User is offline   n-r 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 153
  • Joined: 19-April 13

Posted 03 May 2013 - 11:07 PM

thank you very very much.
You're Indian, you're very kind. Mercy
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users