Check if relation is loaded in Yii

I am trying to do something complicated with an attitude and avoid double loading:

  • I have an active record object, and each of them relates to some subjectthrough a relationship objectSubject.

  • The type subject(with respect to object) is defined in objectSubjectwith a different relation.

  • Each objecthas zero or one relation of subjecteach type.

I have relationships established in the model objectas:

'objectSubjects'=>array(self::HAS_MANY, 'ObjectSubject', 'object_id'),

And the model is objectSubjectlike:

'type'=>array(self::BELONGS_TO, 'Type', 'type_id'),
'subject'=>array(self::BELONGS_TO, 'Subject', 'subject_id'),

I would like to add a function to objectget subjectfrom objectusing type..

I can do:

public function fetchSubject($key_string){
  $object_subject=$this->objectSubjects(array(
       'with'=>'subject'
       'scopes'=>array('typed'=>$key_string) /* Inner Join to type */
  ));
  return $object_subjects?$object_subjects[0]->subject:null;
}

But this will lead to a DB request, even if object_subjectwith them typeand subjecteagerly loads into object.

, , subject, . , ?

- $this->isLoaded('objectSubjects')?

+4
1

?

hasRelated(string $name)

AR docs.

+4

All Articles