S
S
Sergey Pugovkin2017-04-22 13:14:30
Yii
Sergey Pugovkin, 2017-04-22 13:14:30

Lack of synchronization of data from the database when changing - what to do?

In the Domain model, there is such a relationship with users:

public function getUser() {
        return $this->hasOne(User::className(), ['id' => 'userId']);
    }

In action:
$domains = Domain::find()->where(['userId' => 4])->all();
        foreach ($domains as $i => $domain) {
            if ($i == 0) {
                $domain->user->updateCounters(['balance' => -10]);
                echo 'UserId4 Balance: ' . $domain->user->balance . "\n";
            } elseif ($i == 1) {
                echo 'UserId4 Balance: ' . $domain->user->balance . "\n";
                exit;
            }
        }

Expectation:
UserId4 Balance: -10
UserId4 Balance: -10

But in fact:
UserId4 Balance: -10
UserId4 Balance: 0

Although the database shows that immediately after updateCounters it has changed. But nothing has changed in the other $domain->user, although they point to the same user (in my example, with ID equal to 4). Why and what is the best way to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Pugovkin, 2017-04-22
@Driver86

www.yiiframework.com/doc-2.0/yii-db-baseactivereco...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question