blog tutorial

Hi Guys,

I am trying to build a blog application step by step according to the main blog manual.

I am on "Creating and Updating Posts" chapter and when I place this line:




<?php echo CHtml::activeDropDownList($post,'status',Lookup::items('PostStatus')); ?>



I got the following error:




Fatal error: Cannot instantiate abstract class CActiveRecord in /usr/local/www/apache22/data/yii/framework/db/ar/CActiveRecord.php  on line 326



Do you have any idea what could be wrong ?

These methods are missing in the tutorial version of the Lookup model (/webroot/protected/models/Lookup.php)




	/**

	 * Returns the static model of the specified AR class.

	 * @return CActiveRecord the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return '{{lookup}}';

	}



/Tommy

Thanks

Is there a correct version of blog tutorial, because it is very complicated for a newbie like me to learn something with uncompleted/wrong tutorial.

@tri

Thanks I’m also running through the blog tutorial, and had run into the same problem. The tutorial is a bit broken in places - on the other hand you learn a darn site more when you actually have to dig around and figure out why it isn’t working.

For what it’s worth (buggy tutorial not withstanding) I am really enjoying playing with Yii - there are some lovely ideas.

Cheers

Hi all

I have had the same problem so far. I followed initially the basic tutorial from Larry Ullman but now I wanted to learn more about YII with the Blog Tutorial that looks a bit broken.

I have tried to complete it twice and now the second time I am getting a constraint error on the DB


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 (`XXXXXX_yiitest`.`tbl_post`, CONSTRAINT `FK_post_author` FOREIGN KEY (`author_id`) REFERENCES `tbl_user` (`id`) ON DELETE CASCADE)

The above is really strange as I have got the relation between author id and user->id expressed in my rules.

Is there a way in YII to actually check what is the actual SQL query/statement that the system has tried to run?

My next attempt will be with the full online guide and a book … I hope they are less confusing

If the Blog tutorial gets updated I am definitelly interested to hear about it

Read this section of The Definitive Guide to Yii.

In the config file of the application you generated with yiic, you will find a CFileLogroute already defined for you, just to uncomment. The log file will be protected/runtime/application.log. To enable logging of the actual SQL parameters, add enableParamLogging=>true to the db component.

/Tommy

I get the exact same error as Bizmate and am unable to find the problem. The log file provides no more information regarding the raw SQL than what was provided in Bizmate’s post.

If you get the error "foreign key constraint" while trying to add some new records to a table… it means that you are trying to add a value to the foreign key that does not exist in the related table…

For example lets say that you want to add a record to the posts table… and for the author_id you are trying to add the value 2… if in the author table there is no record with the id 2… you will get this error…

I got the same problem, in the Chapter 3 Post management in the blog tutorial.

I got the error




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 (`yiiblog`.`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`) VALUES (:yp0, :yp1, :yp2, :yp3) 



It sees the rules in the post model was not correct, I updated it from


array('title, content, status', 'required'),

to


array('title, content, status,author_id', 'required')

and it solved the problem. Not sure why I have to explicitly specify the author_id attribute.

It doesn’t seem like a good solution. The reason of the problem is that when we do





 if($this->isNewRecord) {

            	$this->create_time=$this->update_time=time();

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

        	}



in our Post model’s beforeSave(), Yii::app()->user->id gives NOT an id, but a username. The fix can be found here.

Cheers