the model is Blog model. i put the code below. it has a base class, generated using Giix module.
i just wonder, is that happen (can not save/return null) because i use "echo CHtml::dropDownList" instead of "echo $form->dropDownList"? if i use "echo $form->dropDownList" for dependent dropdown, i get a PHP Warning, but "echo CHtml::dropDownList" works fine, but it return null and i cannot save it.
Blog.php
//base blog code below blog.php
<?php
Yii::import('application.models.skn._base.BaseBlog');
class Blog extends BaseBlog
{
private $_oldTags;
public static function model($className=__CLASS__) {
return parent::model($className);
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('title, content, blog_status', 'required'),
//array('blog_status', 'in', 'range'=>array(1,2,3,4)),
array('title', 'length', 'max'=>128),
array('tags', 'match', 'pattern'=>'/^[\w\s,]+$/', 'message'=>'Tags can only contain word characters.'),
array('tags', 'normalizeTags'),
array('title, status', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'author' => array(self::BELONGS_TO, 'User', ObjectSender::getColumnAuthorId()),
'comments' => array(self::HAS_MANY, 'Comment', ObjectSender::getColumnBlogId(),
'condition'=>'comments.comment_status='.ObjectSender::getStatusApproved(),
'order'=>'comments.create_time DESC'),
'commentCount' => array(self::STAT, 'Comment', ObjectSender::getColumnBlogId(),
'condition'=>ObjectSender::getColumnCommentStatus().'='.ObjectSender::getStatusApproved()),
);
}
/**
* @return string the URL that shows the detail of the blog
*/
public function getUrl()
{
return Yii::app()->createUrl('skn/blog/view', array(
'id'=>$this->blog_id,
'title'=>$this->title,
));
}
/**
* @return array a list of links that point to the blog list filtered by every tag of this blog
*/
public function getTagLinks()
{
$links=array();
foreach(Tag::string2array($this->tags) as $tag)
$links[]=CHtml::link(CHtml::encode($tag), array('skn/blog/index', 'tag'=>$tag));
return $links;
}
/**
* Normalizes the user-entered tags.
*/
public function normalizeTags($attribute,$params)
{
$this->tags=Tag::array2string(array_unique(Tag::string2array($this->tags)));
}
/**
* Adds a new comment to this blog.
* This method will set status and blog_id of the comment accordingly.
* @param Comment the comment to be added
* @return boolean whether the comment is saved successfully
*/
public function addComment($comment)
{
$comment_status=ObjectSender::getColumnCommentStatus();
$blog_id=ObjectSender::getColumnBlogId();
//if(Yii::app()->params['commentNeedApproval'])
$commentApproval = ObjectSender::getCommentApproval();
if(Yii::app()->$commentApproval)
$comment->$comment_status=ObjectSender::getStatusPending ();
else
$comment->$comment_status=ObjectSender::getStatusApproved();
$comment->$blog_id=$this->$blog_id;
return $comment->save();
}
/**
* This is invoked when a record is populated with data from a find() call.
*/
protected function afterFind()
{
parent::afterFind();
$this->_oldTags=$this->tags;
}
/**
* This is invoked before the record is saved.
* @return boolean whether the record should be saved.
*/
protected function beforeSave()
{
if(parent::beforeSave())
{
if($this->isNewRecord)
{
$this->create_time=$this->update_time=time();
$this->author_id=Yii::app()->user->id;
}
else
$this->update_time=time();
return true;
}
else
return false;
}
/**
* This is invoked after the record is saved.
*/
protected function afterSave()
{
parent::afterSave();
Tag::model()->updateFrequency($this->_oldTags, $this->tags);
}
/**
* This is invoked after the record is deleted.
*/
protected function afterDelete()
{
parent::afterDelete();
Comment::model()->deleteAll('blog_id='.$this->blog_id);
Tag::model()->updateFrequency($this->tags, '');
}
/**
* Retrieves the list of blogs based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the needed blogs.
*/
public function search()
{
$blog_status=ObjectSender::getColumnBlogStatus();
$criteria=new CDbCriteria;
$criteria->compare('title',$this->title,true);
$criteria->compare($blog_status,$this->blog_status);
return new CActiveDataProvider('Blog', array(
'criteria'=>$criteria,
'sort'=>array(
'defaultOrder'=> $blog_status.', update_time DESC',
),
));
}
}
BaseBlog.php
<?php
/**
* This is the model base class for the table "{{blog}}".
* DO NOT MODIFY THIS FILE! It is automatically generated by giix.
* If any changes are necessary, you must set or override the required
* property or method in class "Blog".
*
* Columns in table "{{blog}}" available as properties of the model,
* followed by relations of table "{{blog}}" available as properties of the model.
*
* @property integer $blog_id
* @property integer $category_id
* @property string $title
* @property string $content
* @property string $tags
* @property integer $blog_status
* @property integer $create_time
* @property integer $update_time
* @property integer $author_id
*
* @property User $author
* @property Category $category
* @property PostStatus $blogStatus
* @property Comment[] $comments
*/
abstract class BaseBlog extends GxActiveRecord {
public static function model($className=__CLASS__) {
return parent::model($className);
}
public function tableName() {
return '{{blog}}';
}
public static function label($n = 1) {
return Yii::t('app', 'Blog|Blogs', $n);
}
public static function representingColumn() {
return 'title';
}
public function rules() {
return array(
array('category_id, title, content, blog_status, author_id', 'required'),
array('category_id, blog_status, create_time, update_time, author_id', 'numerical', 'integerOnly'=>true),
array('title', 'length', 'max'=>128),
array('tags', 'safe'),
array('tags, create_time, update_time', 'default', 'setOnEmpty' => true, 'value' => null),
array('blog_id, category_id, title, content, tags, blog_status, create_time, update_time, author_id', 'safe', 'on'=>'search'),
);
}
public function relations() {
return array(
'author' => array(self::BELONGS_TO, 'User', 'author_id'),
'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
'blogStatus' => array(self::BELONGS_TO, 'PostStatus', 'blog_status'),
'comments' => array(self::HAS_MANY, 'Comment', 'blog_id'),
);
}
public function pivotModels() {
return array(
);
}
public function attributeLabels() {
return array(
'blog_id' => Yii::t('app', 'Blog'),
'category_id' => null,
'title' => Yii::t('app', 'Title'),
'content' => Yii::t('app', 'Content'),
'tags' => Yii::t('app', 'Tags'),
'blog_status' => null,
'create_time' => Yii::t('app', 'Create Time'),
'update_time' => Yii::t('app', 'Update Time'),
'author_id' => null,
'author' => null,
'category' => null,
'blogStatus' => null,
'comments' => null,
);
}
public function search() {
$criteria = new CDbCriteria;
$criteria->compare('blog_id', $this->blog_id);
$criteria->compare('category_id', $this->category_id);
$criteria->compare('title', $this->title, true);
$criteria->compare('content', $this->content, true);
$criteria->compare('tags', $this->tags, true);
$criteria->compare('blog_status', $this->blog_status);
$criteria->compare('create_time', $this->create_time);
$criteria->compare('update_time', $this->update_time);
$criteria->compare('author_id', $this->author_id);
return new CActiveDataProvider($this, array(
'criteria' => $criteria,
));
}
}