Back reference in active record

I’m not sure of the best way of wording this, so I’ve struggled to find any information online.

As far as I can tell, when related active records are loaded, either eagerly or lazily, the loaded active records don’t store a reference to the active record that loaded them. I’m trying to determine if there’s a clean way to achieve this.

In my scenario, I have:

  • an AR class Report with a HAS_MANY relation ‘reportSheets’.

  • an AR class ReportSheet with a BELONGS_TO relation ‘report’.

I call $report->reportSheets, which performs a query and stores references to the ReportSheet objects in $report.

How can I automatically store a reference to $report in each of these ReportSheet objects? Currently, calling $this->report from a ReportSheet method causes the Report object to be lazily loaded from the database.

Note that this is not a performance issue. I need to use the original Report object as I have already loaded and configured other related classes.

I could not think of the use of this idea yet, here is what I would do

in model ReportSheet

class ReportSheet extends CActiveRecords{


    public $myparent = null;



so when load ReportSheet by Report via relation, do this:

$reportsheet = $model->reportSheets;

//assign parent's model to child's parent property

$reportsheet->myparent = $model;

to refer back to parent:

echo $reportsheet->myparent->title;


  1. to make it generic, you may want to add this property ($myparent) to the parent class of active record;

  2. avoid using $parent, it might be conflict with php or yii or misleading (though code still would work)