Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding new field in model class using Yii framework

Tags:

php

yii

In table i have column 'description' , which contain description of goods. I want include in CGridView column 'short_description', which will contain first 150 characters.

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);     
    }

Unfortunately, this code does not work .

like image 616
DarkwaveMD Avatar asked May 03 '26 04:05

DarkwaveMD


1 Answers

You need to override afterFind function of model class and write the code

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

in afterFind function.

You need to do some thing like this

protected function afterFind()
{
    parent::afterFind();
    $this->short_description = substr($this->description, 0, 150);
}
like image 176
sadaf Avatar answered May 05 '26 17:05

sadaf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!