Jae Kun Choi
Jae Kun Choi

Reputation: 2029

Yii accessing relational data via find

I have hasMany defined in model for this find function below.

$model=Investor::model()->find('fname=? AND lname=?', array($fname, $lname)

Is there a way we can access the relational data in the view?

Upvotes: 0

Views: 587

Answers (1)

Sam Dark
Sam Dark

Reputation: 5291

Yes. There are two ways:

  • Lazy loading.
  • Eager loading.

To access via lazy loading (additional SQL queries) you shouldn't add anything to your find and in the view you'll just use:

<?php foreach($model->relationName as $relatedModel):?>
  // something
<?php endforeach ?>

Eager loading will get everything in a single SQL query so it's more efficient in most cases. View will stay the same. The part that's different is Investor::model()->with('relationName')->find(….

Upvotes: 2

Related Questions