Confusing Problem With Saving A Model - Help Please?

Hi all, I am having some trouble with the following method in one of my Models




public function assignUserRole($user, $project, $role){

            Yii::log('into assignUserRole','info', 'models.ProjectController');

            $this->projectId = $project;

            $this->userId = $user;

            $this->role = $role;

            Yii::log('projectId= '.$this->projectId." userId = ".$this->userId." role = ".$this->role,'info', 'models.ProjectController');

            $this->dateAssigned = date(DateTime::ISO8601);

            print_r($this->errors);

             if(!$this->save()){ 

                 CVarDumper::dump($this->getErrors()); 

             }else{ return true;}

        }



The method is returning true however nothing is being saved in my DB.

the print_r($this->errors); line is just printing array( ) to my browser.

nothing of interest is being logged - here is my application log




2013/03/17 22:48:38 [info] [models.ProjectController] into assignUserRole

in /Applications/MAMP/htdocs/inkwiki/protected/models/ProjectTeam.php (106)

in /Applications/MAMP/htdocs/inkwiki/protected/controllers/InviteController.php (126)

in /Applications/MAMP/htdocs/inkwiki/index.php (13)

2013/03/17 22:48:38 [info] [models.ProjectController] projectId= 49 userId = 2 role = member

in /Applications/MAMP/htdocs/inkwiki/protected/models/ProjectTeam.php (110)

in /Applications/MAMP/htdocs/inkwiki/protected/controllers/InviteController.php (126)

in /Applications/MAMP/htdocs/inkwiki/index.php (13)



I have tried setting save(false) - this makes no difference, still get no log data and nothing is saved to the DB.

Any idea why this isn’t working for me?

Thanks in advance!

Hi,

I would check the insert sentence, try to print it in the log, although I think if you have:


defined('YII_DEBUG') or define('YII_DEBUG',true);

in your index.php should print the sentence.

Also, are you creating the project model in somewhere else? like:


$project = new Project;

In addition to what is said before, I’d also check before saving the model if validation is passed,

e.g.




$this->validate();

var_dump($this->errors);



and would check model attributes, especially if record identifier is present


print_r($this->attributes);