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

ODM in 0.8.0, can't put conditions in findFirst() or find()

$organization = Organizations::findFirst() return one object. But when I trying to have conditions inside like

$organization = Organizations::findFirst(array(array('tax_id' => $taxId)));

It just can't find anything.

Query in mongo console could get correct object:

db.organizations.findOne({tax_id: "$taxId"})


98.9k

Do you have in the database a record where the value is "$taxId" <- dollar + taxId ?



2.1k

$taxId is a number.



98.9k

Is your model in a namespace?

Can you try to perform the query just using the Mongo extension?

$mongo = new Mongo();
$collection = $mongo->selectCollection("organizations");
$cursor = $collection->find(array('tax_id' => $taxId));
$cursor->rewind();
var_dump($cursor->current());


2.1k
        $mongo = new Mongo();
        $collection = $mongo->selectCollection("organizations");
        $cursor = $collection->find(array('tax_id' => '24436573'));
        $cursor->rewind();
        var_dump($cursor->current());

Warning: Mongo::selectCollection() expects exactly 2 parameters, 1 given in xxx.php

MongoDB Support Version 1.2.9 (found in phpinfo) mongod --version >> db version v2.0.4



2.1k

The model created following the guide in ODM documentation.



98.9k

I missed the database:

<?php

$mongo = new Mongo();
$db = $mongo->selectDb("your-db-name");
$collection = $db->selectCollection("organizations");
$cursor = $collection->find(array('tax_id' => '24436573'));
$cursor->rewind();
var_dump($cursor->current());


2.1k

It could get correct record using the code.



2.1k

Found the working syntaxes:

Organizations::findFirst(array(
            'conditions' => array('tax_id' => "{$taxId}")
        ));
Organizations::find(array(
            'conditions' => array('tax_id' => "{$taxId}"),
            'limit' => 2
        ));