Parameter from ActiveQuery to ::instantiate

I there a possibility to send a parameter fron an ActiveQuery to the instantiate method of the class being created?

I need to send a ref to the parent object.

Post a bit of code.

I want SlaveModel to get a parameter at its instantiate step, so in process of search in DB. Something like that.




class MasterModel extends ActiveRecord

{

    public function getSlaves()

    {

        // Here sendParam is to send parameter master to the SlaveModels

        return $this->hasMany(SlaveModel::className(),['id','slave_id'])->sendParam(['master'=>$this]);

    }

}


class SlaveModel extends ActiveRecord

{

    public $master;


    public static instantiate($row,$param)

    {

        $model = parent::instantiate($row,$param);

        // Here param is to be got from the ActiveQuery class

        $model->load($param);

        return $model;

    }

}




What kind of parameter have you to get from ActiveQuery class? Why can’t you take from ‘$params’ variable, since ActiveQuery class is generated from that?

You mean select ‘param’, table.* ?

I cannot send reference to an object through SQL.

It is not clear your intent.

I just need the SlaveModel instances to be initialized with some external parameter being read from DB. This instances must know the object which creates them.

What for? Because I cannot set direct relation between MasterModel and SlaveModel. Because SlaveModel can be instantiated from MaterModel1 class, MasterModel2 class, etc.

This is especially required in modeuls. The modules are very general and have to have no fixed relations with the main body of the application.

Right now I use static fields in SlaveModel, but I am not satisfied with this trick because it can cause a collision if SlaveModel is instantiated from different classes.

Normally, people do like this:


$x = new SlaveModel(['parent' => $this])

But in the case of DB read like SlaveModel::find() this won’t work.

I have understood.

You could set these fields after find, for example:




public function findSlaves($parent, $extraParamaters)

{

     $arr = SlaveModels::find()->...

     for($k=0;$k<count($arr);$k++) {

            $arr[$k]->parent = $parent;

            $arr[$k]->attr_to_change = $extraParameters['attr_to_change'];

            ...

     }

     return $arr;

} 



It is impossible in my case because the query is performed in a GridView with the DataProvider.