S
S
SalaDIN_912020-04-15 08:53:43
Yii
SalaDIN_91, 2020-04-15 08:53:43

Active record empty relatedRecords. Why?

There are 2 models, ProduceInStore and produceHasSupplier. Which are interconnected.
At ProduceInStore

public function getProduceHasSupplier()
    {
        return $this->hasOne(ProduceHasSupplier::className(), ['code' => 'code']);
    }

In ProduceHasSupplier
public function getProduceHasSupplier()
    {
        return $this->hasOne(ProduceHasSupplier::className(), ['code' => 'code']);
    }

What I do: I create an array of AR(ProduceInStore) objects according to the following conditions.
$two = (new ProduceInStore())::find()->select([
            'lbk_Produce_has_Supplier.idProduce',
            'lbk_Produce_inStore.code',
            'lbk_Produce_inStore.idSupplier',
            'SUM(lbk_Produce_inStore.cnt) as remains'])
            ->innerJoinWith('produceHasSupplier')
            ->where('lbk_Produce_inStore.cnt > 0')
            ->andWhere(['lbk_Produce_has_Supplier.idSupplier' => $access_groups['supplier']])
            ->andWhere(['lbk_Produce_inStore.idSupplier' => $access_groups['supplier']])
            ->andWhere(['lbk_Produce_has_Supplier.idProduce' => [1461127, 820793]])
            ->groupBy('lbk_Produce_has_Supplier.code')
            ->orderBy('lbk_Produce_has_Supplier.idProduce')
            ->all();

What I expect to get: An array with two ProduceInStore in each of which will be a produceHasSupplier object with an attribute (idProduce).
What I get: Object 1461127 contains attributes. Object 820793 contains an empty object produceHasSupplier

PS Tried to specify only 820793 in the -'lbk_Produce_has_Supplier.idProduce' condition. In this case, produceHasSupplier contains attributes.
I thought that the problem was in my record, but as I wrote above, if you specify one specific record, then everything works as it should.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question