Custom Many-To-Many relation in DoctrineUndefined index on doctrine m:n relationEntityAudit - Doctrine2: Using the same entity in both OneToMany and OneToOne possibleIssues in entities from different bundles using different entity managersdoctrine 2 many to many with translationhow to add column in table using doctrine symfony2Symfony2 doctrine join returns too much dataPropper entity associations mapping with one entity for join table (Doctrine 2)Doctrine :: Relation ManyToOne will not workDoctrine2 relation for non Id columnHow to fix broken doctrine relations?

Creating chess engine, machine learning vs. traditional engine?

Is there such thing as plasma (from reentry) creating lift?

useState hook setter incorrectly overwrites state

What does IKEA-like mean?

How to find out which object is taking space?

How to make "acts of patience" exciting?

Can one use mythology to study the history of a region?

How could "aggressor" pilots fly foreign aircraft without speaking the language?

Why didn't Kes send Voyager home?

Is it fine to ask this kind of question to the corresponding author of a paper?

Can you pitch an outline?

Overlay image with parts of another image

Modify real part and leaves imaginary part unchanged

RTL8723DE Working with signal, but never connects

Was Wayne Brady considered a guest star on "Whose Line Is It Anyway?"

Would my post-apocalyptic US Government be able to work and function properly?

Why can I ping 10.0.0.0/8 addresses from a 192.168.1.0/24 subnet?

Conveying the idea of " judge a book by its cover" by " juger un livre par sa couverture"

What do you call the fallacy of thinking that some action A will guarantee some outcome B, when in reality B depends on multiple other conditions?

Can I use I2C over 2m cables?

What does ぎゃんかわ女子 mean?

Difference between $HOME and ~

Can I perform Umrah while on a Saudi Arabian visit e-visa

What is this dial on my old SLR for?



Custom Many-To-Many relation in Doctrine


Undefined index on doctrine m:n relationEntityAudit - Doctrine2: Using the same entity in both OneToMany and OneToOne possibleIssues in entities from different bundles using different entity managersdoctrine 2 many to many with translationhow to add column in table using doctrine symfony2Symfony2 doctrine join returns too much dataPropper entity associations mapping with one entity for join table (Doctrine 2)Doctrine :: Relation ManyToOne will not workDoctrine2 relation for non Id columnHow to fix broken doctrine relations?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty
margin-bottom:0;









0















Let's say I want to implement the following entities in Doctrine:





Thing represents anything that can perform an action over another Thing. A Relation can contain two entities and a description of the action, and its tuple is unique.



Here are some examples of what I'm trying to achieve:



  • Hens (thing) lay (action) eggs (thing)

  • Water (thing) extinguishes (action) fire (thing)

  • Bob (thing) likes (action) apples (thing)

  • Bob (thing) drinks (action) water (thing)

As you can see, order matters (it's not the same "Bob likes apples" as "Apples like Bob").




Despite my best efforts, I can't find any proper way to implement this.



I've tried creating a field called $relations in Thing tagged as "One-To-Many" and then marking $left and $right in Relation as "Many-To-One". The problem is I can't have two inversed sides for the same owning side (at least AFAIK).



This current implementation neither allows for getting all relations for a particular Thing in the $relations field as I'm only able to specify one inversed side.



Here's what I've come up with so far:



/** @ORMEntity */
class Thing
/**
* @ORMId
* @ORMColumn(type="integer", options="unsigned":true)
* @ORMGeneratedValue
*/
protected $id;

/** @ORMColumn(type="string") */
protected $name;

/**
* @ORMOneToMany(targetEntity="Relation", mappedBy="right")
*/
protected $relations;



/** @ORMEntity */
class Relation
/** @ORMId @ORMColumn(type="string") */
protected $action;

/** @ORMId @ORMManyToOne(targetEntity="Thing") */
private $left;

/** @ORMId @ORMManyToOne(targetEntity="Thing", inversedBy="relations") */
private $right;










share|improve this question






























    0















    Let's say I want to implement the following entities in Doctrine:





    Thing represents anything that can perform an action over another Thing. A Relation can contain two entities and a description of the action, and its tuple is unique.



    Here are some examples of what I'm trying to achieve:



    • Hens (thing) lay (action) eggs (thing)

    • Water (thing) extinguishes (action) fire (thing)

    • Bob (thing) likes (action) apples (thing)

    • Bob (thing) drinks (action) water (thing)

    As you can see, order matters (it's not the same "Bob likes apples" as "Apples like Bob").




    Despite my best efforts, I can't find any proper way to implement this.



    I've tried creating a field called $relations in Thing tagged as "One-To-Many" and then marking $left and $right in Relation as "Many-To-One". The problem is I can't have two inversed sides for the same owning side (at least AFAIK).



    This current implementation neither allows for getting all relations for a particular Thing in the $relations field as I'm only able to specify one inversed side.



    Here's what I've come up with so far:



    /** @ORMEntity */
    class Thing
    /**
    * @ORMId
    * @ORMColumn(type="integer", options="unsigned":true)
    * @ORMGeneratedValue
    */
    protected $id;

    /** @ORMColumn(type="string") */
    protected $name;

    /**
    * @ORMOneToMany(targetEntity="Relation", mappedBy="right")
    */
    protected $relations;



    /** @ORMEntity */
    class Relation
    /** @ORMId @ORMColumn(type="string") */
    protected $action;

    /** @ORMId @ORMManyToOne(targetEntity="Thing") */
    private $left;

    /** @ORMId @ORMManyToOne(targetEntity="Thing", inversedBy="relations") */
    private $right;










    share|improve this question


























      0












      0








      0








      Let's say I want to implement the following entities in Doctrine:





      Thing represents anything that can perform an action over another Thing. A Relation can contain two entities and a description of the action, and its tuple is unique.



      Here are some examples of what I'm trying to achieve:



      • Hens (thing) lay (action) eggs (thing)

      • Water (thing) extinguishes (action) fire (thing)

      • Bob (thing) likes (action) apples (thing)

      • Bob (thing) drinks (action) water (thing)

      As you can see, order matters (it's not the same "Bob likes apples" as "Apples like Bob").




      Despite my best efforts, I can't find any proper way to implement this.



      I've tried creating a field called $relations in Thing tagged as "One-To-Many" and then marking $left and $right in Relation as "Many-To-One". The problem is I can't have two inversed sides for the same owning side (at least AFAIK).



      This current implementation neither allows for getting all relations for a particular Thing in the $relations field as I'm only able to specify one inversed side.



      Here's what I've come up with so far:



      /** @ORMEntity */
      class Thing
      /**
      * @ORMId
      * @ORMColumn(type="integer", options="unsigned":true)
      * @ORMGeneratedValue
      */
      protected $id;

      /** @ORMColumn(type="string") */
      protected $name;

      /**
      * @ORMOneToMany(targetEntity="Relation", mappedBy="right")
      */
      protected $relations;



      /** @ORMEntity */
      class Relation
      /** @ORMId @ORMColumn(type="string") */
      protected $action;

      /** @ORMId @ORMManyToOne(targetEntity="Thing") */
      private $left;

      /** @ORMId @ORMManyToOne(targetEntity="Thing", inversedBy="relations") */
      private $right;










      share|improve this question














      Let's say I want to implement the following entities in Doctrine:





      Thing represents anything that can perform an action over another Thing. A Relation can contain two entities and a description of the action, and its tuple is unique.



      Here are some examples of what I'm trying to achieve:



      • Hens (thing) lay (action) eggs (thing)

      • Water (thing) extinguishes (action) fire (thing)

      • Bob (thing) likes (action) apples (thing)

      • Bob (thing) drinks (action) water (thing)

      As you can see, order matters (it's not the same "Bob likes apples" as "Apples like Bob").




      Despite my best efforts, I can't find any proper way to implement this.



      I've tried creating a field called $relations in Thing tagged as "One-To-Many" and then marking $left and $right in Relation as "Many-To-One". The problem is I can't have two inversed sides for the same owning side (at least AFAIK).



      This current implementation neither allows for getting all relations for a particular Thing in the $relations field as I'm only able to specify one inversed side.



      Here's what I've come up with so far:



      /** @ORMEntity */
      class Thing
      /**
      * @ORMId
      * @ORMColumn(type="integer", options="unsigned":true)
      * @ORMGeneratedValue
      */
      protected $id;

      /** @ORMColumn(type="string") */
      protected $name;

      /**
      * @ORMOneToMany(targetEntity="Relation", mappedBy="right")
      */
      protected $relations;



      /** @ORMEntity */
      class Relation
      /** @ORMId @ORMColumn(type="string") */
      protected $action;

      /** @ORMId @ORMManyToOne(targetEntity="Thing") */
      private $left;

      /** @ORMId @ORMManyToOne(targetEntity="Thing", inversedBy="relations") */
      private $right;







      php doctrine-orm doctrine






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 28 at 21:00









      josemmojosemmo

      1,7702 gold badges24 silver badges38 bronze badges




      1,7702 gold badges24 silver badges38 bronze badges

























          1 Answer
          1






          active

          oldest

          votes


















          0
















          Turns out Doctrine doesn't directly allow for this kind of relation.



          The best solution I could came up with was to create two collections in Thing for the relations instead of one and then defining a method that outputs both:



          /** @ORMEntity */
          class Thing
          // [...]

          /** @ORMOneToMany(targetEntity="Relation", mappedBy="left") */
          protected $relationsLeft;

          /** @ORMOneToMany(targetEntity="Relation", mappedBy="right") */
          protected $relationsRight;

          public function getRelations()
          return new ArrayCollection(array_merge(
          $this->relationsLeft->toArray(),
          $this->relationsRight->toArray()
          ));




          /** @ORMEntity */
          class Relation
          // [...]

          /** @ORMId @ORMManyToOne(targetEntity="Thing", inversedBy="relationsLeft") */
          private $left;

          /** @ORMId @ORMManyToOne(targetEntity="Thing", inversedBy="relationsRight") */
          private $right;






          share|improve this answer


























            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/4.0/"u003ecc by-sa 4.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );














            draft saved

            draft discarded
















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406784%2fcustom-many-to-many-relation-in-doctrine%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0
















            Turns out Doctrine doesn't directly allow for this kind of relation.



            The best solution I could came up with was to create two collections in Thing for the relations instead of one and then defining a method that outputs both:



            /** @ORMEntity */
            class Thing
            // [...]

            /** @ORMOneToMany(targetEntity="Relation", mappedBy="left") */
            protected $relationsLeft;

            /** @ORMOneToMany(targetEntity="Relation", mappedBy="right") */
            protected $relationsRight;

            public function getRelations()
            return new ArrayCollection(array_merge(
            $this->relationsLeft->toArray(),
            $this->relationsRight->toArray()
            ));




            /** @ORMEntity */
            class Relation
            // [...]

            /** @ORMId @ORMManyToOne(targetEntity="Thing", inversedBy="relationsLeft") */
            private $left;

            /** @ORMId @ORMManyToOne(targetEntity="Thing", inversedBy="relationsRight") */
            private $right;






            share|improve this answer





























              0
















              Turns out Doctrine doesn't directly allow for this kind of relation.



              The best solution I could came up with was to create two collections in Thing for the relations instead of one and then defining a method that outputs both:



              /** @ORMEntity */
              class Thing
              // [...]

              /** @ORMOneToMany(targetEntity="Relation", mappedBy="left") */
              protected $relationsLeft;

              /** @ORMOneToMany(targetEntity="Relation", mappedBy="right") */
              protected $relationsRight;

              public function getRelations()
              return new ArrayCollection(array_merge(
              $this->relationsLeft->toArray(),
              $this->relationsRight->toArray()
              ));




              /** @ORMEntity */
              class Relation
              // [...]

              /** @ORMId @ORMManyToOne(targetEntity="Thing", inversedBy="relationsLeft") */
              private $left;

              /** @ORMId @ORMManyToOne(targetEntity="Thing", inversedBy="relationsRight") */
              private $right;






              share|improve this answer



























                0














                0










                0









                Turns out Doctrine doesn't directly allow for this kind of relation.



                The best solution I could came up with was to create two collections in Thing for the relations instead of one and then defining a method that outputs both:



                /** @ORMEntity */
                class Thing
                // [...]

                /** @ORMOneToMany(targetEntity="Relation", mappedBy="left") */
                protected $relationsLeft;

                /** @ORMOneToMany(targetEntity="Relation", mappedBy="right") */
                protected $relationsRight;

                public function getRelations()
                return new ArrayCollection(array_merge(
                $this->relationsLeft->toArray(),
                $this->relationsRight->toArray()
                ));




                /** @ORMEntity */
                class Relation
                // [...]

                /** @ORMId @ORMManyToOne(targetEntity="Thing", inversedBy="relationsLeft") */
                private $left;

                /** @ORMId @ORMManyToOne(targetEntity="Thing", inversedBy="relationsRight") */
                private $right;






                share|improve this answer













                Turns out Doctrine doesn't directly allow for this kind of relation.



                The best solution I could came up with was to create two collections in Thing for the relations instead of one and then defining a method that outputs both:



                /** @ORMEntity */
                class Thing
                // [...]

                /** @ORMOneToMany(targetEntity="Relation", mappedBy="left") */
                protected $relationsLeft;

                /** @ORMOneToMany(targetEntity="Relation", mappedBy="right") */
                protected $relationsRight;

                public function getRelations()
                return new ArrayCollection(array_merge(
                $this->relationsLeft->toArray(),
                $this->relationsRight->toArray()
                ));




                /** @ORMEntity */
                class Relation
                // [...]

                /** @ORMId @ORMManyToOne(targetEntity="Thing", inversedBy="relationsLeft") */
                private $left;

                /** @ORMId @ORMManyToOne(targetEntity="Thing", inversedBy="relationsRight") */
                private $right;







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Apr 11 at 16:42









                josemmojosemmo

                1,7702 gold badges24 silver badges38 bronze badges




                1,7702 gold badges24 silver badges38 bronze badges

































                    draft saved

                    draft discarded















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55406784%2fcustom-many-to-many-relation-in-doctrine%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현