How To Retrieve Data Who Have Foreign Key In Other Table


I have for this exemple 3 table :

personne : personne_id

TarifVente : fk_personne,fk_projet

projet : projet_id

I would like to retrieve projet for one personne.

How can I achieve that ?

Should I use relation ? if yes how? Or should I use Cd Criteria with join ?

Sorry for my approximate english.


Yep, use a MANY_MANY relation. There’s an example in the first answer here.

for example

class User extends CActiveRecord


public function relations()


    return array(

        'posts'=>array(self::HAS_MANY, 'Post', 'author_id'),




then you can use



foreach($posts as $post) {

//your code … post->id;



'ProjetsActif' => array(self::MANY_MANY, 'TTarifsvente', 't_tarifsvente(fk_personne,fk_projet)','condition'=> 'fk_statut ='.Yii::app()->params->statutsProjet,'order'=>'acronyme'),

$fk_personne = 107;

$Projet = TProjets::model()->with('ProjetsActif')->findAll('ProjetsActif.fk_personne=:personneId',array(':personneId'=>107));

is same as

SELECT DISTINCT p.acronyme, p.projet_id

FROM t_projets p

INNER JOIN t_tarifsvente tv ON p.projet_id = tv.fk_projet

WHERE tv.fk_personne =107

AND p.fk_statut =2

ORDER BY p.acronyme

in fact, that code doesn’t work :mellow:

yes, it is working now.

In fact, the correct solution is

'ProjetsActif' => array(self::MANY_MANY, 'TTarifsvente', 


                'condition'=> 'fk_statut ='.Yii::app()->params->statutsProjet,


instead of :

'ProjetsActif' => array(self::MANY_MANY, 'TTarifsvente', 


                'condition'=> 'fk_statut ='.Yii::app()->params->statutsProjet,


thank you for helping !