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

Incubators Mongodb returns too much information

I have just got incubators mongodb to work with phalcon but when doing a simple find() it is returning a whole pile of other stuff I do not need.

Is there any way to reduce this to only the results from the table?

the result is huge so here is a sample of what I get returned...

[_dependencyInjector:protected] => Phalcon\Di\FactoryDefault Object
                (
                    [_services:protected] => Array
                        (
                            [router] => Phalcon\Di\Service Object
                                (
                                    [_name:protected] => router
                                    [_definition:protected] => Phalcon\Mvc\Router
                                    [_shared:protected] => 1
                                    [_resolved:protected] => 1
                                    [_sharedInstance:protected] => Phalcon\Mvc\Router Object
                                        (
                                            [_dependencyInjector:protected] => Phalcon\Di\FactoryDefault Object
 *RECURSION*
                                            [_eventsManager:protected] => 
                                            [_uriSource:protected] => 
                                            [_namespace:protected] => 
                                            [_module:protected] => 
                                            [_controller:protected] => user
                                            [_action:protected] => 
                                            [_params:protected] => Array
                                                (
                                                )

                                            [_routes:protected] => Array
                                                (
                                                    [0] => Phalcon\Mvc\Router\Route Object
                                                        (
                                                            [_pattern:protected] => #^/([\w0-9\_\-]+)[/]{0,1}$#u
                                                            [_compiledPattern:protected] => #^/([\w0-9\_\-]+)[/]{0,1}$#u
                                                            [_paths:protected] => Array
                                                                (
                                                                    [controller] => 1
                                                                )

                                                            [_methods:protected] => 

.....

           [_skipped:protected] => 
            [address] => Array
                (
                    [street] => 2 Avenue
                    [zipcode] => 10075
                    [building] => 1480
                    [coord] => Array
                        (
                            [0] => -73.9557413
                            [1] => 40.7720266
                        )

                )

            [borough] => Manhattan
            [cuisine] => Italian
            [grades] => Array
                (
                    [0] => Array
                        (
                            [date] => MongoDB\BSON\UTCDateTime Object
                                (
                                    [milliseconds] => 1412121600000
                                )

                            [grade] => A
                            [score] => 11
                        )

                    [1] => Array
                        (
                            [date] => MongoDB\BSON\UTCDateTime Object
                                (
                                    [milliseconds] => 1389830400000
                                )

                            [grade] => B
                            [score] => 17
                        )

                )

            [name] => Vella
            [restaurant_id] => 41704620
        )

)

Any help would be gratefully received.

Use hydratation mode HYDRATE_ARRAYS or toArray() then you'll only got the query result data
https://olddocs.phalcon.io/en/3.0.2/reference/models-advanced.html



2.5k

Perfect, many thanks!



2.5k
edited Oct '17

I have tried for a few hours today and cant seem to get this to work. I know it should.... I'm stuck. As I said before I am using the mongodb adapter from Incubator.

it seems as though I have the wrong namespace for the resultset as I keep getting

Fatal error: Uncaught Error: Call to a member function toArray() on array in...

I have tried

<?php 

use Phalcon\Mvc\Model\Resultset;

class ProductsController extends ControllerBase
{
    public function indexAction()
    {
        $products = Products::find();
        print_r($products->toArray());

        // not working

        ...
        //and 
        $products->setHydrateMode(Resultset::HYDRATE_ARRAY);
        print_r($products);

        // also not working

I presume Incubator would need a different namespce for the Resultset I just cant find it.

and this way ? :

use Phalcon\Mvc\Model\Resultset;

class ProductsController extends ControllerBase
{
    public function indexAction()
    {
        $products = Products::find([
            "hydration" => Resultset::HYDRATE_ARRAYS,
        ]);

else what the type of your result ?



2.5k
edited Oct '17

and this way ? :

use Phalcon\Mvc\Model\Resultset;

class ProductsController extends ControllerBase
{
   public function indexAction()
   {
       $products = Products::find([
          "hydration" => Resultset::HYDRATE_ARRAYS,
       ]);

else what the type of your result ?

That way does return a result although it is still returns all the unwanted information.

initially I thought it was an object but on closer look the result type it is returning is an array. This would explain why toArray() is not working HYDRATE_ARRAYS is not outputing the desired result.



2.5k
edited Oct '17

Ok, I have done a little experiment on this hydrate and toArray() issue.


use Phalcon\Mvc\Model\Resultset;

class ProductsController extends ControllerBase
{
    public function indexAction()
    {
        $products = Products::find();
        Print_r($products);
    }
}

outputs and result of array like so...


array
(
    [0] => Products Object
        (
            [name] => 
            [_id] => MongoDB\BSON\ObjectID Object
                (
                    [oid] => 59d930ce1d41c806e074dcb2
                )

            [_dependencyInjector:protected] => Phalcon\Di\FactoryDefault Object
                (
                    [_services:protected] => Array
                        (
                            [router] => Phalcon\Di\Service Object
                                (
                                    [_name:protected] => router
                                    [_definition:protected] => Phalcon\Mvc\Router
                                    [_shared:protected] => 1
                                    [_resolved:protected] => 1
                                    [_sharedInstance:protected] => Phalcon\Mvc\Router Object
                                        (
                                            [_dependencyInjector:protected] => Phalcon\Di\FactoryDefault Object
 *RECURSION*
                                            [_eventsManager:protected] => 
                                            [_uriSource:protected] => 
                                            [_namespace:protected] => 
                                            [_module:protected] => 
                                            [_controller:protected] => products
                                            [_action:protected] => 
                                            [_params:protected] => Array
                                                (
                                                )

                                            [_routes:protected] => Array
                                                (
                                                    [0] => Phalcon\Mvc\Router\Route Object
                                                        (
                                                            [_pattern:protected] => #^/([\w0-9\_\-]+)[/]{0,1}$#u
                                                            [_compiledPattern:protected] => #^/([\w0-9\_\-]+)[/]{0,1}$#u
                                                            [_paths:protected] => Array
                                                                (
                                                                    [controller] => 1
                                                                )

                                                            [_methods:protected] => 
                                                            [_hostname:protected] => 
                                                            [_converters:protected] => 
                                                            [_id:protected] => 0
                                                            [_name:protected] => 
                                                            [_beforeMatch:protected] => 
                                                            [_match:protected] => 
                                                            [_group:protected] => 
                                                        )

                                                    [1] => Phalcon\Mvc\Router\Route Object
                                                        (
                                                            [_pattern:protected] => #^/([\w0-9\_\-]+)/([\w0-9\.\_]+)(/.*)*$#u
                                                            [_compiledPattern:protected] => #^/([\w0-9\_\-]+)/([\w0-9\.\_]+)(/.*)*$#u
                                                            [_paths:protected] => Array
                                                                (
                                                                    [controller] => 1
                                                                    [action] => 2
                                                                    [params] => 3
                                                                )

                                                            [_methods:protected] => 
                                                            [_hostname:protected] => 
                                                            [_converters:protected] => 
                                                            [_id:protected] => 1
                                                            [_name:protected] => 
                                                            [_beforeMatch:protected] => 
                                                            [_match:protected] => 
                                                            [_group:protected] => 
                                                        )

                                                    [2] => Phalcon\Mvc\Router\Route Object
                                                        (
                                                            [_pattern:protected] => /:controller/:action/:params
                                                            [_compiledPattern:protected] => #^/([\w0-9\_\-]+)/([\w0-9\_\-]+)(/.*)*$#u
                                                            [_paths:protected] => Array
                                                                (
                                                                    [controller] => 1
                                                                    [action] => 2
                                                                    [params] => 3
                                                                )

                                                            [_methods:protected] => 
                                                            [_hostname:protected] => 
                                                            [_converters:protected] => 
                                                            [_id:protected] => 2
                                                            [_name:protected] => 
                                                            [_beforeMatch:protected] => 
                                                            [_match:protected] => 
                                                            [_group:protected] => 
                                                        )

                                                )

                                            [_matchedRoute:protected] => Phalcon\Mvc\Router\Route Object
                                                (
                                                    [_pattern:protected] => #^/([\w0-9\_\-]+)[/]{0,1}$#u
                                                    [_compiledPattern:protected] => #^/([\w0-9\_\-]+)[/]{0,1}$#u
                                                    [_paths:protected] => Array
                                                        (
                                                            [controller] => 1
                                                        )

                                                    [_methods:protected] => 
                                                    [_hostname:protected] => 
                                                    [_converters:protected] => 
                                                    [_id:protected] => 0
                                                    [_name:protected] => 
                                                    [_beforeMatch:protected] => 
                                                    [_match:protected] => 
                                                    [_group:protected] => 
                                                )

                                            [_matches:protected] => Array
                                                (
                                                    [0] => /products/
                                                    [1] => products
                                                )

                                            [_wasMatched:protected] => 1
                                            [_defaultNamespace:protected] => 
                                            [_defaultModule:protected] => 
                                            [_defaultController:protected] => 
                                            [_defaultAction:protected] => 
                                            [_defaultParams:protected] => Array
                                                (
                                                )

                                            [_removeExtraSlashes:protected] => 
                                            [_notFoundPaths:protected] => 
                                        )

                                )

                            [dispatcher] => Phalcon\Di\Service Object
                                (
                                    [_name:protected] => dispatcher
                                    [_definition:protected] => Phalcon\Mvc\Dispatcher
                                    [_shared:protected] => 1
                                    [_resolved:protected] => 1
                                    [_sharedInstance:protected] => Phalcon\Mvc\Dispatcher Object
                                        (
                                            [_dependencyInjector:protected] => Phalcon\Di\FactoryDefault Object
 *RECURSION*
                                            [_eventsManager:protected] => 
                                            [_activeHandler:protected] => ProductsController Object
                                                (
                                                    [_dependencyInjector:protected] => Phalcon\Di\FactoryDefault Object
 *RECURSION*
                                                    [_eventsManager:protected] => 
                                                )

                                            [_finished:protected] => 1
                                            [_forwarded:protected] => 
                                            [_moduleName:protected] => 
                                            [_namespaceName:protected] => 
                                            [_handlerName:protected] => products
                                            [_actionName:protected] => index
                                            [_params:protected] => Array
                                                (
                                                )

                                            [_returnedValue:protected] => 
                                            [_lastHandler:protected] => ProductsController Object
                                                (
                                                    [_dependencyInjector:protected] => Phalcon\Di\FactoryDefault Object
 *RECURSION*
                                                    [_eventsManager:protected] => 
                                                )

                                            [_defaultNamespace:protected] => 
                                            [_actionSuffix:protected] => Action
                                            [_previousNamespaceName:protected] => 
                                            [_previousHandlerName:protected] => 
                                            [_previousActionName:protected] => 
                                            [_modelBinding:protected] => 
                                            [_modelBinder:protected] => 
                                            [_handlerSuffix:protected] => Controller
                                            [_defaultHandler:protected] => index
                                            [_defaultAction:protected] => index
                                        )

                                )

                            [url] => Phalcon\Di\Service Object
                                (
                                    [_name:protected] => url
                                    [_definition:protected] => Closure Object
                                        (
                                        )

                                    [_shared:protected] => 1
                                    [_resolved:protected] => 
                                    [_sharedInstance:protected] => 
                                )

                            [modelsManager] => Phalcon\Di\Service Object
                                (
                                    [_name:protected] => modelsManager
                                    [_definition:protected] => Phalcon\Mvc\Model\Manager
                                    [_shared:protected] => 1
                                    [_resolved:protected] => 
                                    [_sharedInstance:protected] => 
                                )

                            [modelsMetadata] => Phalcon\Di\Service Object
                                (
                                    [_name:protected] => modelsMetadata
                                    [_definition:protected] => Closure Object
                                        (
                                        )

                                    [_shared:protected] => 1
                                    [_resolved:protected] => 
                                    [_sharedInstance:protected] => 
                                )

 ........

                                    [readConcern] => MongoDB\Driver\ReadConcern Object
                                        (
                                        )

                                    [readPreference] => MongoDB\Driver\ReadPreference Object
                                        (
                                            [mode] => primary
                                        )

                                    [typeMap] => Array
                                        (
                                            [array] => Phalcon\Db\Adapter\MongoDB\Model\BSONArray
                                            [document] => Phalcon\Db\Adapter\MongoDB\Model\BSONDocument
                                            [root] => Phalcon\Db\Adapter\MongoDB\Model\BSONDocument
                                        )

                                    [writeConcern] => MongoDB\Driver\WriteConcern Object
                                        (
                                        )

                                )

                            [_errorMessages:protected] => Array
                                (
                                )

                            [_skipped:protected] => 
                        )

                    [_eventsManager:protected] => 
                    [_customEventsManager:protected] => 
                    [_connectionServices:protected] => 
                    [_implicitObjectsIds:protected] => 
                    [_behaviors:protected] => 
                    [_serviceName:protected] => mongo
                )

            [_source:protected] => 
            [_operationMade:protected] => 0
            [_dirtyState:protected] => 1
            [_connection:protected] => 
            [_errorMessages:protected] => Array
                (
                )

            [_skipped:protected] => 
            [title] => Aberdeen Hooks
            [variations] => Array
                (
                    [Size] => 1/0
                    [Qty] => 10
                )

            [sku] => aber-1-0-10
        )
...

where as the same query but using findFirst() results in an object same as above but without the containing array. i.e the beginning becomes


Products Object
        (
            [name] => 
            [_id] => MongoDB\BSON\ObjectID Object
                (
                    [oid] => 59d930ce1d41c806e074dcb2

Because the Find() method is retuning an array by default, I cannot use toArray() or HYDRATE_ARRAYS.

However I can Use toArray() for the findFirst method but unfortunatly it does not change the resulting output. Whent I do Print_r($products->toArray()); the output is identiacal to print_r($products);

Is there a setting I have missed? or is this a bug? or simply, it is default behaviour for phalcon or incubator and I have to put up with it? :(

Sorry I don't have experience with mongodb, can you try serialize/unserialize instead of toArray ?