Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

model::query() couldn't get a empty result

I want to check a record is exists, so I use query in the model

if ($record = static::query()->where('email = "'.$this->email.'"')->andWhere("id != " . $this->id)->execute()) {

}

here is the sql log

[Mon, 04 Sep 17 08:09:23 +0000][INFO] SELECT `users`.`id`, `users`.`uid`, `users`.`username`, `users`.`email`, `users`.`emailValid`, `users`.`phone`, `users`.`phoneValid`, `users`.`password`, `users`.`aid`, `users`.`status`, `users`.`created_at`, `users`.`updated_at` FROM `users` WHERE (`users`.`email` = '[email protected]') AND (`users`.`id` <> 7)

print_r($record)

Phalcon\Mvc\Model\Resultset\Simple Object
(
    [_result:protected] => 
    [_cache:protected] => 
    [_isFresh:protected] => 1
    [_pointer:protected] => 0
    [_count:protected] => 0
    [_activeRow:protected] => 
    [_rows:protected] => Array
        (
        )

    [_row:protected] => 
    [_errorMessages:protected] => 
    [_hydrateMode:protected] => 0
    [_model:protected] => app\models\Users Object
        (
            [aid] => 
            [id] => 
            [uid] => 
            [username] => 
            [email] => 
            [emailValid] => 
            [phone] => 
            [phoneValid] => 
            [password] => 
            [status] => 
            [_dependencyInjector:protected] => Phalcon\Di\FactoryDefault Object

why $record not empty, but return a model with null of field

Phalcon DevTools (3.2.0)
Environment:
  OS: Darwin MacBook-Pro.local 15.6.0 Darwin Kernel Version 15.6.0: Sun Jun  4 21:43:07 PDT 2017; root:xnu-3248.70.3~1/RELEASE_X86_64 x86_64
  PHP Version: 7.0.22
  PHP SAPI: cli
  PHP Bin: /usr/local/Cellar/php70/7.0.22_14/bin/php
  PHP Extension Dir: /usr/local/Cellar/php70/7.0.22_14/lib/php/extensions/no-debug-non-zts-20151012
  PHP Bin Dir: /usr/local/Cellar/php70/7.0.22_14/bin
  Loaded PHP config: /usr/local/etc/php/7.0/php.ini
Versions:
  Phalcon DevTools Version: 3.2.0
  Phalcon Version: 3.2.0
  AdminLTE Version: 2.3.6


9.0k
Accepted
answer

Hi @lvshutao it's ok query() is the OOP version of ::find()

You can use limit(1) to improve performance and check if record exists with $result->count()

Good luck

edited Sep '17

execute returns resultset. If you want just single record then you need to use getSingleResult()

Also use binding!

->where('email = :email:', ['email' => $this->email])->andWhere("id != :id:", ['id' => $this->id])

>execute returns resultset. If you want just single record then you need to use getSingleResult() > >Also use binding! > >``` >->where('email = :email:', ['email' => $this->email])->andWhere("id != :id:", ['id' => $this->id]) >```

thank you for you answer. I use

Users::query()->where('id=3')->execute()->getFirst()

instead. I don't like use getSingleResult, because it has to write more codes.

$this->modelsManager->createBuilder()->from(Users::class)->where('id=2')->getQuery()->getSingleResult();

>Hi @lvshutao it's ok query() is the OOP version of ::find() > >You can use limit(1) to improve performance and check if record exists with $result->count() > >Good luck

thank you for your answer. I read the document again. I make a mistake in Model and Resultset before.