ActiveRecord model not writing to database

Hi everyone,

I am new to Yii (and to PHP), I was trying my first project using Yii framework but I am having issues writting data to a database from one of the components.

The code I use is:

$post = new Doctors;

$post->fullname = $doctor['fullname'];

$post->firstname = $doctor['firstname'];

$post->lastname = $doctor['lastname'];

$post->address1 = $doctor['address1'];

if (array_key_exists('address2', $doctor)){

	$post->address2 = $doctor['address2'];


$post->recognitions = $doctor['recognitions'];

$post->speciality = $doctor['speciality'];

$post->postalcode = $doctor['postalcode'];

$post->locality = $doctor['locality'];

if (array_key_exists('phone', $doctor))


	$post->phone = $doctor['phone'];


if (array_key_exists('fax', $doctor))


	$post->fax = $doctor['fax'];



The application runs through this code without crashing or showing any errors but nothing gets saved into the database.

Am I missing something?


It can be simplified to:

$post = new Doctors();

$post->attributes = $doctor;


Check return value of $post->save(). If it’s false, try var_dump($post->errors).

Make sure in your model that you have rules() defined for each attribute. If you don’t have at least one validator for each the data cannot be massively assigned.

IT is t’he model created by Gii so I am assuming it is correct.

I cull try checking the return value is save()

Where does $doctor come from? Are you assigning $_POST[‘whatever’] to $doctor?

Great, the var_dump($post->errors) really worked for me since I was able to undestand what was wrong … I was basically not filling up a couple of fields that were required to be not empty.

I was not able to use the simplified sintax that you proposed to fill in the $post->atributes… I will look into that later though. Thanks.