licass
(L R M D)
April 24, 2013, 1:03pm
1
Thanks in advance.
I have a many-to-many relation with models:
Dispositivos
'localizacoes' => array(self::MANY_MANY, 'Localizacoes', 'dispositivos_local(id_dispositivos, id_local)'),
Localizacoes
'dispositivos' => array(self::MANY_MANY, 'Dispositivos', 'dispositivos_local(id_local, id_dispositivos)'),
how can i filter cgridview of dispositivos by field of localizacoes.id_projeto
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->with=array('localizacoes');
$criteria->compare('localizacoes.id_projeto', Yii::app()->user->projeto,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
Column not found: 1054 Unknown column ‘localizacoes.id_projeto’ in ‘where clause’
redguy
(Maciej Lizewski)
April 24, 2013, 1:05pm
2
add
$criteria->together = true;
Keith
(Kburton)
April 24, 2013, 1:13pm
4
Also, be very aware of the problems with pagination in HAS_MANY and MANY_MANY relations. This page is very helpful.
licass
(L R M D)
April 24, 2013, 1:32pm
5
And in cgridview
array(
'name'=>'localizacao',
'value'=>'$data->localizacoes->local . " ," . $data->localizacoes->distrito',
'header'=>'Local',
),
i have
Trying to get property of non-object
Keith
(Kburton)
April 24, 2013, 1:37pm
6
If it’s a MANY_MANY relationship, $data->localizacoes will be an array. You would need to either iterate through all of the elements or use a specific one by index:
'value'=>'isset($data->localizacoes[0])
? $data->localizacoes[0]->local . " ," . $data->localizacoes[0]->distrito
: ""',
Or a less ugly construct if you prefer…
licass
(L R M D)
April 24, 2013, 1:56pm
8
Keith:
If it’s a MANY_MANY relationship, $data->localizacoes will be an array. You would need to either iterate through all of the elements or use a specific one by index:
'value'=>'isset($data->localizacoes[0])
? $data->localizacoes[0]->local . " ," . $data->localizacoes[0]->distrito
: ""',
Or a less ugly construct if you prefer…
and how to do this
How i can filter if isset($_GET(‘idlocalizacao’))?
$criteria->compare('localizacoes.id_local', isset($_GET('idlocalizacao'))? $_GET('idlocalizacao'):NULL);
thanks again.
Keith
(Kburton)
April 24, 2013, 2:05pm
9
You shouldn’t be accessing $_GET in your model, that’s the job of the controller.
Read this page for information on the appropriate way to handle this.
licass
(L R M D)
April 24, 2013, 2:20pm
10
Keith:
You shouldn’t be accessing $_GET in your model, that’s the job of the controller.
Read this page for information on the appropriate way to handle this.
But,Keith ,then i get again
Trying to get property of non-object
Keith
(Kburton)
April 24, 2013, 2:32pm
11
Read through the link that I gave you; you need to implement each part of that to get this to work correctly.
licass
(L R M D)
April 24, 2013, 2:42pm
12
but what about the many-many relation.
Maybe need a coffee
Keith
(Kburton)
April 24, 2013, 2:49pm
13
The technique works fine with MANY_MANY, but you need to also take the pagination issues into account.
First, get the filter working with the help of this page.
Then, fix pagination issues with the help of this page.
The second link might also help you to understand how the filters work with MANY_MANY relations.
EDIT: Also, coffee is recommended