A Ghost Label

i build a simple blog with yii

when i try to create a new post it tell me that author cannot be blank

but i dont have any label of author

(it should be full "by it self")

someone have any idea?

tnx

Check the validation rules in model.

so this is my post model

and it check the author

where else it should be checked?

public function rules()

{

// NOTE: you should only define rules for those attributes that

// will receive user inputs.

return array(

array(‘title, content, status, author_id’, ‘required’),

array(‘status’, ‘in’, ‘range’=>array(1,2,3)),

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’),

);

}

I’m not sure what you’re trying to do.

If you don’t need required author field, just remove author_id from the validation rule.

well when i try to remove it

so i get an error note

"[CDbException

CDbCommand failed to execute the SQL statement: SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (blog.tbl_post, CONSTRAINT FK_post_author FOREIGN KEY (author_id) REFERENCES tbl_user (id) ON DELETE CASCADE). The SQL statement executed was: INSERT INTO tbl_post (title, content, tags, status, update_time, create_time, author_id) VALUES (:yp0, :yp1, :yp2, :yp3, :yp4, :yp5, :yp6)"

(maybe if you know so actually i build the same demo blog of yii)

tnx

If you removed the rule for "author" you also have to remove at least the foreign key in your database and/or additional remove the author column in post table.

If you want to have the author something like you wrote before ("by it self") then you can leave the column/foreign key but create one dummy author "by it self" and assign every post to this author.

In short words, right after


$model= new Post;



do


$model->author= 'author_id';

did that but nothing

just stay the same.

public function actionCreate()

{


	$model=new Post;


	$model->author= 'author_id';


	if(isset($_POST['Post']))


	{


		$model->attributes=$_POST['Post'];


		if($model->save())


			$this->redirect(array('view','id'=>$model->id));


	}





	$this->render('create',array(


		'model'=>$model,


	));


}

See Customizing create and update Operations of the blog demo guide.




// instead of:

$this->author_id=Yii::app()->user->id;

// use this:

$this->author_id=1; // where "1" is a valid user id from user table (e.g. id of user "by it self" <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/wink.gif' class='bbc_emoticon' alt=';)' />)



Your ‘author_id’ is a foreign key probably related to ‘id’ in ‘author’ table. This ‘id’ is likely to be an integer.

Changing this:


$model->author= 'author_id';

to this:


$model->author = 1;

should do the trick, but all relations will then be with the author who’s id = 1.

You will need to learn more if you want to do anything serious though because that is a nasty hack.

tnx all , guys.