PHP-ORM Lazy Load / Identity Map implementation issue

I have a bare-bone ORM implementation consisting of data cards that load and save entities. Each cartographer internally manages an identification card for all objects read from the database, so that the same object is loaded only once in memory.

I am currently doing lazy loading for related objects using a proxy class that only downloads related data when objects access the accessed object. My problem is that the proxy class is not the entity itself and is used only if the object is loaded indirectly (through relationships). Thus, any === checking comparison of the actual object with the proxy server that downloads the same object will return false. My goal is not to limit the entities and client code to proxy objects.

The proxy class looks something like this:

class EntityProxy { protected $_entity; protected $_loader; public function __construct(EntityProxyLoader $loader) { $this->_loader = $loader; } protected function _load() { if (null === $this->_entity) { $this->_entity = $this->_loader->load(); unset($this->_loader); } } public function __get($name) { $this->_load(); return $this->_entity->$name; } public function __set($name, $value) { $this->_load(); $this->_entity->$name = $value; } } 

And the cartographers look something like this:

 class PersonEntityMapper { // Find by primary key public function find($id) { if ($this->inIdentityMap($id) { return $this->loadFromIdentityMap($id); } $data = ...; // gets the data $person = new Person($data); // Proxy placeholder for a related entity. Assume the loader is // supplied the information it needs in order to load the related // entity. $person->Address = new EntityProxy(new EntityProxyLoader(...)); $this->addToIdentityMap($id, $person); return $person; } } class AddressEntityMapper { // Find by primary key public function find($id) { ... $address = new AddressEntity($data); $address->Person = new EntityProxy(new EntityProxyLoader(...)); $this->addToIdentityMap($id, $address); return $address; } } 

If I load the PersonEntity record that has the associated AddressEntity, load the same AddressEntity record directly through AddressEntityMapper and compare the two objects, they will not be the same (since one of them is a proxy that delegates) . Is there a way to override PHP built into object comparison? Any suggestions on a better way to handle this without entering a proxy code in the entity and / or client code?

In addition, I know that it would be useful for me to accept an existing and installed ORM, but there are various problems that prevent me from doing this.

+4
source share
2 answers

The usual method is to create an equals method, as in Java. PHP does not allow you to override == or ===, and I never found a way to override comparators, but I used to make a mistake, and it would be great if I made a mistake.

+2
source

Not sure about your problem, but I think the comparison should be done using interfaces, and instead of == or === you should use if($User instanceof UserInterface)

Take a look: http://www.davegardner.me.uk/blog/tag/lazy-load/

0
source

All Articles