Bidirectional One-To-One Relationships in Flow

2019-08-30 04:35发布

问题:

Is it possible to have One-To-One Relationships in Flow without having to set the attributes twice?

I have two tables that are connected in a One-To-One Relationship, but only one of them should contain an extra column for this Relation.

Doctrine clearly supports this behavior: http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#one-to-one-bidirectional

The class that should come with a componenttape column:

/**
 * @Flow\Entity
*/
class Component{
    /**
     * @var \Some\Package\Domain\Model\Component\Tape
     * @ORM\OneToOne(cascade={"all"}, inversedBy="component")
     */
    protected $componentTape;
   …
}

The class that should just be able to find the connection without an extra column:

/**
 * @Flow\Entity
*/
class Tape{
    /**
     * @var \ Some\Package\Domain\Model\Component
     * @ORM\OneToOne(mappedBy="componentTape")
     */
    protected $component;
}

A doctrine update will create extra columns for both models.

This is what my workarround at the moment looks like:

class Component{    
     ..    
     /**
     * @param \Some\Package\Domain\Model\Component\Tape $componentTape
     * @return void
     */
    public function setComponentTape($componentTape) {
        $this->componentTape = $componentTape;
        $this->componentTape->setComponent($this);
    }

回答1:

The workaround will be necessary anyway to keep the relation correct at all times during a request. But the second DB column shouldn't be necessary. Did you check if doctrine actually fills it? Maybe/Probably just the created migration is wrong and the component column in Tape can be omitted.



回答2:

Does your workaround stil work for you? In my case, I have to update the ComponentTape model on the repository by self:

class Component {    

   /**
    * @param \Some\Package\Domain\Model\Component\Tape $componentTape
    * @return void
    */
   public function setComponentTape($componentTape) {
     $this->componentTape = $componentTape;
     $this->componentTape->setComponent($this);
     $this->componentTapeRepository->update($this->componentTape);
   }