Authenticated Can See Only His Tasks

Hello.

I have problem.

I can view tasks only autenticated user.

i have function:


public function defaultScope() {

        if (Yii::app()->user->checkAccess('admin')) {

            return array();

        } else {

        $alias = Task::model()->getTableAlias(false,false);

//            var_dump($tabAlias); die;

            return array(

                'condition' => $alias . ".user_id = '" . Yii::app()->user->id . "'",

            );

        }

    }

I have two tables. TASK and USER_ID.

My relation in task:


 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(

            'messages' => array(self::HAS_MANY, 'Message', 'task_id'),

            'project' => array(self::BELONGS_TO, 'Project', 'project_id'),

            'user' => array(self::BELONGS_TO, 'User', 'user_id'),

            'author' => array(self::BELONGS_TO, 'User', 'author_id'),

                //    'file' => array(self::BELONGS_TO, 'Files', 'path'),

        );

    }

My relation in Project:


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(

            'tasks' => array(self::HAS_MANY, 'Task', 'project_id'),

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

            'author' => array(self::BELONGS_TO, 'User', 'author_id'),

        );

    }

I render tasks in project.

and i need something that: task_id = Authenticated user_id.

But my error is:


CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 't.user_id' in 'where clause'. The SQL statement executed was: SELECT COUNT(DISTINCT `t`.`id`) FROM `project` `t` LEFT OUTER JOIN `user_project` `users_users` ON (`t`.`id`=`users_users`.`project_id`) LEFT OUTER JOIN `user` `users` ON (`users`.`id`=`users_users`.`user_id`) LEFT OUTER JOIN `user` `author` ON (`t`.`author_id`=`author`.`id`) LEFT OUTER JOIN `task` `tasks` ON (`tasks`.`project_id`=`t`.`id`) WHERE (t.user_id = '49') 

The error is saying that you don’t have a column named user_id in your project table.

Your MANY_MANY users relation in Project shows that the user_id field is actually in your user_project table.

Maybe defining this default scope in the User model instead would help? I’m not sure how that will interact with the MANY_MANY relation, but it’s worth testing.