Yii:HAS_MANY搜索 [英] Yii: HAS_MANY search
问题描述
我有下表:
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 have defined the following relations:
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()方法中向$ criteria添加'with'语句,以尝试搜索用户辛苦工作的位置,但没有成功:
The problem is: logged in as a Company, I need to display a CGridView listing all the users and being able to search by any of the fields of the related tables, such as 'position' (from cv_laboral_exp), 'language_name' (from cv_languages), and so on. I can't seem to find a solution to search fields that come from a HAS_MANY relation. I tried adding in the search() method of the User class the 'with' statement to the $criteria, in an attempt to search the position of a user laboral experience, but without success:
$criteria->compare('cvLaboralExps.position',$this->cvLaboralExps,true);
$criteria->with = array('cvLaboralExps'=>array('select'=>'cvLaboralExps.position','together'=>true));
如您所见,有很多关系构成用户的简历.如果有人可以帮助我解决这个问题,我将不胜感激,即使这意味着更改数据库/模型结构也是如此.
As you see there are a lot of relationships that form the CV of a user. I'd be very grateful if anyone could help me solve this, even if it implies changing the database/model structure.
推荐答案
实际上,您需要为该模型声明一个成员变量,这里是 User .您正在做什么的问题是这个(在compare()
中):$this->cvLaboralExps
,这里的cvLaboralExps
只是类的关系,而不是可以存储值的变量,因此比较的$value
是空的.在比较文档中检查此行,解释第二个参数$value
:
You'll actually need to declare a member variable for the model in question, here it is User. The problem with what you are doing is this(in compare()
) : $this->cvLaboralExps
, here cvLaboralExps
is just a relation of the class, and not a variable, that can store a value, hence the compare's $value
is empty. Check this line, explaining the second parameter $value
, in the compare documentation:
如果字符串或数组为空,则不会修改现有的搜索条件.
If the string or the array is empty, the existing search condition will not be modified.
可以通过为模型声明一个成员变量,并修改compare()
调用以使用新变量来避免这种情况.
This can be avoided by declaring a member variable for the model, and modifying the compare()
calls to use the new variables.
...
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,因此您必须注意最终用户如何输入值.
Notes: 1. Remember to change the _search.php form to accept the new variables.
2. Since this is has_many, you'll have to take care of how the end user is entering the values.
这篇关于Yii:HAS_MANY搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!