I’m currently using Yii 1.1.6
I kept receiving project.update_time is not defined error. What am I doing wrong?
1211
Here is my TrackStarActiveRecord
<?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 = Yii::app()->user->id;
}
return parent::beforeValidate();
}
}
Here is my ProjectTest
<?php
class ProjectTest extends CDbTestCase
{
public $fixtures=array
(
'projects' => 'Project',
'users' => 'User',
'projUsrAssign' => ':tbl_project_user_assignment',
);
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',
// 'create_time' => '2009-09-09 00:00:00',
// 'create_user_id' => '1',
//'update_time' => '2009-09-09 00:00:00',
// 'update_user_id' => '1',
));
//set the application user id to the first user in our user fixture data
Yii::app()->user->setId($this->users('user1')->id);
//set 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);
$this->assertEquals(Yii::app()->user->id,$retrievedProject->create_user_id);
}
public function testRead()
{
$retrievedProject = $this->projects('project1');
$this->assertTrue($retrievedProject instanceof Project);
$this->assertEquals('Test project 1',$retrievedProject->name);
}
public function testUpdate()
{
$project = $this->projects('project2');
$updatedProjectName = 'Updated Test Project 2';
$project->name = $updatedProjectName;
$this->assertTrue($project->save(false));
//read back the record again to ensure the update worked
$updatedProject=Project::model()->findByPk($project->id);
$this->assertTrue($updatedProject instanceof Project);
$this->assertEquals($updatedProjectName,$updatedProject->name);
}
public function testDelete()
{
$project = $this->projects('project2');
$savedProjectId = $project->id;
$this->assertTrue($project->delete());
$deletedProject=Project::model()->findByPk($savedProjectId);
$this->assertEquals(NULL,$deletedProject);
}
public function testGetUserOptions()
{
$project = $this->projects('project1');
$options = $project->userOptions;
$this->assertTrue(is_array($options));
$this->assertTrue(count($options) > 0);
}
}
Thanks