Vik*_*ika 3 php sql search has-many yii
我有以下表格:
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, ...)
Run Code Online (Sandbox Code Playgroud)
在我的用户模型中,我定义了以下关系:
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'),
}
Run Code Online (Sandbox Code Playgroud)
问题是:作为公司登录,我需要显示列出所有用户的CGridView,并能够通过相关表的任何字段进行搜索,例如'position'(来自cv_laboral_exp),'language_name'(来自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));
Run Code Online (Sandbox Code Playgroud)
如您所见,有很多关系构成了用户的简历.如果有人能帮助我解决这个问题,我将非常感激,即使它意味着改变数据库/模型结构.
你实际上需要为有问题的模型声明一个成员变量,这里是User.你正在做的问题是这个(in 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));
}
}
Run Code Online (Sandbox Code Playgroud)
注意:1.记得更改_search.php表单以接受新变量.
2.由于这是has_many,您必须注意最终用户输入值的方式.
| 归档时间: |
|
| 查看次数: |
2384 次 |
| 最近记录: |