简体   繁体   中英

yii 1.1 dropDownList with parent Category Name

We have the Categories table with columns: id parent_id title

Also we have the relation :

public function relations()
    {
        return array(
                    'Parent' => array(self::BELONGS_TO, 'Categories', 'parent_id'),
        );
    }

We use the function :

public function getFullCategory() 
{
          $showparentname = 'Parent.title';
              return $this->$showparentname.' - '.$this->title;
}

The form dropDownList use:

$categories = Categories::model()->findAll();
$categories_list = CHtml::listData($categories, 'id', 'FullCategory');

But it doesn't work

Property "Categories.Parent.title" is not defined.

You can't use 'Parent.title', it's triing to get $this->Parent.title property. Use this:

function getTitleWithParent(){
    return ( $this->Parent !== null ? $this->Parent->title.' - ' : '' ).$this->title;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM