2011年12月13日

Yii Framework: テーブルカラムに別名をつける

SQL の select で SUM, MIN, MAX やらを使い AS で別名をつける場合、Yii ではモデルにプロパティを追加します。

<?php
class Forum extends ActiveRecord
{
public $max_number;
public $max_create_time;
...
public function getLatestAll()
{
$c = new CDbCriteria();
$c->with = array('topic');
$c->select = 'MAX(t.number) AS max_number, MAX(t.create_time) AS max_create_time';
$c->group = 't.topic_id';
$c->order = 'mac_create_time DESC';
return $this->findAll($c);
}
...
view raw Forum.php hosted with ❤ by GitHub
<?php foreach ($models as $model): ?>
<?php echo CHtml::encode($model->max_createtime); ?>
<?php echo CHtml::encode($model->max_number); ?>
...
<?php endforeach; ?>
view raw index.php hosted with ❤ by GitHub

0 件のコメント:

コメントを投稿