k123456
(Kassa 923)
June 5, 2013, 1:56pm
1
Hi, hope you are well,
Im still kinder new to yii and is still learning the framework.
Im trying to modify my search so that I can retrieve results when searched via an attribute in a related table.
I understand that i have to do relational querying here, so i used "with" in my search function as below:-
In my model
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('commentId',$this->commentId);
$criteria->compare('timetableId',$this->timetableId);
$criteria->compare('datecommented',$this->datecommented,true);
$criteria->compare('comment',$this->comment,true);
if($this->staff_name)//to disply the academic name which satisfy the id
{
$criteria->together = true;
$criteria->with = array('teachertimetable');
$criteria->compare('teachertimetable.academicstaffId',$this->staff_name,true);
}
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
Then in my view i called the search function, i introduced a field to my search view as shown below:-
<div class="row">
<?php echo $form->label($model,'staff_name'); ?>
<?php echo $form->textField($model,'staff_name'); ?>
</div>
But, im getting a error in a popup saying :-
Error 500 - relation teachertimetable not defined in active records class principalcomments.
How can i define my related relation in my current AR?
Any help is greatly appreciated.
Thanks a lot for your time
Cheerz!
ragua
(Brice)
June 5, 2013, 2:16pm
2
Hi,
in your model you must declare like my example:
public function relations()
{
return array(
'civility' => array(self::BELONGS_TO, 'Civilities', 'civility_id'),
...
}
Usually, you generate this part of your project by using gii after you create your database.
k123456
(Kassa 923)
June 5, 2013, 3:02pm
3
ragua:
Hi,
in your model you must declare like my example:
public function relations()
{
return array(
'civility' => array(self::BELONGS_TO, 'Civilities', 'civility_id'),
...
}
Usually, you generate this part of your project by using gii after you create your database.
@ragua -
Thanks for the reply,
But i have already defined my relations as:-
return array(
'timetable' => array(self::BELONGS_TO, 'Teachertimetable', 'timetableId'),
);
Sorry, i should have mentioned that in my question.
Cheerz!
Thanks again!
Any help is appreciated?
konapaz
(Konapaz)
June 5, 2013, 3:27pm
4
@ragua -
Thanks for the reply,
But i have already defined my relations as:-
return array(
'timetable' => array(self::BELONGS_TO, 'Teachertimetable', 'timetableId'),
);
Sorry, i should have mentioned that in my question.
Cheerz!
Thanks again!
Any help is appreciated?
Hi my friend
did you define the $staff_name in your class ? (not only in the native class)
you said teachertimetable but you refererenced timetable? did you mean on the native class? (you have to add relation in the second class) something like that
return array(
'teachertimetable' => array(self::HAS_MANY, 'timetable', 'the_Id'),
);
Please post the relations both of two classes
k123456
(Kassa 923)
June 5, 2013, 3:54pm
5
KonApaz:
Hi my friend
did you define the $staff_name in your class ? (not only in the native class)
you said teachertimetable but you refererenced timetable? did you mean on the native class? (you have to add relation in the second class) something like that
return array(
'teachertimetable' => array(self::HAS_MANY, 'timetable', 'the_Id'),
);
Please post the relations both of two classes
@KonApaz - Thanks a lot for your time
Yes , i have define it as a public attribute
in my model (principalcomments):-
public $staff_name;
I thought the referencing was done by :
$criteria->compare('teachertimetable.academicstaffId',$this->staff_name,true);
Am i missing something here?
and my relations would be:-
Principalcomments model
public function relations()
{
return array(
'timetable' => array(self::BELONGS_TO, 'Teachertimetable', 'timetableId'),
);
}
Teachertime table model
public function relations()
{
return array(
'principalcomments' => array(self::HAS_MANY, 'Principalcomments', 'timetableId'),
'class' => array(self::BELONGS_TO, 'Schoolclasses', 'classId'),
'academicstaff' => array(self::BELONGS_TO, 'Academicstaff', 'academicstaffId'),
);
}
Please let me know if i am doing anything wrong…
Thanks a lot for your time
Cheerz!
konapaz
(Konapaz)
June 5, 2013, 4:51pm
6
@KonApaz - Thanks a lot for your time
Yes , i have define it as a public attribute
in my model (principalcomments):-
public $staff_name;
I thought the referencing was done by :
$criteria->compare('teachertimetable.academicstaffId',$this->staff_name,true);
Am i missing something here?
and my relations would be:-
Principalcomments model
public function relations()
{
return array(
'timetable' => array(self::BELONGS_TO, 'Teachertimetable', 'timetableId'),
);
}
Teachertime table model
public function relations()
{
return array(
'principalcomments' => array(self::HAS_MANY, 'Principalcomments', 'timetableId'),
'class' => array(self::BELONGS_TO, 'Schoolclasses', 'classId'),
'academicstaff' => array(self::BELONGS_TO, 'Academicstaff', 'academicstaffId'),
);
}
Please let me know if i am doing anything wrong…
Thanks a lot for your time
Cheerz!
replace
$criteria->with = array('teachertimetable');
$criteria->compare('teachertimetable.academicstaffId',$this->staff_name,true);
with
$criteria->with = array('timetable');
$criteria->compare('timetable',$this->staff_name,true);
le_top
(Ext Yiiframework Com)
June 5, 2013, 5:46pm
7
Hi
Have a look at http://www.yiiframework.com/extension/relatedsearchbehavior/ - it’s a big timesaver when working with relations and ‘search()’.
k123456
(Kassa 923)
June 6, 2013, 5:00am
8
KonApaz:
replace
$criteria->with = array('teachertimetable');
$criteria->compare('teachertimetable.academicstaffId',$this->staff_name,true);
with
$criteria->with = array('timetable');
$criteria->compare('timetable',$this->staff_name,true);
@KonApaz -
Thanks a lot for your time,
I changed my principalcomment model as:-
if($this->staff_name)
{
$criteria->together = true;
$criteria->with = array('timetable');
$criteria->compare('timetable',$this->staff_name,true);
}
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
But still I get the same error. I attached a screen shot of the error.
If you can, please help me…
Thanks again…
Cheerz!
k123456
(Kassa 923)
June 6, 2013, 5:02am
9
@le_top -
Hi,
thanks a lot for the suggestion i will look at it…
Cheerz!
konapaz
(Konapaz)
June 6, 2013, 6:33am
10
@KonApaz -
Thanks a lot for your time,
I changed my principalcomment model as:-
if($this->staff_name)
{
$criteria->together = true;
$criteria->with = array('timetable');
$criteria->compare('timetable',$this->staff_name,true);
}
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
But still I get the same error. I attached a screen shot of the error.
If you can, please help me…
Thanks again…
Cheerz!
Sorry my wrong.
You have wrote
$criteria->compare('teachertimetable.academicstaffId',$this->staff_name,true);
I posted it
$criteria->with = array('timetable');
$criteria->compare('timetable',$this->staff_name,true);
but the timetable is the relation not a field! so
$criteria->with = array('timetable');
$criteria->compare('timetable.academicstaffId',$this->staff_name,true);
or check this
$criteria->compare('academicstaffId',$this->staff_name,true);
but the academicstaffId seems to be an id NOT a name! (staff_name = academicstaffId ??) are you sure for that?
If you have problem again please post your schema of ER database.
k123456
(Kassa 923)
June 6, 2013, 7:40am
11
KonApaz:
Sorry my wrong.
You have wrote
$criteria->compare('teachertimetable.academicstaffId',$this->staff_name,true);
I posted it
$criteria->with = array('timetable');
$criteria->compare('timetable',$this->staff_name,true);
but the timetable is the relation not a field! so
$criteria->with = array('timetable');
$criteria->compare('timetable.academicstaffId',$this->staff_name,true);
or check this
$criteria->compare('academicstaffId',$this->staff_name,true);
but the academicstaffId seems to be an id NOT a name! (staff_name = academicstaffId ??) are you sure for that?
If you have problem again please post your schema of ER database.
@KonApaz - Yup that was the error!! I have not defined the relations properly, once i changed my code (model) to:-
if($this->staff_name)
{
$criteria->together = true;
$criteria->with = array('timetable');
$criteria->compare('timetable.academicstaffId',$this->staff_name,true);
}
Yes the name is a bit ambiguous but i am selecting the Id.
Anyways,
It worked perfectly…
Thank you very much for the help, really appreciate it!
Cheerz!