Issue About Add Record With Activerecord

I have tbl_test table with this structure: ID:PK,AI; name:varchar ; family:varchar;

And have th following method in tests/unit/TestTest.php:


	public function testARCreate()

	{

		$newUser=new Test;

		$newUser->setAttributes(array(

			'name' => 'your name',

			'family' => 'your family',

			)

		);

		$newUser->save();

	}

The above code just insert ID attribute that created automately by DBMS, and name and family are empty in the DBMS. but following code work correctly:


	public function testARCreate()

	{

		$newUser=new Test;

		$newUser->name='your name';

		$newUser->family='your family';

		$newUser->save();

	}

Is any one for help? thanks

It’s because you don’t have validation rules for ‘name’ and ‘family’.

Try …




$newUser->setAttributes(array(

        'name' => 'your name',

        'family' => 'your family', 

    ),

    false  // without validation

);



http://www.yiiframework.com/doc/api/1.1/CModel#setAttributes-detail

But the recommended way is to define the rules.

oh, yse. Since i define Test model manually, forgotten define Rules method. Indeed its better use from safe for those attributes dont want validate. however your method worked.