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

how to fetch a simple model data?

when i use Article::find()/findfirst(), and use var_dump() or volt dump output

i get the data like this:

   ["_sharedInstances":protected]=>
        array(8) {
          ["router"]=>
          object(Phalcon\Mvc\Router)#30 (16) {
            ["_dependencyInjector":protected]=>
            *RECURSION*
            ["_namespace":protected]=>
            NULL
            ["_module":protected]=>
            string(7) "backend"
            ["_controller":protected]=>
            string(8) "articles"
            ["_action":protected]=>
            string(4) "edit"
            ["_params":protected]=>
            array(1) {
              [0]=>
              string(5) "13079"
            }
            ["_routes":protected]=>
            array(4) {
              [0]=>
              object(Phalcon\Mvc\Router\Route)#31 (7) {
                ["_pattern":protected]=>
                string(29) "#^/([a-zA-Z0-9\_]+)[/]{0,1}$#"
                ["_compiledPattern":protected]=>
                string(29) "#^/([a-zA-Z0-9\_]+)[/]{0,1}$#"
                ["_paths":protected]=>
                array(1) {
                  ["controller"]=>
                  int(1)
                }
                ["_methods":protected]=>
                NULL
                ["_converters":protected]=>
                NULL
                ["_id":protected]=>
                int(0)
                ["_name":protected]=>
                NULL
              }
              [1]=>
              object(Phalcon\Mvc\Router\Route)#32 (7) {
                ["_pattern":protected]=>
                string(46) "#^/([a-zA-Z0-9\_]+)/([a-zA-Z0-9\.\_]+)(/.*)*$#"
                ["_compiledPattern":protected]=>
                string(46) "#^/([a-zA-Z0-9\_]+)/([a-zA-Z0-9\.\_]+)(/.*)*$#"
                ["_paths":protected]=>
                array(3) {
                  ["controller"]=>
                  int(1)
                  ["action"]=>
                  int(2)
                  ["params"]=>
                  int(3)
                }
                ["_methods":protected]=>
                NULL
                ["_converters":protected]=>
                NULL
                ["_id":protected]=>
                int(1)
                ["_name":protected]=>
                NULL
              }
              [2]=>
              object(Phalcon\Mvc\Router\Route)#33 (7) {
                ["_pattern":protected]=>
                string(20) "/:controller/:action"
                ["_compiledPattern":protected]=>
                string(42) "#^/([a-zA-Z0-9\_\-]+)/([a-zA-Z0-9\_\-]+)$#"
                ["_paths":protected]=>
                array(3) {
                  ["module"]=>
                  string(8) "frontend"
                  ["controller"]=>
                  int(1)
                  ["action"]=>
                  int(2)
                }
                ["_methods":protected]=>
                NULL
                ["_converters":protected]=>
                NULL
                ["_id":protected]=>
                int(2)
                ["_name":protected]=>
                NULL
              }
              [3]=>
              object(Phalcon\Mvc\Router\Route)#34 (7) {
                ["_pattern":protected]=>
                string(34) "/admin/:controller/:action/:params"
                ["_compiledPattern":protected]=>
                string(54) "#^/admin/([a-zA-Z0-9\_\-]+)/([a-zA-Z0-9\_\-]+)(/.*)*$#"
                ["_paths":protected]=>
                array(4) {
                  ["module"]=>
                  string(7) "backend"
                  ["controller"]=>
                  int(1)
                  ["action"]=>
                  int(2)
                  ["params"]=>
                  int(3)
                }
                ["_methods":protected]=>
                NULL
                ["_converters":protected]=>
                NULL
                ["_id":protected]=>
                int(3)
                ["_name":protected]=>
                NULL
              }
            }
            ["_matchedRoute":protected]=>
            object(Phalcon\Mvc\Router\Route)#34 (7) {
              ["_pattern":protected]=>
              string(34) "/admin/:controller/:action/:params"
              ["_compiledPattern":protected]=>
              string(54) "#^/admin/([a-zA-Z0-9\_\-]+)/([a-zA-Z0-9\_\-]+)(/.*)*$#"
              ["_paths":protected]=>
              array(4) {
                ["module"]=>
                string(7) "backend"
                ["controller"]=>
                int(1)
                ["action"]=>
                int(2)
                ["params"]=>
                int(3)
              }
              ["_methods":protected]=>
              NULL
              ["_converters":protected]=>
              NULL
              ["_id":protected]=>
              int(3)
              ["_name":protected]=>
              NULL
            }
            ["_matches":protected]=>
            array(4) {
              [0]=>
              string(26) "/admin/articles/edit/13079"
              [1]=>
              string(8) "articles"
              [2]=>
              string(4) "edit"
              [3]=>
              string(6) "/13079"
            }
            ["_wasMatched":protected]=>
            bool(true)
            ["_defaultNamespace":protected]=>
            NULL
            ["_defaultModule":protected]=>
            string(8) "frontend"
            ["_defaultController":protected]=>
            NULL
            ["_defaultAction":protected]=>
            NULL
            ["_defaultParams":protected]=>
            array(0) {
            }
            ["_removeExtraSlashes":protected]=>
            bool(false)
          }
          ["view"]=>
          object(Phalcon\Mvc\View)#50 (25) {
            ["_dependencyInjector":protected]=>
            *RECURSION*
            ["_eventsManager":protected]=>
            NULL
            ["_options":protected]=>
            NULL
            ["_basePath":protected]=>
            string(0) ""
            ["_content":protected]=>
            NULL
            ["_renderLevel":protected]=>
            int(5)
            ["_disabledLevels":protected]=>
            NULL
            ["_viewParams":protected]=>
            array(1) {
              ["baseUri"]=>
              string(7) "/admin/"
            }
            ["_layout":protected]=>
            NULL
            ["_layoutsDir":protected]=>
            string(0) ""
            ["_partialsDir":protected]=>
            string(0) ""
            ["_viewsDir":protected]=>
            string(22) "../apps/backend/views/"
            ["_templatesBefore":protected]=>
            NULL
            ["_templatesAfter":protected]=>
            NULL
            ["_engines":protected]=>
            bool(false)
            ["_registeredEngines":protected]=>
            array(1) {
              [".phtml"]=>
              string(4) "volt"
            }
            ["_mainView":protected]=>
            string(5) "index"
            ["_controllerName":protected]=>
            NULL
            ["_actionName":protected]=>
            NULL
            ["_params":protected]=>
            NULL
            ["_pickView":protected]=>
            NULL
            ["_cache":protected]=>
            NULL
            ["_cacheLevel":protected]=>
            int(0)
            ["_activeRenderPath":protected]=>
            NULL
            ["_disabled":protected]=>
            bool(false)
          }
          ["dispatcher"]=>
          object(Phalcon\Mvc\Dispatcher)#51 (15) {
            ["_dependencyInjector":protected]=>
            *RECURSION*
            ["_eventsManager":protected]=>
            object(Phalcon\Events\Manager)#52 (3) {
              ["_events":protected]=>
              array(1) {
                ["dispatch"]=>
                object(SplPriorityQueue)#54 (3) {
                  ["flags":"SplPriorityQueue":private]=>
                  int(1)
                  ["isCorrupted":"SplPriorityQueue":private]=>
                  bool(false)
                  ["heap":"SplPriorityQueue":private]=>
                  array(1) {
                    [0]=>
                    array(2) {
                      ["data"]=>
                      object(Acl)#53 (3) {
                        ["_module":protected]=>
                        string(7) "backend"
                        ["_dependencyInjector":protected]=>
                        NULL
                        ["_eventsManager":protected]=>
                        NULL
                      }
                      ["priority"]=>
                      int(100)
                    }
                  }
                }
              }

when i use toArray convert to array, i get the simple array data, but i don't like use array data

so , how can i get a simple model data, because it's useful when i debug the programe



98.9k
Accepted
answer

Dumping a model will always return any internal structure attached to the model. If you want to see an object instead of an array you could cast the array right after dumping it:

$robot = Robots::findFirst();
var_dump((object) $robot->dump());
var_dump((object) $robot->toArray());


17.8k

ok,thanks