Error on database

Hello friends

I have a error when I Submit information to save on database, this the error:

This is the class Project:




<?php


/**

 * This is the model class for table "tbl_project".

 *

 * The followings are the available columns in table 'tbl_project':

 * @property integer $id

 * @property string $name

 * @property string $description

 * @property string $create_time

 * @property integer $create_user_id

 * @property string $update_time

 * @property integer $update_user_id

 *

 * The followings are the available model relations:

 */

class Project extends TrackStarActiveRecord

{

	/**

	 * Returns the static model of the specified AR class.

	 * @return Project the static model class

	 */

	public static function model($className=__CLASS__)

	{

		return parent::model($className);

	}


	/**

	 * @return string the associated database table name

	 */

	public function tableName()

	{

		return 'tbl_project';

	}


	/**

	 * @return array validation rules for model attributes.

	 */

	public function rules()

	{

		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(

			array('create_user_id, update_user_id', 'numerical', 'integerOnly'=>true),

			array('name', 'length', 'max'=>128),

			array('description, create_time, update_time', 'safe'),

			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('id, name, description, create_time, create_user_id, update_time, update_user_id', 'safe', 'on'=>'search'),

                        array('name, description','required'),

		);

	}


	/**

	 * @return array relational rules.

	 */

	

        public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'issues' => array(self::HAS_MANY, 'Issue', 'project_id'),

			'users' => array(self::MANY_MANY, 'User', 'tbl_project_user_assignment(project_id, user_id)'),

		);

	}


	/**

	 * @return array customized attribute labels (name=>label)

	 */

	public function attributeLabels()

	{

		return array(

			'id' => 'ID',

			'name' => 'Name',

			'description' => 'Description',

			'create_time' => 'Create Time',

			'create_user_id' => 'Create User',

			'update_time' => 'Update Time',

			'update_user_id' => 'Update User',

		);

	}


	/**

	 * Retrieves a list of models based on the current search/filter conditions.

	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.

	 */

	public function search()

	{

		// Warning: Please modify the following code to remove attributes that

		// should not be searched.


		$criteria=new CDbCriteria;


		$criteria->compare('id',$this->id);

		$criteria->compare('name',$this->name,true);

		$criteria->compare('description',$this->description,true);

		$criteria->compare('create_time',$this->create_time,true);

		$criteria->compare('create_user_id',$this->create_user_id);

		$criteria->compare('update_time',$this->update_time,true);

		$criteria->compare('update_user_id',$this->update_user_id);


		return new CActiveDataProvider(get_class($this), array(

			'criteria'=>$criteria,

		));

	}


       

        public function getUserOptions()

        {

            $usersArray = CHtml::listData($this->users, 'id', 'username');

            return $usersArray;


        }


        /*

         * creartes an associantion between the project, the user and the user's role within the project

         */

        public function associateUserToRole($role,$userId)

        {

            $sql = "insert into tbl_project_user_role (project_id, user_id, role)values(:projectId, :userId, :role)";

            $command = Yii::app()->db->createCommand($sql);

            $command->bindValue(":projectId", $this->id, PDO::PARAM_INT);

            $command->bindValue(":userId", $userId, PDO::PARAM_INT);

            $command->bindValue(":role", $role, PDO::PARAM_STR);

            return $command->execute();

        }

        public function removeUserFromRole($role,$userId)

        {

            $sql = "delete from tbl_project_user_role where project_id=:projectId and user_id=:userId and role=:role";

            $command = Yii::app()->db->createCommand($sql);

            $command->bindValue(":projectId", $this->id, PDO::PARAM_INT);

            $command->bindValue(":userId", $userId, PDO::PARAM_INT);

            $command->bindValue(":role", $role, PDO::PARAM_STR);

            return $command->execute();

        }

        /*

         * @return boolean whether or not the current user is in the specified role within the context of this project

         *

         */


        public function isUserInRole($role)

        {

            $sql = "select role from tbl_project_user_role where project_id=:projectId and user_id=:userId and role=:role";

            $command = Yii::app()->db->createCommand($sql);

            $command->bindValue(":projectId", $this->id, PDO::PARAM_INT);

            $command->bindValue(":userId", Yii::app()->user->getId(), PDO::PARAM_INT);

            $command->bindValue(":role", $role, PDO::PARAM_STR);

            return $command->execute() == 1 ? true : false;

        }


        /*

         * returns an array of available roles in which a user can be placed when being added to a project

         */

        public static function getUserRoleOptions()

        {

            return CHtml::listData(Yii::app()->authManager->getRoles(), 'name', 'name');

        }


        /*

         * makes as associantion betwaeen a user and a the project

         */

        public function associateUserToProject($user)

        {

            $sql = "insert into tbl_project_user_assignment(project_id, user_id)values(:projectId, :userId)";

            $command = Yii::app()->db->createCommand($sql);

            $command->bindValue(":projectId", $this->id, PDO::PARAM_INT);

            $command->bindValue(":userId", $user->id, PDO::PARAM_INT);

            return $command->execute();

        }


        /*

         * determines whether or not a user is already part of a project

         */

        public function isUserInProject($user)

        {

            $sql = "select user_id from tbl_project_user_assignment where project_id=:projectId and user_id=:userId";

            $command = Yii::app()->db->createCommand($sql);

            $command->bindValue(":projectId", $this->id, PDO::PARAM_INT);

            $command->bindValue(":userId", $user->id, PDO::PARAM_INT);

            return $command->execute()==1 ? true : false;

        }

}



This is the class ProjectUserForm:




<?php

/* 

 * To change this template, choose Tools | Templates

 * and open the template in the editor.

 */


/**

 * Description of ProjectUserForm

 *

 * @author alexandre

 */

class ProjectUserForm extends CFormModel

{

    /*

     * @var stgring usename of the user being added to the project

     */

    public $username;

    /*

     * var string the role to which the usedr will be associated

     */

    public $role;


    /*

     * @var object as instance of the project ar model class

     */

    public $project;


    /*

     * declares the validation rules

     * The rules state that username and password are required, and password needs to be authenticated using the verify() method

     */


    public function rules()

    {

        return array(

            //username and password are required

            array('username, role', 'required'),

            //password needs to be authenticated

            array('username', 'exist', 'className'=>'User'),

            array('username', 'verify'),

        );

    }


    /*

     * Authenticates the existence of the user in the system.

     * If valid, it will also make the association between the user, role and project

     * This is the 'verify' validator as declared in rules()

     */

    public function verify($attribute,$params)

    {

        if(!$this->hasErrors()) // we only want to authenticate when no other input erros are present

        {

            $user = User::model()->findByAttributes(array('username'=>$this->username));

            if($this->project->isUserInProject($user))

            {

                $this->addError('username', 'This user name has already been added to the project');

            }

            else

            {

                $this->project->associateUserToProject($user);

                $this->project->associateUserToProject($this->role, $user->id);

                $auth = Yii::app()->authManager;

                $bizRule='return isset($params["project"]) && $params["project"]->isUserInRole("'.$this->role.'");';

                $auth->assign($this->role,$user->id, $bizRule);

            }

        }

    }

}

?>

This is the line 168: [b]$command->bindValue(":userId", $user->id, PDO::PARAM_INT);[/b] But I believe is correct.


where I went wrong? Why?



in your verify() function inside ProjectUserForm class you are calling associateUserToProject twice, the second call should be associateUserToRole… like this:




public function verify($attribute,$params)

{

...

    else  

    {

      $this->project->associateUserToProject($user);  

      $this->project->associateUserToRole($this->role, $user->id); 

...



Thanks for help, I need to pay atention. I read this code several times and not noticed it

Thanks friend