0
votes

I use phalcon framework and mongo db. I show you 2 different ways. First return not right result, but second way return right result(he use default php extension).

Why i get different results? And how parse it?

Sample in phalcon(way 1):

    $documents = new AdminModel();
    $cursor = $documents->find(array(
        'login' => 'hello'
    ));

    echo "<pre>";
    foreach($cursor as $doc){
        foreach($doc as $prop){
            var_dump($prop);
        }
    }
    echo "</pre>";

How looks AdminModel(way 1):

    class AdminModel extends \Phalcon\Mvc\Collection{

        public function getSource()
        {
            return "accounts";
        }
    }

Result(way 1):

    object(MongoId)#70 (1) {
      ["$id"]=>
      string(24) "540822d55d34c4087a320064"
    }

    array(3) {
      ["login"]=>
      string(3) "one"
      ["password"]=>
      string(60) "$2y$10$RB2PvyRWxD7kdZcYpb8BGO6E/2ftbAEwnJ/28TBWUaDmX.iesPqY2"
      ["role"]=>
      string(4) "user"
    }

And when i try use default extension(way 2):

    $mongo = new \MongoClient();
    $db = $mongo->weather;
    $collection = $db->accounts;

    $cursor = $collection->find();

    echo "<pre>";
    foreach($cursor as $doc){
            var_dump($doc);echo "1<br/>";
    }
    echo "</pre>";

Result(way 2):

    array(2) {
      ["_id"]=>
      object(MongoId)#72 (1) {
        ["$id"]=>
        string(24) "540822d55d34c4087a320064"
      }
      ["user"]=>
      array(3) {
        ["login"]=>
        string(3) "one"
        ["password"]=>
        string(60) "$2y$10$RB2PvyRWxD7kdZcYpb8BGO6E/2ftbAEwnJ/28TBWUaDmX.iesPqY2"
        ["role"]=>
        string(4) "user"
      }
    }

UPDATE:

        foreach($cursor as $model){
            echo $model->user['login']."<br/>";
            echo $model->user['password']."<br/>";
            echo $model->user['role']."<br/>";
        }

two
$2y$10$RB2PvyRWxD7kdZcYpb8BGO6E/2ftbAEwnJ/28TBWUaDmX.iesPqY2
admin
three
$2y$10$RB2PvyRWxD7kdZcYpb8BGO6E/2ftbAEwnJ/28TBWUaDmX.iesPqY2
guest

Notice: Undefined property: Weather\Model\AdminModel::$user in /Users/rd/sites/app/controllers/Admin/IndexController.php on line 19

Notice: Undefined property: Weather\Model\AdminModel::$user in /Users/rd/sites/app/controllers/Admin/IndexController.php on line 20

Notice: Undefined property: Weather\Model\AdminModel::$user in /Users/rd/sites/app/controllers/Admin/IndexController.php on line 21

I told, what find return 1 object and 1 array. What we see, $model try access to:

    object(MongoId)#70 (1) {
      ["$id"]=>
      string(24) "540822d55d34c4087a320064"
    }

And then we get Notice.

1

1 Answers

0
votes

What do you mean why??? You use two completely different things hence you get two completely different results. Mongo client returns your JSON documents as is. Phalcon uses Mongo client (maybe not the same one) to retrieve those documents and maps them to your models for a more convenient use. So, AdminModel::find internally calls Collection::find and then stores the result in own format, which you see when dumping the var.

See ODM (Object-Document Mapper) documentation, it talks more about the implementation and how to use it.

Edit:

You don't really need to parse it, because the model supposed to do this for you. To access your data you can do the following.

$documents = new AdminModel();
$cursor = $documents->find(array(
    'login' => 'hello'
));

foreach($cursor as $model){
    var_dump($model->user);
    echo $model->user['login'];
    echo $model->user['password'];
    echo $model->user['role'];
}

$model = $documents->findFirst(array(
    'login' => 'hello'
));
var_dump($model->user);
echo $model->user['login'];
echo $model->user['password'];
echo $model->user['role'];

PS: you also have a nested loop in first example, otherwise (I assume) it would show an object with id and user properties.