H
H
happ2017-03-02 08:52:20
Yii
happ, 2017-03-02 08:52:20

How to check if a record is found in Yii2?

I make a selection of a non-existent record, the result is true because into the array is not empty, it contains other fields.
Next, you will be prompted to delete the entry.

$followRecord = Follow::find([
            'userId' => 111,
            'onUserId' => 777
])->one();

 if($followRecord)
              //Удаление

Answer the question

In order to leave comments, you need to log in

3 answer(s)
C
Crash, 2017-03-02
@happ

if ( !empty($followRecord) ) {
    // Удаление
}

A
Anton, 2017-03-02
@karminski

If you have a "true result", then it means that the one() function found the same record. One() returns either an array of the found entry's attributes, or NULL (`null` will be returned
if the query results in nothing). So check your database query.
And yes, I usually check like this:

$followRecord = Follow::find(['userId' => 111, 'onUserId' => 777])->one();
if ($followRecord instanceof Follow) {
// запись найдена - удаляем ???
}

B
Boris Yakushev, 2017-03-02
@za4me

!== null in if.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question