Search code examples
phpmongodbdoctrine-odm

How to get following values


I am doing projec using mongodb with ODM tool. so here I try to use find query. this is my code

$dm    = $this->getServiceLocator()->get('doctrine.documentmanager.odm_default');
$user = $dm->getRepository('Application\Document\User')->findOneBy(array('username' => 'admin'));

so now $user variable contain some values related to find query. so when I use var_dump($user) it show following result

object(Application\Document\User)#253 (4) { ["id":"Application\Document\User":private]=> string(24) "5103d0aca00b2a3205000001" ["username":"Application\Document\User":private]=> string(5) "admin" ["email":"Application\Document\User":private]=> string(15) "[email protected]" ["password":"Application\Document\User":private]=> string(3) "abc" }

so please tell me how to get username value from the $user object`.


Solution

  • You should have a protected username member in your User entity. Once you have that you should add your getters and setters for that member and use them to fetch given value.

    $user->getUsername();
    

    You could also optionally run app/console doctrine:generate:entities to let Doctrine generate getters and setter functions for all of your entities.