Problem Symfony/Doctrine : One-To-Many - Self-referencing on a primary key





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















I would like to have a "post" with an identifier. This one could be classified in another "post" by storing the identifier of his parent.



I tried to do like this:



class Post {
/**
* @ORMId
* @ORMGeneratedValue
* @ORMColumn(type="integer")
* @ORMOneToMany(targetEntity="Post", mappedBy="Id_Post_Parent")
*/
private $Id_Post;

/**
* @ORMManyToOne(targetEntity="AppEntityPost", inversedBy="Id_Post")
* @ORMJoinColumn(name="Id_Post", referencedColumnName="Id_Post", nullable=true)
*/
private $Id_Post_Parent;
...
}


but I have this error when i'm checking with doctrine:schema:validate :




[FAIL] The entity-class AppEntityPost mapping is invalid:




  • The association AppEntityPost#Id_Post_Parent refers to the inverse side field AppEntityPost#Id_Post which is not defined as association.

  • The association AppEntityPost#Id_Post_Parent refers to the inverse side field AppEntityPost#Id_Post which does not exist.

  • The referenced column name 'Id_Post' has to be a primary key column on the target entity class 'AppEntityPost'.




Can someone help me to fix this ?










share|improve this question





























    0















    I would like to have a "post" with an identifier. This one could be classified in another "post" by storing the identifier of his parent.



    I tried to do like this:



    class Post {
    /**
    * @ORMId
    * @ORMGeneratedValue
    * @ORMColumn(type="integer")
    * @ORMOneToMany(targetEntity="Post", mappedBy="Id_Post_Parent")
    */
    private $Id_Post;

    /**
    * @ORMManyToOne(targetEntity="AppEntityPost", inversedBy="Id_Post")
    * @ORMJoinColumn(name="Id_Post", referencedColumnName="Id_Post", nullable=true)
    */
    private $Id_Post_Parent;
    ...
    }


    but I have this error when i'm checking with doctrine:schema:validate :




    [FAIL] The entity-class AppEntityPost mapping is invalid:




    • The association AppEntityPost#Id_Post_Parent refers to the inverse side field AppEntityPost#Id_Post which is not defined as association.

    • The association AppEntityPost#Id_Post_Parent refers to the inverse side field AppEntityPost#Id_Post which does not exist.

    • The referenced column name 'Id_Post' has to be a primary key column on the target entity class 'AppEntityPost'.




    Can someone help me to fix this ?










    share|improve this question

























      0












      0








      0








      I would like to have a "post" with an identifier. This one could be classified in another "post" by storing the identifier of his parent.



      I tried to do like this:



      class Post {
      /**
      * @ORMId
      * @ORMGeneratedValue
      * @ORMColumn(type="integer")
      * @ORMOneToMany(targetEntity="Post", mappedBy="Id_Post_Parent")
      */
      private $Id_Post;

      /**
      * @ORMManyToOne(targetEntity="AppEntityPost", inversedBy="Id_Post")
      * @ORMJoinColumn(name="Id_Post", referencedColumnName="Id_Post", nullable=true)
      */
      private $Id_Post_Parent;
      ...
      }


      but I have this error when i'm checking with doctrine:schema:validate :




      [FAIL] The entity-class AppEntityPost mapping is invalid:




      • The association AppEntityPost#Id_Post_Parent refers to the inverse side field AppEntityPost#Id_Post which is not defined as association.

      • The association AppEntityPost#Id_Post_Parent refers to the inverse side field AppEntityPost#Id_Post which does not exist.

      • The referenced column name 'Id_Post' has to be a primary key column on the target entity class 'AppEntityPost'.




      Can someone help me to fix this ?










      share|improve this question














      I would like to have a "post" with an identifier. This one could be classified in another "post" by storing the identifier of his parent.



      I tried to do like this:



      class Post {
      /**
      * @ORMId
      * @ORMGeneratedValue
      * @ORMColumn(type="integer")
      * @ORMOneToMany(targetEntity="Post", mappedBy="Id_Post_Parent")
      */
      private $Id_Post;

      /**
      * @ORMManyToOne(targetEntity="AppEntityPost", inversedBy="Id_Post")
      * @ORMJoinColumn(name="Id_Post", referencedColumnName="Id_Post", nullable=true)
      */
      private $Id_Post_Parent;
      ...
      }


      but I have this error when i'm checking with doctrine:schema:validate :




      [FAIL] The entity-class AppEntityPost mapping is invalid:




      • The association AppEntityPost#Id_Post_Parent refers to the inverse side field AppEntityPost#Id_Post which is not defined as association.

      • The association AppEntityPost#Id_Post_Parent refers to the inverse side field AppEntityPost#Id_Post which does not exist.

      • The referenced column name 'Id_Post' has to be a primary key column on the target entity class 'AppEntityPost'.




      Can someone help me to fix this ?







      symfony doctrine primary-key






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 23 '18 at 19:25









      AzrixAzrix

      377




      377
























          1 Answer
          1






          active

          oldest

          votes


















          1














          There is small logical error with your structure - your ID_Post variable tries to be both the primary key (the ID) and the collection association side. I didn't check this syntax in too much details (you can find an example of this association along with most of the other associations from doctrine documentation: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/association-mapping.html#one-to-many-self-referencing), but basically you need to add the children association separately to your entity like this:



          class Post 
          {
          /**
          * @ORMId
          * @ORMGeneratedValue
          * @ORMColumn(type="integer")
          */
          private $id;

          /**
          * @ORMManyToOne(targetEntity="Post", inversedBy="postChildren")
          * @ORMJoinColumn(name="id_parent_post", referencedColumnName="id", nullable=true)
          */
          private $postParent;

          /**
          * @ORMOneToMany(targetEntity="Post", mappedBy="postParent")
          */
          private $postChildren;

          public function __construct() {
          $this->postChildren = new DoctrineCommonCollectionsArrayCollection();
          }
          }





          share|improve this answer


























          • Isn't it possible to get a recursive entity on the Id ? Actually I wanted to get the Id of another post into each one

            – Azrix
            Nov 23 '18 at 21:15











          • it is referencing to the ID of the parent post entity - that part is visible in the $Post_parent join column definition 'referencedColumnName="Id_Post". I.e. this entity structure will generate in the database columns "Id_Post" (the post id) and "Id_Parent_Post" (the foreign key reference to the parent post id). Assuming that "recursive entity" means "self-referencing entity" that is.

            – ejuhjav
            Nov 26 '18 at 9:44













          • Ok thanks ! But with your solution, I still have this error : The referenced column name 'Id_Post' has to be a primary key column on the target entity class 'AppEntityPost'. Do you know why ?

            – Azrix
            Nov 26 '18 at 19:46











          • Are you using doctrine to generate the database structure from the entities or have you defined your table separately? Namely, do you have column with name "Post_Id" in the related database table for posts?

            – ejuhjav
            Nov 27 '18 at 7:48








          • 1





            ah, I just typoed the field name the wrong away around in my comment there. But that's fine, you answered to the question in any case. I haven't checked how doctrine handles the capitalized member variable names when generating the column names so there might be some mismatch due to that - I updated the example above to use the names without the capitals (if you don't want to regenerate the schema - you can also just check what the actual database column name is for the id: and if it is "id_post" (without capitals), update the "referencedColumnName" to use this uncapitalized version.

            – ejuhjav
            Nov 28 '18 at 9:30












          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/3.0/"u003ecc by-sa 3.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%2f53452024%2fproblem-symfony-doctrine-one-to-many-self-referencing-on-a-primary-key%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









          1














          There is small logical error with your structure - your ID_Post variable tries to be both the primary key (the ID) and the collection association side. I didn't check this syntax in too much details (you can find an example of this association along with most of the other associations from doctrine documentation: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/association-mapping.html#one-to-many-self-referencing), but basically you need to add the children association separately to your entity like this:



          class Post 
          {
          /**
          * @ORMId
          * @ORMGeneratedValue
          * @ORMColumn(type="integer")
          */
          private $id;

          /**
          * @ORMManyToOne(targetEntity="Post", inversedBy="postChildren")
          * @ORMJoinColumn(name="id_parent_post", referencedColumnName="id", nullable=true)
          */
          private $postParent;

          /**
          * @ORMOneToMany(targetEntity="Post", mappedBy="postParent")
          */
          private $postChildren;

          public function __construct() {
          $this->postChildren = new DoctrineCommonCollectionsArrayCollection();
          }
          }





          share|improve this answer


























          • Isn't it possible to get a recursive entity on the Id ? Actually I wanted to get the Id of another post into each one

            – Azrix
            Nov 23 '18 at 21:15











          • it is referencing to the ID of the parent post entity - that part is visible in the $Post_parent join column definition 'referencedColumnName="Id_Post". I.e. this entity structure will generate in the database columns "Id_Post" (the post id) and "Id_Parent_Post" (the foreign key reference to the parent post id). Assuming that "recursive entity" means "self-referencing entity" that is.

            – ejuhjav
            Nov 26 '18 at 9:44













          • Ok thanks ! But with your solution, I still have this error : The referenced column name 'Id_Post' has to be a primary key column on the target entity class 'AppEntityPost'. Do you know why ?

            – Azrix
            Nov 26 '18 at 19:46











          • Are you using doctrine to generate the database structure from the entities or have you defined your table separately? Namely, do you have column with name "Post_Id" in the related database table for posts?

            – ejuhjav
            Nov 27 '18 at 7:48








          • 1





            ah, I just typoed the field name the wrong away around in my comment there. But that's fine, you answered to the question in any case. I haven't checked how doctrine handles the capitalized member variable names when generating the column names so there might be some mismatch due to that - I updated the example above to use the names without the capitals (if you don't want to regenerate the schema - you can also just check what the actual database column name is for the id: and if it is "id_post" (without capitals), update the "referencedColumnName" to use this uncapitalized version.

            – ejuhjav
            Nov 28 '18 at 9:30
















          1














          There is small logical error with your structure - your ID_Post variable tries to be both the primary key (the ID) and the collection association side. I didn't check this syntax in too much details (you can find an example of this association along with most of the other associations from doctrine documentation: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/association-mapping.html#one-to-many-self-referencing), but basically you need to add the children association separately to your entity like this:



          class Post 
          {
          /**
          * @ORMId
          * @ORMGeneratedValue
          * @ORMColumn(type="integer")
          */
          private $id;

          /**
          * @ORMManyToOne(targetEntity="Post", inversedBy="postChildren")
          * @ORMJoinColumn(name="id_parent_post", referencedColumnName="id", nullable=true)
          */
          private $postParent;

          /**
          * @ORMOneToMany(targetEntity="Post", mappedBy="postParent")
          */
          private $postChildren;

          public function __construct() {
          $this->postChildren = new DoctrineCommonCollectionsArrayCollection();
          }
          }





          share|improve this answer


























          • Isn't it possible to get a recursive entity on the Id ? Actually I wanted to get the Id of another post into each one

            – Azrix
            Nov 23 '18 at 21:15











          • it is referencing to the ID of the parent post entity - that part is visible in the $Post_parent join column definition 'referencedColumnName="Id_Post". I.e. this entity structure will generate in the database columns "Id_Post" (the post id) and "Id_Parent_Post" (the foreign key reference to the parent post id). Assuming that "recursive entity" means "self-referencing entity" that is.

            – ejuhjav
            Nov 26 '18 at 9:44













          • Ok thanks ! But with your solution, I still have this error : The referenced column name 'Id_Post' has to be a primary key column on the target entity class 'AppEntityPost'. Do you know why ?

            – Azrix
            Nov 26 '18 at 19:46











          • Are you using doctrine to generate the database structure from the entities or have you defined your table separately? Namely, do you have column with name "Post_Id" in the related database table for posts?

            – ejuhjav
            Nov 27 '18 at 7:48








          • 1





            ah, I just typoed the field name the wrong away around in my comment there. But that's fine, you answered to the question in any case. I haven't checked how doctrine handles the capitalized member variable names when generating the column names so there might be some mismatch due to that - I updated the example above to use the names without the capitals (if you don't want to regenerate the schema - you can also just check what the actual database column name is for the id: and if it is "id_post" (without capitals), update the "referencedColumnName" to use this uncapitalized version.

            – ejuhjav
            Nov 28 '18 at 9:30














          1












          1








          1







          There is small logical error with your structure - your ID_Post variable tries to be both the primary key (the ID) and the collection association side. I didn't check this syntax in too much details (you can find an example of this association along with most of the other associations from doctrine documentation: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/association-mapping.html#one-to-many-self-referencing), but basically you need to add the children association separately to your entity like this:



          class Post 
          {
          /**
          * @ORMId
          * @ORMGeneratedValue
          * @ORMColumn(type="integer")
          */
          private $id;

          /**
          * @ORMManyToOne(targetEntity="Post", inversedBy="postChildren")
          * @ORMJoinColumn(name="id_parent_post", referencedColumnName="id", nullable=true)
          */
          private $postParent;

          /**
          * @ORMOneToMany(targetEntity="Post", mappedBy="postParent")
          */
          private $postChildren;

          public function __construct() {
          $this->postChildren = new DoctrineCommonCollectionsArrayCollection();
          }
          }





          share|improve this answer















          There is small logical error with your structure - your ID_Post variable tries to be both the primary key (the ID) and the collection association side. I didn't check this syntax in too much details (you can find an example of this association along with most of the other associations from doctrine documentation: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/association-mapping.html#one-to-many-self-referencing), but basically you need to add the children association separately to your entity like this:



          class Post 
          {
          /**
          * @ORMId
          * @ORMGeneratedValue
          * @ORMColumn(type="integer")
          */
          private $id;

          /**
          * @ORMManyToOne(targetEntity="Post", inversedBy="postChildren")
          * @ORMJoinColumn(name="id_parent_post", referencedColumnName="id", nullable=true)
          */
          private $postParent;

          /**
          * @ORMOneToMany(targetEntity="Post", mappedBy="postParent")
          */
          private $postChildren;

          public function __construct() {
          $this->postChildren = new DoctrineCommonCollectionsArrayCollection();
          }
          }






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 28 '18 at 9:26

























          answered Nov 23 '18 at 20:25









          ejuhjavejuhjav

          1,70811217




          1,70811217













          • Isn't it possible to get a recursive entity on the Id ? Actually I wanted to get the Id of another post into each one

            – Azrix
            Nov 23 '18 at 21:15











          • it is referencing to the ID of the parent post entity - that part is visible in the $Post_parent join column definition 'referencedColumnName="Id_Post". I.e. this entity structure will generate in the database columns "Id_Post" (the post id) and "Id_Parent_Post" (the foreign key reference to the parent post id). Assuming that "recursive entity" means "self-referencing entity" that is.

            – ejuhjav
            Nov 26 '18 at 9:44













          • Ok thanks ! But with your solution, I still have this error : The referenced column name 'Id_Post' has to be a primary key column on the target entity class 'AppEntityPost'. Do you know why ?

            – Azrix
            Nov 26 '18 at 19:46











          • Are you using doctrine to generate the database structure from the entities or have you defined your table separately? Namely, do you have column with name "Post_Id" in the related database table for posts?

            – ejuhjav
            Nov 27 '18 at 7:48








          • 1





            ah, I just typoed the field name the wrong away around in my comment there. But that's fine, you answered to the question in any case. I haven't checked how doctrine handles the capitalized member variable names when generating the column names so there might be some mismatch due to that - I updated the example above to use the names without the capitals (if you don't want to regenerate the schema - you can also just check what the actual database column name is for the id: and if it is "id_post" (without capitals), update the "referencedColumnName" to use this uncapitalized version.

            – ejuhjav
            Nov 28 '18 at 9:30



















          • Isn't it possible to get a recursive entity on the Id ? Actually I wanted to get the Id of another post into each one

            – Azrix
            Nov 23 '18 at 21:15











          • it is referencing to the ID of the parent post entity - that part is visible in the $Post_parent join column definition 'referencedColumnName="Id_Post". I.e. this entity structure will generate in the database columns "Id_Post" (the post id) and "Id_Parent_Post" (the foreign key reference to the parent post id). Assuming that "recursive entity" means "self-referencing entity" that is.

            – ejuhjav
            Nov 26 '18 at 9:44













          • Ok thanks ! But with your solution, I still have this error : The referenced column name 'Id_Post' has to be a primary key column on the target entity class 'AppEntityPost'. Do you know why ?

            – Azrix
            Nov 26 '18 at 19:46











          • Are you using doctrine to generate the database structure from the entities or have you defined your table separately? Namely, do you have column with name "Post_Id" in the related database table for posts?

            – ejuhjav
            Nov 27 '18 at 7:48








          • 1





            ah, I just typoed the field name the wrong away around in my comment there. But that's fine, you answered to the question in any case. I haven't checked how doctrine handles the capitalized member variable names when generating the column names so there might be some mismatch due to that - I updated the example above to use the names without the capitals (if you don't want to regenerate the schema - you can also just check what the actual database column name is for the id: and if it is "id_post" (without capitals), update the "referencedColumnName" to use this uncapitalized version.

            – ejuhjav
            Nov 28 '18 at 9:30

















          Isn't it possible to get a recursive entity on the Id ? Actually I wanted to get the Id of another post into each one

          – Azrix
          Nov 23 '18 at 21:15





          Isn't it possible to get a recursive entity on the Id ? Actually I wanted to get the Id of another post into each one

          – Azrix
          Nov 23 '18 at 21:15













          it is referencing to the ID of the parent post entity - that part is visible in the $Post_parent join column definition 'referencedColumnName="Id_Post". I.e. this entity structure will generate in the database columns "Id_Post" (the post id) and "Id_Parent_Post" (the foreign key reference to the parent post id). Assuming that "recursive entity" means "self-referencing entity" that is.

          – ejuhjav
          Nov 26 '18 at 9:44







          it is referencing to the ID of the parent post entity - that part is visible in the $Post_parent join column definition 'referencedColumnName="Id_Post". I.e. this entity structure will generate in the database columns "Id_Post" (the post id) and "Id_Parent_Post" (the foreign key reference to the parent post id). Assuming that "recursive entity" means "self-referencing entity" that is.

          – ejuhjav
          Nov 26 '18 at 9:44















          Ok thanks ! But with your solution, I still have this error : The referenced column name 'Id_Post' has to be a primary key column on the target entity class 'AppEntityPost'. Do you know why ?

          – Azrix
          Nov 26 '18 at 19:46





          Ok thanks ! But with your solution, I still have this error : The referenced column name 'Id_Post' has to be a primary key column on the target entity class 'AppEntityPost'. Do you know why ?

          – Azrix
          Nov 26 '18 at 19:46













          Are you using doctrine to generate the database structure from the entities or have you defined your table separately? Namely, do you have column with name "Post_Id" in the related database table for posts?

          – ejuhjav
          Nov 27 '18 at 7:48







          Are you using doctrine to generate the database structure from the entities or have you defined your table separately? Namely, do you have column with name "Post_Id" in the related database table for posts?

          – ejuhjav
          Nov 27 '18 at 7:48






          1




          1





          ah, I just typoed the field name the wrong away around in my comment there. But that's fine, you answered to the question in any case. I haven't checked how doctrine handles the capitalized member variable names when generating the column names so there might be some mismatch due to that - I updated the example above to use the names without the capitals (if you don't want to regenerate the schema - you can also just check what the actual database column name is for the id: and if it is "id_post" (without capitals), update the "referencedColumnName" to use this uncapitalized version.

          – ejuhjav
          Nov 28 '18 at 9:30





          ah, I just typoed the field name the wrong away around in my comment there. But that's fine, you answered to the question in any case. I haven't checked how doctrine handles the capitalized member variable names when generating the column names so there might be some mismatch due to that - I updated the example above to use the names without the capitals (if you don't want to regenerate the schema - you can also just check what the actual database column name is for the id: and if it is "id_post" (without capitals), update the "referencedColumnName" to use this uncapitalized version.

          – ejuhjav
          Nov 28 '18 at 9:30




















          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%2f53452024%2fproblem-symfony-doctrine-one-to-many-self-referencing-on-a-primary-key%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







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini