Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to have a model Manager in Symfony2?
    primarykey
    data
    text
    <p>Sorry if this question has already been answered, I couldn't find any.</p> <p>I'm building this model in Symfony2:</p> <pre><code>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; ... } </code></pre> <p>With TranslationMapping like this :</p> <pre><code>/** * 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-&gt;stringId == null) { $this-&gt;stringId = "laGen_".time(); } } </code></pre> <p>LogEntry is built based on a string parsed through regex. TranslationMapping represent a key/value stored translation string.</p> <p>Currently what I'm doing is :</p> <ol> <li>Retrieving the string to build the LogEntry</li> <li><p>Building the LogEntry from the string in the LogEntryRepository</p> <pre><code>$logEntry = new LogEntry(); $source = new TranslationMapping(); if ($logEntry-&gt;getSourceIsPlayer()) { $source-&gt;setValueKey(str_replace("@", "", $matches["source"][0])); } else { $source-&gt;setValueKey($matches["source"][0], $matches["source_id"][0]); if (isset($matches["source_companion_name"][0])) { // It's a companion ! $companion = new TranslationMapping(); $companion-&gt;setValueKey($matches["source_companion_name"][0], $matches["source_companion_id"][0]); $logEntry-&gt;setSourceCompanion($companion); $source-&gt;setValueKey(str_replace("@", "", $matches["source"][0])); // And the source is the player's character name } } $logEntry-&gt;setSource($source); $logEntry-&gt;setTargetIsPlayer(strpos($matches["target"][0], "@") !== false); $target = new TranslationMapping(); if ($logEntry-&gt;getTargetIsPlayer()) { $target-&gt;setValueKey(str_replace("@", "", $matches["target"][0])); } else { $target-&gt;setValueKey($matches["target"][0], $matches["target_id"][0]); if (isset($matches["target_companion_name"][0])) { // It's a companion ! $companion = new TranslationMapping(); $companion-&gt;setValueKey($matches["target_companion_name"][0], $matches["target_companion_id"][0]); $logEntry-&gt;setTargetCompanion($companion); $target-&gt;setValueKey(str_replace("@", "", $matches["target"][0])); // And the target is the player's character name } } $logEntry-&gt;setTarget($target); </code></pre></li> <li><p>Displaying the LogEntry</p></li> </ol> <p>My issue is that I want to create a translation mapping entity if the key/value pair is non-existent yet. However, I don't see how I can do that since :</p> <ul> <li>I can't access TranslationMappingRepository from another repository</li> <li>I can't access a Service from a repository</li> <li>I can't use INSERT ... ON DUPLICATE KEY UPDATE queries via Doctrine2</li> </ul> <p>Basically what I'm trying to do is to get a Manager for my TranslationMapping entities, which would manage DB operations (fetch &amp; insert if needed) and expose the entities through all the Symfony application, and mostly to the Repositories.</p> <p>I've tried several solutions and ran out of options, does anyone have a clue of how I can achieve this ?</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload