简体   繁体   中英

Yii CDbCriteria select with AS alias

I'm joining page_content table with the same and after that both have column named sef_url Following code returns an error. Is there any possibility to get the AS 'alias_name' use inside of CDbCriteria->select

$criteria=new CDbCriteria;
$criteria->alias = 'p';
$criteria->select = array('p.sef_url, p.date_last_modified, pc.sef_url AS parent_sef_url');
$criteria->join = 'LEFT JOIN page_content pc ON p.parent_pageid = pc.pageid';

$criteria->condition = 'p.published=:published AND p.sitemap_index=:sitemap_index';
$criteria->params = array(':published'=>1,':sitemap_index'=>0); 

$result_arr = parent::model()->findAll($criteria);

echo '<pre>';
print_r($result_arr);

foreach($result_arr as $row) {
  echo $row['parent_sef_url'];
}

Also you should define a relation:

public function relations()
{
    return array(
        'parentPageContent'=>array(self::BELONGS_TO, 'PageItem', 'parent_pageid'),
    );
}

After you can do that:

$pageContents = PageContent::model()->with('parentPageContent')->findAllByAttributes(array(':published'=>1,':sitemap_index'=>0));
foreach($pageContents as $pageContent){
    echo $pageContent->parentPageContent->sef_url;
}

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