Please read DigitalZombie’s reply for what looks to be the answer. You must ensure that the Project class extends the base active record class TrackStarActiveRecord. It is in this base class where the create_user_id is being set to the current app user id, and indeed part of what is being tested:
<?php
/**
* This is the model class for table "tbl_project".
*/
class Project extends [b]TrackStarActiveRecord[/b]
{
...
<?php
abstract class TrackStarActiveRecord extends CActiveRecord
{
/**
* Prepares create_time, create_user_id, update_time and update_user_id attributes before performing validation.
*/
protected function beforeValidate()
{
if($this->isNewRecord)
{
// set the create date, last updated date and the user doing the creating
$this->create_time=$this->update_time=new CDbExpression('NOW()');
$this->create_user_id=$this->update_user_id=Yii::app()->user->id;
}
else
{
//not a new record, so just set the last updated time and last updated user id
$this->update_time=new CDbExpression('NOW()');
$this->update_user_id=Yii::app()->user->id;
}
return parent::beforeValidate();
}
}
I’ve just found the solution, maybe it will be useful to someone else so I’m posting it. The issue was not my Project class, it was my ProjectTest class (function testCreate()) :
//save the new project, triggering attribute validation
$this->assertTrue($newProject->save(false));
must become :
//save the new project, triggering attribute validation
$this->assertTrue($newProject->save());
It was specified in the book (in bold page 151) so it’s my mistake, it seems I can’t follow correctly
Thanks for posting this. I also missed it. That one small word ‘false’ is easy to miss. Most of the issues I’ve had through chapter 8 so far have been tiny mistakes such as this so we have to go back and carefully scan every line altered. Not so fun if you take big steps and have to go way back.
I have checked my code perfect as per given book and dually checked as per "khad" comments on this forum i dont have this silly mistakes. My code is :
public function testCreate()
{
//CREATE a new Project
$newProject=new Project;
$newProjectName = 'Test Project Creation';
$newProject->setAttributes(array(
'name' => $newProjectName,
'description' => 'This is a test for new project creation',
)
);
//set the application user id to the first user in our users fixture data
Yii::app()->user->setId($this->users('user1')->id);
//save the new project, triggering attribute validation
$this->assertTrue($newProject->save());
//READ back the newly created Project to ensure the creation worked
$retrievedProject=Project::model()->findByPk($newProject->id);
$this->assertTrue($retrievedProject instanceof Project);
$this->assertEquals($newProjectName,$retrievedProject->name);
i got the same problem, but i fix this by make sure all the variables name, cause the code was not same at all, for example: in the testUpdate there is, $updatedProject = Project::model()->findByPk($project->id);
but in the previous code that’s written, $updatedProject = Project::model()->findByPk($newProject->id);