Issue With Cdbcriteria

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! :)

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? :)

Hi my friend

  1. did you define the $staff_name in your class ? (not only in the native class)

  2. 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 :)

  1. Yes , i have define it as a public attribute

in my model (principalcomments):-




 public $staff_name;



  1. 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);

Hi

Have a look at http://www.yiiframework.com/extension/relatedsearchbehavior/ - it’s a big timesaver when working with relations and ‘search()’.

@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!

@le_top -

Hi,

thanks a lot for the suggestion i will look at it… :)

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.

@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! :)