在表格中,我有列’描述’,其中包含商品描述.
我希望在CGridView列中包含’short_description’,它将包含前150个字符.
class Product extends CActiveRecord
{
/**
* The followings are the available columns in table 'Product':
* @var integer $id
* @var integer $id_cat
* @var string $title
* @var string $description
* @var timestamp $date
*/
public $id;
public $id_cat;
public $title;
public $description;
public $date;
public $short_description ;
public function init()
{
$this->short_description = substr($this->description, 0, 150);
}
不幸的是,这段代码不起作用.
解决方法:
$this-> short_description = substr($this-> description,0,150);
在afterFind函数中.
你需要做这样的事情
protected function afterFind()
{
parent::afterFind();
$this->short_description = substr($this->description, 0, 150);
}