Pregunta

I'm new to Yii and used to Kohana. I have two models: 1. sampleRequests -> (this has many SampleShares) 2. sampleShares

In my case I have already instantiated an object (sampleRequest) and I am trying to get a particular sampleShares based on added criteria.

In Kohana I could do:

$myShare = $this->sampleShares->where('user_id','=',$my_user_id)->find(); 

It Yii I have tried something similar:

$myShare = $this->sampleShares->find(array(
'condition'=>'user_id:user_id',
'params'=>array(':user_id'=>$my_user_id))); 

The previous does not work for me however, the following will work:

$myShare = SampleShare::model()->find(array(
'condition'=>'sample_id=:id AND user_id=:user_id',
'params'=>array(':id'=>$this->id, ':user_id'=>Yii::app()->user->id)));

So my question is, can you grab related models with appended select criteria to the relationships established in Yii (particular with the instance or $this)? (Similar to my Koahana example?) If so how do you do that?

¿Fue útil?

Solución 3

Nevermind I figured it out. I just had to replace:

$this->sampleShares->find(

with

$this->with('sampleShares')->find(

Otros consejos

$myShare = SampleShare::model()->findAll(array('condition'=>'sample_id='.$id.' AND user_id='.$user_id));

and relation query example

$myShare = SampleShare::model()->with('sampleRequests')->findAll(array('condition'=>'sample_id='.$id.' AND user_id='.$user_id));

you can find the realtion name in your model file...

And then you can get the related table records using $myShare->sampleRequests

you can set relationship between the tables in your model file, like this:

public function relations()
{
    return array(
        'category' => array(self::BELONGS_TO, 'CategoryCategory', 'category_id'),
    );
}

how to use:

$model=Blog::model()->with('category')->findAll(); //object
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top