Hello,
I have a 1:N relation that I leave below.
On the admin.php of the tbl_pessoa I can add the column that have all the values that I need from tbl_acordo. In this case for every record in tbl_pessoa I need to see all values that the "referencia" field has in tbl_acordo.
And in this case, this Person has 2 related records in tbl_acordo
The only problem is, that I can’t filter this column that I added. Can anyone shed me some light in this issue?
Thanks.
PS: those black squares on the images are made because I’m working with real values.
admin.php
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'id' => 'pessoa-grid',
'dataProvider' => $model->searchByEmpresa($_GET['idempresa']),
'filter' => $model,
'columns' => array(
'nome',
'nif',
array(
'name'=>'referencia',
'type'=>'raw',
),
array(
'class' => 'CButtonColumn',
'template' => '{acordos} {view} {update} {delete}',
'htmlOptions' => array('style' => 'width:85px'),
'buttons' => array(
'delete' => array(
'visible' => 'PessoaController::checkAcessWModel($data->id) > 3'
),
'update' => array(
'visible' => 'PessoaController::checkAcessWModel($data->id) > 2'
),
'view' => array(
'visible' => 'PessoaController::checkAcessWModel($data->id) > 1'
),
'acordos' => array(
'visible' => 'PessoaController::checkAcessWModel($data->id) > 2',
'label' => 'Ver Acordos',
'imageUrl' => Yii::app()->request->baseUrl . '/images/rar_16.png',
'url' => 'Yii::app()->createUrl("acordo/admin", array ( "idpessoa" => $data->id))',
),
),
),
),
));
?>
(Model)
Pessoa.php
class Pessoa extends CActiveRecord {
public $referencia;
/**
* Returns the static model of the specified AR class.
* @return Pessoa the static model class
*/
public static function model($className=__CLASS__) {
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName() {
return '{{pessoa}}';
}
/**
* @return array validation rules for model attributes.
*/
public function rules() {
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('nome, morada, concelho, codpostal, nif, bi, idempresa', 'required'),
array('nif, bi, idempresa', 'numerical', 'integerOnly' => true),
array('nome, email', 'length', 'max' => 250),
array('concelho', 'length', 'max' => 150),
array('codpostal', 'length', 'max' => 30),
array('telefone, telemovel, fax', 'length', 'max' => 50),
array('info', 'safe'),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, nome, morada, concelho, codpostal, nif, bi, telefone, telemovel, email, fax, info, idempresa', 'safe', 'on' => 'search'),
);
}
/**
* @return array relational rules.
*/
public function relations() {
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'acordos' => array(self::HAS_MANY, 'Acordo', 'idpessoa'),
'idempresa0' => array(self::BELONGS_TO, 'Empresa', 'idempresa'),
);
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels() {
return array(
'id' => 'ID',
'nome' => 'Nome',
'morada' => 'Morada',
'concelho' => 'Concelho',
'codpostal' => 'Codpostal',
'nif' => 'Nif',
'bi' => 'Bi',
'telefone' => 'Telefone',
'telemovel' => 'Telemovel',
'email' => 'Email',
'fax' => 'Fax',
'info' => 'Info',
'idempresa' => 'Idempresa',
);
}
/**
* Retrieves a list of models filtered by idempresa based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function searchByEmpresa($idempresa) {
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria = new CDbCriteria;
$criteria->compare('id', $this->id);
$criteria->compare('nome', $this->nome, true);
$criteria->compare('morada', $this->morada, true);
$criteria->compare('concelho', $this->concelho, true);
$criteria->compare('codpostal', $this->codpostal, true);
$criteria->compare('nif', $this->nif, true);
$criteria->compare('bi', $this->bi);
$criteria->compare('telefone', $this->telefone, true);
$criteria->compare('telemovel', $this->telemovel, true);
$criteria->compare('email', $this->email, true);
$criteria->compare('fax', $this->fax, true);
$criteria->compare('info', $this->info, true);
$criteria->compare('idempresa', $idempresa);
$criteria->compare('referencia', $this->referencia, true);
return new CActiveDataProvider(get_class($this), array(
'criteria' => $criteria,
));
}
public function getAcordos($acordos){
$arc='';
foreach ($acordos as $a){
$arc .= $a->referencia .' ';
}
return $arc;
}
public function beforeFind() {
$this->referencia = $this->getAcordos($this->acordos);
parent::beforeFind();
}
public function afterFind() {
parent::afterFind();
$this->referencia = $this->getAcordos($this->acordos);
}
}