A
A
Albert Tobacco2015-08-03 18:46:51
Yii
Albert Tobacco, 2015-08-03 18:46:51

Why doesn't an unsaved model see relays in Yii2?

I have one pre-ordered parameter (by which the relay is connected), the connection is not by id.
That is, I create a new instance, and set a certain parameter in init

$productEntity = ProductEntity::find()->where(['alias' => $this->productName()])->one();

And this is how my relay looks like
public function getParamList()
    {
        return $this->hasMany(ProductAttribute::className(), ['product_entity_id' => 'id'])
            ->where(['product_entity_id' => $this->product_entity_id]);
    }

Alas, without saving the model, I cannot access the relays. What can you think of?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Bay, 2015-08-03
@kawabanga

when you create a model, your model is virtual and is not tied to the base in any way.
There are a couple of solutions that come to mind:
$mode->save(); /*code*/ $model->delete();
and the second way, make a getter

protected $_rel;

  public function getRel() {
    if ($this->$_rel) return $this->$rel; 

else {
$model = Model::findAllByAttributes();
if (!$model)  return false;

$this->_rel = $model
return $this->_rel;


}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question