PHP-ORM延迟加载/标识映射实现问题
我有一个简单的ORM实现,由加载和持久化实体的数据映射器组成。每个映射器在内部管理从数据库读取的所有实体的标识映射,以便同一实体只加载到内存中一次 我目前正在使用代理类为相关实体实现延迟加载,该代理类仅在访问实体上的属性时加载相关数据。我的问题是代理类不是实体本身,仅在间接加载实体(通过关系)时使用。因此,任何===将实际实体与加载相同实体的代理进行比较的检查都将返回false。我的目标是让实体和客户端代码都不知道代理对象 代理类类似于:PHP-ORM延迟加载/标识映射实现问题,php,orm,lazy-loading,identity,Php,Orm,Lazy Loading,Identity,我有一个简单的ORM实现,由加载和持久化实体的数据映射器组成。每个映射器在内部管理从数据库读取的所有实体的标识映射,以便同一实体只加载到内存中一次 我目前正在使用代理类为相关实体实现延迟加载,该代理类仅在访问实体上的属性时加载相关数据。我的问题是代理类不是实体本身,仅在间接加载实体(通过关系)时使用。因此,任何===将实际实体与加载相同实体的代理进行比较的检查都将返回false。我的目标是让实体和客户端代码都不知道代理对象 代理类类似于: class EntityProxy { prot
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;
}
}
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;
}
}
地图绘制者看起来像:
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;
}
}
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;
}
}
如果我加载一个具有相关“AddressEntity”的“PersonEntity”记录,然后通过“AddressEntityMapper”直接加载相同的“AddressEntity”记录,并比较两个对象,它们将不相同(因为其中一个是委托的代理)。有没有办法覆盖PHP的内置对象比较?在实体和/或客户端代码中不引入代理感知代码的情况下,有没有更好的处理方法的建议
另外,我知道采用现有的和已建立的ORM对我有利,但有许多问题阻止我这样做。通常的方法是创建一个equals方法,就像在Java中一样。PHP不允许重写==或===并且我从来没有找到一种重写比较器的方法,但我以前就错了,如果我在这方面错了,那就太酷了 不确定您的问题,但我认为应该使用接口进行比较,而不是
=
或==
您应该使用if($User instanceof UserInterface)
看:事实上,没有办法覆盖
=
或===
(以及其他运算符的加载),因此方法是唯一的方法。如果它成为可能,那将是一件好事。