Page 1 of 1
A Ghost Label help please
#1
Posted 09 October 2012 - 01:10 AM
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
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
#3
Posted 09 October 2012 - 04:09 AM
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'),
);
}
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'),
);
}
#4
Posted 09 October 2012 - 04:19 AM
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.
If you don't need required author field, just remove author_id from the validation rule.
Born to create drama
#5
Posted 09 October 2012 - 04:37 AM
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
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
#6
Posted 09 October 2012 - 04:48 AM
asafswis, on 09 October 2012 - 04:37 AM, said:
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
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.
#7
Posted 09 October 2012 - 06:20 AM
In short words, right after
do
$model= new Post;
do
$model->author= 'author_id';
#8
Posted 09 October 2012 - 07:24 AM
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,
));
}
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,
));
}
#9
Posted 09 October 2012 - 09:17 AM
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" ;))
#10
Posted 09 October 2012 - 12:02 PM
asafswis, on 09 October 2012 - 07:24 AM, said:
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,
));
}
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,
));
}
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.
Share this topic:
Page 1 of 1

Help
















