Yii Framework Forum: Validation Role - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Validation Role Rate Topic: -----

#1 User is offline   slevin10 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 20-February 14

Posted 17 April 2014 - 03:58 PM

I create this validation function
 public function workerUniq($attributes,$params)//
        {
        isset($_POST['Activity']['activity_code']) ? $activity_code=$_POST['Activity']['activity_code']&$flag=TRUE:'';
        $timetable_code = $_POST['Activity']['timetable_code'];
        $workers_code = $_POST['Activity']['workerToActivity'];
        $criteria = new CDbCriteria();
        $criteria->with = array('workers');
        $criteria->together = true;
        $criteria->select=array('activity_code,timetable_code,worker_code');
        $criteria->condition = 'timetable_code=:timetable_code AND worker_code=:worker_code';
      
                  foreach ($workers_code as $v1){
                        $criteria->params = array(':timetable_code'=>$timetable_code,':worker_code'=>'$v1->worker_code');
                        if(Activity::model()->exists($criteria)){
                            if($flag){
                            $findactivity=Activity::model()->find($criteria);
                            if(!$findactivity->activity_code==$activity_code){
                                $this->addError('workerToActivity','worker alredy besy');
                            }
                        }  else {
                            $this->addError('workerToActivity','worker alredy besy');
                        }
                        
        }
        }
        }

the DB is:
three tables: "activity","worker" and "worker to activity"
and i want to make sure that a worker is register only to one activiy at any "timecode"
the relations between worker to activity meny-meny

and it retren SQL error, what am i doing wrong?
it is ok to put the citeria parm in the foreach?
0

#2 User is offline   Bjorn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 156
  • Joined: 15-April 13
  • Location:The Netherlands

Posted 17 April 2014 - 04:05 PM

What is the SQL error?

For me prefixing column names in relation conditions helps preventing a lot of trouble. Could be it. But give us the SQL error, then we can help you out maybe.
0

#3 User is offline   slevin10 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 20-February 14

Posted 17 April 2014 - 04:45 PM

View PostBjorn, on 17 April 2014 - 04:05 PM, said:

What is the SQL error?

For me prefixing column names in relation conditions helps preventing a lot of trouble. Could be it. But give us the SQL error, then we can help you out maybe.

thenks
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'worker_code' in where clause is ambiguous. The SQL statement executed was: SELECT COUNT(DISTINCT `t`.`activity_code`) FROM `tbl_activity` `t` LEFT OUTER JOIN `tbl_worker_to_activity` `workers_workers` ON (`t`.`activity_code`=`workers_workers`.`activity_code`) LEFT OUTER JOIN `tbl_worker` `workers` ON (`workers`.`worker_code`=`workers_workers`.`worker_code`) WHERE (timetable_code=:timetable_code AND worker_code=:worker_code)
0

#4 User is offline   Bjorn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 156
  • Joined: 15-April 13
  • Location:The Netherlands

Posted 17 April 2014 - 05:02 PM

Because the column 'worker_code' is in more tables, SQL don't know which one you mean with:

 $criteria->condition = 'timetable_code=:timetable_code AND worker_code=:worker_code';


So you have to use the table alias. Something like:

 $criteria->condition = 'timetable_code=:timetable_code AND workers.worker_code=:worker_code';


etc.

Besides that, I wonder if you could do it with the default unique validator somehow: http://www.yiiframew...UniqueValidator to check on the "worker to activity" class.

    public function rules(){
        array('worker_code', 'unique', 'className'=> 'WorkerToActivity'),
    }

0

#5 User is offline   slevin10 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 20-February 14

Posted 17 April 2014 - 05:13 PM

I'll try that thenks!
0

#6 User is offline   slevin10 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 20-February 14

Posted 18 April 2014 - 02:32 AM

View PostBjorn, on 17 April 2014 - 05:02 PM, said:

Because the column 'worker_code' is in more tables, SQL don't know which one you mean with:

 $criteria->condition = 'timetable_code=:timetable_code AND worker_code=:worker_code';


So you have to use the table alias. Something like:

 $criteria->condition = 'timetable_code=:timetable_code AND workers.worker_code=:worker_code';


etc.

Besides that, I wonder if you could do it with the default unique validator somehow: http://www.yiiframew...UniqueValidator to check on the "worker to activity" class.

    public function rules(){
        array('worker_code', 'unique', 'className'=> 'WorkerToActivity'),
    }


so it's not return error but also not prevent doble worker situation

and the unique rule not working either :(
0

#7 User is offline   Bjorn 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 156
  • Joined: 15-April 13
  • Location:The Netherlands

Posted 18 April 2014 - 04:59 AM

View Postslevin10, on 18 April 2014 - 02:32 AM, said:

so it's not return error but also not prevent doble worker situation

and the unique rule not working either :(


You can have more unique rules if you want. And also you can use a condition in the unique rule.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users