Yii 从另一个模型筛选自定义变量
所以基本上我有三个模型,即信用、员工和用户 信用通过员工id列属于员工模型,员工通过用户id属于用户模型 在Employee模型中,我添加了一个自定义变量$fullName,它从用户表中检索员工的姓名Yii 从另一个模型筛选自定义变量,yii,Yii,所以基本上我有三个模型,即信用、员工和用户 信用通过员工id列属于员工模型,员工通过用户id属于用户模型 在Employee模型中,我添加了一个自定义变量$fullName,它从用户表中检索员工的姓名 private $_fullName = null; public function getFullName() { if ($this->_fullName === null && $this->usr !== null) { $this-&
private $_fullName = null;
public function getFullName()
{
if ($this->_fullName === null && $this->usr !== null) {
$this->_fullName = $this->usr->last_name . ', ' . $this->usr->first_name. ' ' . $this->usr->middle_name;
}
return $this->_fullName;
}
public function setFullName($value)
{
$this->_fullName = $value;
}
public function relations()
{
return array(
'usr'=>array(self::BELONGS_TO, 'User', 'employee_id'),
);
}
现在在Credits模型中,我重用了相同的变量$fullName,并将其值设置为employee.fullName,这样我就可以访问Credits.fullName并在cgridview中使用它。我的问题是它在cgrid上工作得很好,但我不知道如何使用自定义变量过滤数据。我需要知道我可以使用什么来比较$this->fullName和credits,以便在搜索功能中进行过滤。我能想到的一个解决方案是将Credits表直接关联到User表,但我想知道是否可以只在Employee模型上使用自定义变量
学分模式:
public function getFullName()
{
if ($this->_fullName === null && $this->employee !== null) {
$this->_fullName = $this->employee->fullName;
}
return $this->_fullName;
}
public function setFullName($value)
{
$this->_fullName = $value;
}
public function relations()
{
return array(
'employee' => array(self::BELONGS_TO, 'Employee', 'employee_id'),
);
}
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->with ='employee';
???????????? what to add here
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
Cgridview:
$this->widget('zii.widgets.grid.CGridView', array(
'id'=>'credits-grid',
'dataProvider'=>$model->search(),
'filter'=>$model,
'columns'=>array(
'fullName',
添加比较方法
$criteria->compare('employee.fullName', $this->fullName, true);
你必须在方法中包含实体用户,在比较concat中包含字段名,在mysql中是这样的
$criteria->with = array('employee' => array('with' => 'user'));
$criteria->compare('concat(user.firstname, \' \', user.lastname)', $this->fullName, true);
是 啊已经尝试过了,但没有意义,因为employee表中没有全名列。您如何或如何获得此值$这个->employee->fullnamemployee模型具有自定义变量fullName,正如我所说,它检索user.firstname user.middlename和user.lastname。