We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Phalcon\Mvc\Model::find() return empty, but exists records.

use version 1.3.4., Amazon RDS. This is the model class

class Products extends \Phalcon\Mvc\Model {
  public $id;
  public $name;
  public function initialize(){
    $this->setSource('products');
  }
}

table status:

mysql> show columns from products;
+-------+--------------+------+-----+---------+----------------+
| Field | Type         | Null | Key | Default | Extra          |
+-------+--------------+------+-----+---------+----------------+
| id    | int(11)      | NO   | MUL | NULL    | auto_increment |
| name  | varchar(255) | YES  |     | NULL    |                |
+-------+--------------+------+-----+---------+----------------+

mysql> select * from `products`;
+----+--------+
| id | name   |
+----+--------+
|  1 | value1 | 
|  2 | value2 |
+----+--------+

If use Model

$products = Products::find();
var_export($products); // Bad  : 0

$product = Products::findFirst();
var_export($product); // Bad  : false

If use PDOConnection

$connection = $this->di->get('db');
$result = $connection->query('SELECT * from `products`');
$products = $result->fetchAll();

var_export(count($products)); // Good  : 2

Is there something wrong? db setting is here.

$di->set('db', function(){
  return new \Phalcon\Db\Adapter\Pdo\Mysql(array(
    "host" => "%AMAZON RDS HOST NAME%",
    "username" => "%username%",
    "password" => "%password%",
    "dbname" => "% DB NAME %"
  ));
});


58.4k
edited Nov '14

Hey Because var_export() gets structured information about the given variable. It is similar to var_dump() with one exception: the returned representation is valid PHP code. You try it

    $product = Products::findFirst();
var_export($product->id); // Bad  : false
edited Nov '14

Result is

$product = Products::findFirst();
var_dump($product); // bool(false)
var_export($product->id); // NULL

and for logging

$di->set('db', function(){
    $adapter = new \Phalcon\Db\Adapter\Pdo\Mysql(array(
        "host" => "%AMAZON RDS HOST NAME%",
        "username" => "%username%",
        "password" => "%password%",
        "dbname" => "% DB NAME %"
    ));
    $eventsManager = new \Phalcon\Events\Manager();
    // listen to all the database events
    $eventsManager->attach(
        'db',
        function ($event, $connection) {
            if ($event->getType() == 'beforeQuery') {
                print_r($connection->getSQLStatement());
            }
        });
    $adapter->setEventsManager($eventsManager);
    return $adapter;
});

Products::findFirst(); log is

SELECT IF(COUNT(*)>0, 1 , 0) FROM `INFORMATION_SCHEMA`.`TABLES` WHERE `TABLE_NAME`='products'
DESCRIBE `products`
SELECT `products`.`id`, `products`.`name` FROM `products` LIMIT 1

seems good. but response means empty...

edited May '15

I have he same problem ! Did you solve it ? PDOConnection return the result but model/pql or modelsManager dont ;/



120

Do you happent to use pdo_mysql.so? instead of mysqlnd.so. If you so try to install mysqlnd.so with like that the following command:

$ sudo yum install php-mysql

You can echo phpinfo() to check Configure Command.

if not include --without-pdo-mysql, maybe need to re-mark php with pdo extension.

edited Oct '18

Hi there,

i have the same problem and really no clue, how to solve it.

I'm running newest phalcon on php7.2-fpm.

I opened an discussion here since i did not found any solution.

Did anyone find a solution to this?

--Holger