Has_Many Relation

Good day guys I have here 3 models/table

which has a relation like this




Consultation          Emap                 

------------          ----------           Sickness

cons_id       <----   fk_cons              ---------

fk_sickness           fk_sick      ---->   sck_id

                                           sck_word

                                           sck_desc


Consultation:

public function relations()

{

	return array(

		'emaps' => array(self::HAS_MANY, 'Emap', 'fk_cons'),

	);

}


Emap:

public function relations()

{

	return array(

		'fkSick' => array(self::BELONGS_TO, 'Sickness', 'fk_sick'),

		'fkCons' => array(self::BELONGS_TO, 'Consultation', 'fk_cons'),

	);

}


Sickness:

public function relations()

{

	return array(

		'emaps' => array(self::HAS_MANY, 'Emap', 'fk_sick'),

	);

}



what i wanted to do is like this

$criteria = new CDbCriteria;

$criteria->condition = "LIKE %".$keyword."%";

$consultation = Consultation::model()->findAll($criteria);

foreach($consultation->fk_cons as $cons)

{

 echo &#036;cons-&gt;sck_word;

}

thank you very much who ever can solve my problems. I need this to understand as soon as possible :(

Hi

  1. FinadAll returns an Array of Consultation(s)

  2. fk_cons exists in Emap not Consultation

So, for one Consultation


$consultation = Consultation::model()->find($criteria);


foreach($consultation->emaps as $emap)

{

if ($emap->fkSick)

  echo ($emap->fkSick->sck_word);

}

  1. don’t use a variable directly in sql ($criteria->condition = “LIKE %”.$keyword."%"; )

;)

If you have a composite primary key in your emap table, one suggestion is to take advantage of YII’s MANY_MANY relationship. Your Consulation relations array could have an entry that looks like this




'sickEntries' => array(self::MANY_MANY, 'Sickness', 'emap(fk_cons, fk_sick)'), // Assuming emap is the name of your table



Then all you would need to do is




$consultation = Consultation::model()->find($criteria);


foreach ($consultation->sickEntries as $sickEntry)

{

	// $sickEntry should be an instance of the Sickness class

}



You condition is all wrong: What attribute are you comparing $keyword to?

Look in a model actionSearch() function. the group of compares() show how to do this. The 3rd param, I believe, is for partial matches. That means ‘LIKE % %’.

It also looks like you are really setting up a MANY_MANY relation between Consultation and Sickness THROUGH Emap. Just thinking.

tnx for reply sir, lets disregard that criteria I made. what i really wanted to do is there is one consultation process and i would like to get access or get those selected sickness word used in consultation using the relation of consultation->emap->sickness do you have any idea or sample codes for HAS_MANY relations

lets forget that keyword, well my real concern is i want to display the sck_word in my consultation cgridview and filter it. tnx please help me with this.

tnx for your time, well im not really good in relations can you give me example using my table to display the sck_word in consultation cgridview.

$consultation->emap->sickness is your answer

If you want directly from $consultation (for example $consultation->sickness) you could make a function that returns the desired result

in your model consultation add this


public function getSickness() {

if ($this->emap)

     return $this->emap->sickness;

else

     return null;

}



now you can call directly $consultation->getSickness() or $consultation->sickness;

Also you could use the key ‘through’ (I didn’t test it)


'emaps' => array(self::HAS_MANY, 'Emap', 'fk_sick'),

'sickness' => array(self::HAS_MANY, 'Sickness', array('fk_sickness' => 'sck_id'), 'through' => 'emaps'),

I think ‘sickness’ above should be a self::MANY_MANY relation. The rest of the options don’t look exactly right but I don’t have the info at my fingertips

tnx jkofsky you solved my problem :) :D

Glad to hear it. Thanks for the up vote.