Hug*_*and 4 design-patterns entitymanager symfony doctrine-orm
对不起,如果这个问题已经得到解答,我找不到任何问题.
我在Symfony2中构建这个模型:
class LogEntry {
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var \DateTime $log_timestamp
*
* @ORM\Column(name="log_timestamp", type="datetime")
*/
private $log_timestamp;
/**
* @var TranslationMapping $source
*
* @ORM\ManyToOne(targetEntity="TranslationMapping", cascade={"persist"})
* @ORM\JoinColumn(name="source", referencedColumnName="id")
*/
private $source;
/**
* @var TranslationMapping $target
*
* @ORM\ManyToOne(targetEntity="TranslationMapping", cascade={"persist"})
* @ORM\JoinColumn(name="target", referencedColumnName="id")
*/
private $target;
...
}
Run Code Online (Sandbox Code Playgroud)
使用TranslationMapping像这样:
/**
* LogAnalyzer\Bundle\CombatLogBundle\Entity\TranslationMapping
*
* @ORM\Table(name="TranslationMapping", uniqueConstraints={@ORM\UniqueConstraint(name="idValue_idx", columns={"stringId", "stringValue"})})
* @ORM\Entity
* @ORM\HasLifecycleCallbacks()
*/
class TranslationMapping
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $stringId
*
* @ORM\Column(name="stringId", type="string", length=255)
*/
private $stringId;
/**
* @var string $stringValue
*
* @ORM\Column(name="stringValue", type="string", length=255)
*/
private $stringValue;
/**
* @ORM\PrePersist
*/
public function beforePersist()
{
if ($this->stringId == null) {
$this->stringId = "laGen_".time();
}
}
Run Code Online (Sandbox Code Playgroud)
LogEntry是基于通过正则表达式解析的字符串构建的.TranslationMapping表示存储的键/值转换字符串.
目前我正在做的是:
从LogEntryRepository中的字符串构建LogEntry
$logEntry = new LogEntry();
$source = new TranslationMapping();
if ($logEntry->getSourceIsPlayer()) {
$source->setValueKey(str_replace("@", "", $matches["source"][0]));
} else {
$source->setValueKey($matches["source"][0], $matches["source_id"][0]);
if (isset($matches["source_companion_name"][0])) { // It's a companion !
$companion = new TranslationMapping();
$companion->setValueKey($matches["source_companion_name"][0], $matches["source_companion_id"][0]);
$logEntry->setSourceCompanion($companion);
$source->setValueKey(str_replace("@", "", $matches["source"][0])); // And the source is the player's character name
}
}
$logEntry->setSource($source);
$logEntry->setTargetIsPlayer(strpos($matches["target"][0], "@") !== false);
$target = new TranslationMapping();
if ($logEntry->getTargetIsPlayer()) {
$target->setValueKey(str_replace("@", "", $matches["target"][0]));
} else {
$target->setValueKey($matches["target"][0], $matches["target_id"][0]);
if (isset($matches["target_companion_name"][0])) { // It's a companion !
$companion = new TranslationMapping();
$companion->setValueKey($matches["target_companion_name"][0], $matches["target_companion_id"][0]);
$logEntry->setTargetCompanion($companion);
$target->setValueKey(str_replace("@", "", $matches["target"][0])); // And the target is the player's character name
}
}
$logEntry->setTarget($target);
Run Code Online (Sandbox Code Playgroud)显示LogEntry
我的问题是,如果键/值对尚不存在,我想创建一个翻译映射实体.但是,我不知道我怎么能这样做,因为:
基本上我要做的是为我的TranslationMapping实体获取一个Manager,它将管理数据库操作(如果需要,可以获取和插入),并通过所有Symfony应用程序公开实体,主要是存储库.
我已经尝试了几种解决方案并且没有选择,有没有人知道如何实现这一目标?
谢谢.
基本上,您希望创建S2服务并将实体管理器注入其中.手册中有很多例子,但如果你真的需要的话,我可以发一个.
处理一种实体类型时,存储库很棒但是,正如您所知,处理多种类型时并非如此.我很少再使用它们了.只需将服务中的功能包装起来即可.
INSERT ... ON DUPLICATE KEY UPDATE很遗憾不是标准的sql,因此Doctrine不支持它,尽管你可以将它添加为自定义函数.但最好的办法是检查密钥是否存在然后进行相应调整.请务必抓住任何例外以防万一.
我没有真正详细介绍你的代码,但S2确实有很多翻译内容.其中一些可能也有帮助.
更新:
有关创建服务的信息,请访问:http: //symfony.com/doc/current/book/service_container.html
在您的services.xml文件中,您可能具有以下内容:
<service
id="zayso.core.project.manager"
class="Zayso\ZaysoBundle\Component\Manager\ProjectManager" public="true">
<argument type="service" id="doctrine.orm.entity_manager" />
</service>
Run Code Online (Sandbox Code Playgroud)
你的经理会像__construct一样:
class ProjectManager
{
protected $em = null;
public function getEntityManager() { return $this->em; }
public function __construct($em) { $this->em = $em; }
Run Code Online (Sandbox Code Playgroud)
然后在您的控制器中,您将执行以下操作:
$manager = $this->get(zayso.core.project.manager);
$manager->newLogEntry($params);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5781 次 |
| 最近记录: |