Yii: search for HAS_MANY

I have the following tables:

user (id, cv_personal_data_id),
cv_personal_data (id, firstname, surname, gender, address, ...),
cv_laboral_exp (id, user_id, position, seniority,... ),
cv_study (id, user_id, name, institution, average, ...),
cv_language (id, user_id, language_name, writing_level, ...)

In my User model, I defined the following relationships:

    public function relations()
{
    return array(
        'cvLaboralExps' => array(self::HAS_MANY, 'CvLaboralExp', 'user_id'),
        'cvLanguages' => array(self::HAS_MANY, 'CvLanguage', 'user_id'),
        'cvStudies' => array(self::HAS_MANY, 'CvStudy', 'user_id'),
        'cvPersonalData' => array(self::BELONGS_TO, 'CvPersonalData', 'cv_personal_data_id'),
}

The problem is this: logged in as a company, I need to display a CGridView, which lists all the users and can be found in any of the fields of related tables, such as "position" (from cv_laboral_exp), "language_name" '(from cv_languages) etc. I cannot find a solution to search for fields that come from the HAS_MANY relationship. I tried adding the expression 'with' to the criteria $ in the search () method of the User class to try to find the user's position in the work experience, but to no avail:

                $criteria->compare('cvLaboralExps.position',$this->cvLaboralExps,true);
                $criteria->with = array('cvLaboralExps'=>array('select'=>'cvLaboralExps.position','together'=>true)); 

, , CV . , - , /.

+3
1

- , User. , , ( compare()): $this->cvLaboralExps, cvLaboralExps - , , , $value . , $value, :

, .

, - compare() .

...
class User extends CActiveRecord{
    // declare the variables that we need
    public $cvLaboralExpsLocal,$cvLanguages,$cvStudies;

    // now put these variables in rules array, so that massive assignment can be done, i.e. safe rule
    public function rules(){
         return array(
              // other rules ...
              array('attributesxyz, cvLaboralExpsLocal, cvLanguages, cvStudies', 'safe', 'on'=>'search')
         );
    }

    // other functions

    // the search can be kept just the way you have in the question but with the new variables
    public function search(){
          // other statements
          $criteria->compare('cvLaboralExps.position',$this->cvLaboralExpsLocal,true);
          $criteria->with = array('cvLaboralExps'=>array('select'=>'cvLaboralExps.position','together'=>true));
    }
}

: 1. _search.php, .
2. has_many, , .

+5

All Articles