Hello guys, how do to include a table associated in a consult?
I am doing this way:
public function relations()
{
return array(
'newsPublications' => array(self::HAS_MANY, 'NewsPublication', 'news_id'),
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->with = array('newsPublications');
$criteria->compare('newsPublications.pageslot_id', 12);
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
));
}
but this not is working
Thank’s!
tri
(tri - Tommy Riboe)
June 20, 2011, 7:09pm
2
Try adding
$criteria->together = true;
If still no luck, tell os more about the error.
/Tommy
That’s right Tommy, thank you!
Tell me if I do this right:
public function search()
{
$criteria=new CDbCriteria;
$criteria->with = array('newsPublications');
$newsPublication = new NewsPublication();
$newsPublication = $_GET['NewsPublication'];
$pageslotId = $newsPublication['pageslot_id'];
$criteria->compare('newsPublications.pageslot_id', $pageslotId);
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
));
}
It works, but the combo loses value after applying the filter.
tri
(tri - Tommy Riboe)
June 20, 2011, 11:05pm
5
Tell me if I do this right:
public function search()
{
$criteria=new CDbCriteria;
$criteria->with = array('newsPublications');
$newsPublication = new NewsPublication();
$newsPublication = $_GET['NewsPublication'];
$pageslotId = $newsPublication['pageslot_id'];
$criteria->compare('newsPublications.pageslot_id', $pageslotId);
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
));
}
It works, but the combo loses value after applying the filter.
Assuming you feed a CGridView with data generated by this provider, as well as an instance of "this" model as filter.
I haven’t yet worked with these filters myself but there’s already many threads on this in the forum.
I think you should declare $pageslotId as a virtual attribute (public property) in "this" model. No need to instantiate NewsPublication in the search() method. The desired filter value should be brought back from the posted view in the "this" model and should be available as $this->pageslotId (massively assigned by the controller).
(As said, completely untested)
/Tommy
tri:
Assuming you feed a CGridView with data generated by this provider, as well as an instance of "this" model as filter.
I haven’t yet worked with these filters myself but there’s already many threads on this in the forum.
I think you should declare $pageslotId as a virtual attribute (public property) in "this" model. No need to instantiate NewsPublication in the search() method. The desired filter value should be brought back from the posted view in the "this" model and should be available as $this->pageslotId (massively assigned by the controller).
(As said, completely untested)
/Tommy
I’m doing well:
view
array(
'name' => 'slotName',
'filter' => CHtml::activeDropDownList(NewsPublication::model(), 'pageslot_id', CHtml::listData(NewsPageSlot::model()->findAll(), 'id', 'name'), array('prompt' => ''))
),
model:
public $slotName;
public function relations()
{
return array(
'newsPublications' => array(self::HAS_MANY, 'NewsPublication', 'news_id'),
'newsPageSlots'=>array(self::HAS_MANY,'NewsPageSlot','pageslot_id', 'through'=>'newsPublications'),
);
}
public function search()
{
$criteria=new CDbCriteria;
$criteria->with = array('newsPageSlots');
# utiliza through no relacionamento
$criteria->together = true;
$criteria->compare('newsPageSlots.name', $this->slotName);
return new CActiveDataProvider(get_class($this), array(
'criteria'=>$criteria,
));
}
tri:
Glad to hear
/Tommy
I’m doing it like you said, but I can not send the value of the combo filter, in addition, when selecting the value of the combo filter, it does not stay set, loses value.
The only way I can recover the value is as follows:
$criteria->compare('newsPageSlots.id', $_GET['NewsPublication']['pageslot_id']);
But beyond that I find wrong in doing this model, the combo filter continues to lose value.
Thanks Tommy!