Search code examples
phpdoctrine-ormsilexentitymanager

Accessing Entity Manager from Model Class in Silex


I'm using Silex for simple site. I use Doctrine DBAL and that project https://github.com/dflydev/dflydev-doctrine-orm-service-provider as ORM and Entity Manager. I know how access that manger from controller but I wonder how to do that from Model class. For example we have

class UserModel {

public function getPhones() {

????????

}

}

I don't know how to access EntityManager from Model class to find User Phones.

Thanks for advice


Solution

  • You should not.

    Instead you should set up your entities so your user has a relation to its phones, something like this:

    /** @Entity */
    class User
    {
        /**
         * @OneToMany(targetEntity="Phone")
         */
        private $phones;
        // ...
    
        public function __construct() {
            $this->phones = new ArrayCollection();
        }
    }
    

    and the neccessary getters/setters. After this, you won't need the entity manager in your entity.