Last active
September 28, 2015 10:38
-
-
Save jamband/1426023 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Hoge extends CActiveRecord | |
{ | |
public $username; // 追加 | |
... | |
/** | |
* @see CModel::rules() | |
*/ | |
public function rules() | |
{ | |
return array( | |
array('id, username, fuga, piyo', 'safe', 'on'=>'search'), // user_idをusernameに変更 | |
); | |
} | |
/** | |
* @see CActiveRecord::relations() | |
*/ | |
public function relations() | |
{ | |
return array( | |
'user' => array(self::BELONGS_TO, 'User', 'user_id'), | |
); | |
} | |
/** | |
* Retrieves a list of models based on the current search/filter conditions. | |
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions. | |
*/ | |
public function search() | |
{ | |
$c = new CDbCriteria(); | |
$c->with = array('user'); // 追加 | |
$c->compare('id', $this->id); | |
$c->compare('username', $this->username, true); // user_idをusernameに変更 | |
$c->compare('fuga', $this->fuga, true); | |
$c->compare('piyo', $this->piyo, true); | |
return new CActiveDataProvider(get_class($this), array( | |
'criteria' => $c, | |
'sort' => array( | |
'defaultOrder' => 't.id DESC', | |
// usernameがソートできるようにasc, descを設定 | |
'attributes' => array( | |
'username' => array( | |
'asc' => 'username, t,id', | |
'desc' => 'username DESC, t.id', | |
), | |
'*', // これは「その他すべて」という意味 | |
), | |
), | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment