本文介绍了可接受的实体逻辑?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个实体,Post和Post \Version。我已经设置好了,因此版本控制在Post实体中自动处理,所以开发人员不必手动使用Post\Version。它不使用EntityManager,只是一点反思...这是可以吗? <?php
public function setContent($ content)
{
$ this-> _setVersionValue('content',$ content);
}
私有函数_setVersionValue($ property,$ value)
{
//获取反映属性
$ version = clone $ this-> getActiveVersion();
$ refl = new \ReflectionProperty($ version,$ property);
$ refl-> setAccessible(true);
//更新值
$ version-> setCreatedBy($ this-> getCurrentUser());
$ refl-> setValue($ version,$ value);
//清除ID
$ reflProp = new \ReflectionProperty($ version,'id');
$ reflProp-> setAccessible(true);
$ reflProp-> setValue($ version,null);
//设置为新版本
$ this-> setActiveVersion($ version);
}
邮政只存储对最新版本的引用。版本具有对他们属于的帖子的反向引用。
解决方案
这是完全正确的。当然,您应该始终找到自己的工作流程,以了解如何混合类和实例。
I have two entities, Post and Post\Version. I've set it up so the versioning is automatically handled within the Post entity, so the developer doesn't have to use Post\Version manually. It doesn't use the EntityManager, just a bit of reflection... is this ok?
<?php
public function setContent($content)
{
$this->_setVersionValue('content', $content);
}
private function _setVersionValue($property, $value)
{
// get reflection property
$version = clone $this->getActiveVersion();
$refl = new \ReflectionProperty($version, $property);
$refl->setAccessible(true);
// update value
$version->setCreatedBy($this->getCurrentUser());
$refl->setValue($version, $value);
// clear ID
$reflProp = new \ReflectionProperty($version, 'id');
$reflProp->setAccessible(true);
$reflProp->setValue($version, null);
// set to new version
$this->setActiveVersion($version);
}
The Post only stores a reference to the latest version. Version's have a back-reference to the Post they are belong.
解决方案
This is perfectly fine. Of course you should always find your own workflow of how to mix up classes and instances.
这篇关于可接受的实体逻辑?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!