I have 2 objects in a one-to-one association. The first, Person , is stored in the MySQL database and processed by Doctrine. The second, AdUserRecord , describes an ActiveDirectory user entry. It is read-only. You do not need to know about Person . In addition, AdUserRecord properties AdUserRecord never be stored in MySQL db for privacy reasons.
An AdUserRecord is retrieved using the AdSearcher service, which can search by samaccountname or objectGUID . Whenever a search is successful, the service checks to see if there is a matching Person record and creates it if it is not. It works great.
My problem arises when I start with a Person object. Basically, I do not need to contact Person AdUserRecord , so I would prefer not to query Active Directory if this is not required. This means that I think that Person::getAdrecord() should have access to the AdSearcher service. Something like that:
public function getAdrecord(){ if($this->adrecord) return $this->adrecord; $searcher = ???; //get AdSearcher service somehow $record = $search->getRecordByUserGuid($this->ad_guid); if(!$record) throw new \Exception('this person no longer exists'); $this->adrecord = $record; return $this->adrecord; }
I read the Symfony docs pretty hard, but I'm still at a dead end.
Questions
- How do I get a service in essence? Should it be entered through the constructor or only where necessary in the getter? If this only happens in getter, should I enter it or is there a way to import it?
- adds an entity service to a canonical way of dealing with these types of situations? Would it be preferable to create an object manager for
AdUserRecord s? - What interfaces do I need to implement if I need to create an entity manager?
Person class
namespace ACRD\DefaultBundle\Entity; use Symfony\Component\Validator\Constraints as Assert; use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; use ACRD\DefaultBundle\Entity\AdUserRecord; class Person { protected $id; protected $ad_guid; protected $adrecord;
source share