Commentモデルのカスタマイズ
属性のルール、名前、ラベルをカスタマイズする。
“rules()”メソッド、”attributeLabels()”メソッドが修正対象
rules()メソッドカスタマイズ
“Comment”モデルを修正
public function rules() { // NOTE: you should only define rules for those attributes that // will receive user inputs. return array( array('content, status, author, email, post_id', 'required'), // 必須 array('status, create_time, post_id', 'numerical', 'integerOnly'=>true), // 数値のみ array('author, email, url', 'length', 'max'=>128), // 128文字まで array('email', 'email'), // メールアドレス array('url', 'url'), // URL // The following rule is used by search(). // @todo Please remove those attributes that should not be searched. array('id, content, status, create_time, author, email, url, post_id', 'safe', 'on'=>'search'), ); }
attributeLabels()メソッドのカスタマイズ
モデルの各属性を表示する際のラベルを宣言する。
public function attributeLabels() { return array( 'id' => 'ID', 'content' => 'コメント', 'status' => '状態', 'create_time' => '作成日時', 'author' => '名前', 'email' => 'メール', 'url' => 'ウェブサイト', 'post_id' => '記事', ); }
保存プロセスのカスタマイズ
コメントの作成時刻を記録するため、”Post”モデル同様、”Comment”モデルの”beforeSave()”メソッドをオーバーライドする。
/** * * 自動設定処理 * 登録日:crate_time * **/ protected function beforeSave() { if(parent::beforeSave()) { if($this->isNewRecord) { // 新規登録:登録時間を登録 $this->create_time = time(); } return true; } else { return false; } }
参考サイト:
http://www.yiiframework.com/doc/blog/1.1/ja/comment.model