V
V
Vyacheslav2017-10-06 20:16:05
Yii
Vyacheslav, 2017-10-06 20:16:05

Is it possible to somehow check the index in relation yii2?

Hey!
I have a query that has links: ->with(['one', 'two'])
and the code for them:

public function getOne() {
  return $this->hasOne(One::classname(), ['id' => 'one']);
}
public function getTwo() {
  return $this->hasOne(Two::classname(), ['id' => 'two']);
}

But the problem is that indexes one and two are not in every line of the query result,
i.e. the initial data array could be like this:
[
  0 => [
    'id' => 1,
    'one' => 10,
  ]
  
  1 => [
    'id' => 2,
    'two' => 12,
  ]
  
  2 => [
    'id' => 3,
    'two' => 25,
  ]
]

You can of course insert empty indexes long before this selection, but in the future there may be more such indexes
. Is there a way to check whether this index exists in a relation?
Something like
if(!isset($this->one))
  return false;

return $this->hasOne(One::classname(), ['id' => 'one']);

Has anyone run into something like this?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2017-10-07
@slo_nik

Goodnight.
Maybe this will suit you

return $this->hasOne(One::classname(), ['id' => 'one'])->andWhere([*****]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question