微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

php – 使用Yii框架在模型类中添加新字段

在表格中,我有列’描述’,其中包含商品描述.
我希望在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);     
    }

不幸的是,这段代码不起作用.

解决方法:

您需要覆盖模型类的afterFind函数并编写代码

$this-> short_description = substr($this-> description,0,150);

在afterFind函数中.

你需要做这样的事情

protected function afterFind()
{
    parent::afterFind();
    $this->short_description = substr($this->description, 0, 150);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐