Yii Framework Forum: A Ghost Label - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

A Ghost Label help please Rate Topic: -----

#1 User is offline   asafswis 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 17-September 12

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
0

#2 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,694
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 09 October 2012 - 03:55 AM

Check the validation rules in model.
God is real unless declared as integer
0

#3 User is offline   asafswis 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 17-September 12

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'),
);
}
0

#4 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,694
  • Joined: 20-April 09
  • Location:Moscow, Russia

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.
God is real unless declared as integer
0

#5 User is offline   asafswis 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 17-September 12

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
0

#6 User is offline   kokomo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 289
  • Joined: 23-July 10

Posted 09 October 2012 - 04:48 AM

View Postasafswis, 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

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.
0

#7 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 09 October 2012 - 06:20 AM

In short words, right after
$model= new Post;

do

$model->author= 'author_id';

0

#8 User is offline   asafswis 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 17-September 12

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,
));
}
0

#9 User is offline   kokomo 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 289
  • Joined: 23-July 10

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" ;))

0

#10 User is offline   outrage 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 313
  • Joined: 10-November 09
  • Location:Blackpool, United Kingdom

Posted 09 October 2012 - 12:02 PM

View Postasafswis, 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,
));
}


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.
0

#11 User is offline   asafswis 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 23
  • Joined: 17-September 12

Posted 09 October 2012 - 04:14 PM

tnx all , guys.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users