3

次のテーブルがあります。

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, ...)

私の User モデルでは、次の関係を定義しました。

    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'),
}

問題は次のとおりです。会社としてログインし、すべてのユーザーを一覧表示する CGridView を表示し、関連するテーブルの任意のフィールド (「位置」(cv_laboral_exp から)、「言語名」(からcv_languages) などです。HAS_MANY リレーションからのフィールドを検索する解決策が見つからないようです。ユーザーの労働経験の位置を検索しようとして、User クラスの search() メソッドに 'with' ステートメントを $criteria に追加しようとしましたが、成功しませんでした。

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

ご覧のとおり、ユーザーの CV を形成する多くの関係があります。データベース/モデル構造の変更を意味する場合でも、誰かがこれを解決するのを手伝ってくれたらとても感謝しています。

4

1 に答える 1

5

実際には、問題のモデルのメンバー変数を宣言する必要があります。ここではUserです。あなたがしていることの問題は this(in compare()) :$this->cvLaboralExpsです。これcvLaboralExpsは値を格納できるクラスの関係であり、変数ではないため、比較$valueは空です。比較ドキュメント$valueで、2 番目のパラメーターを説明する次の行を確認してください。

文字列または配列が空の場合、既存の検索条件は変更されません。

これは、モデルのメンバー変数を宣言し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 であるため、エンド ユーザーが値を入力する方法に注意する必要があります。

于 2012-04-23T20:50:37.107 に答える