Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8479011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:54:26+00:00 2026-06-10T18:54:26+00:00

I have a simple table for recursive categories: id name parent_id – is a

  • 0

I have a simple table for recursive categories:

id
name
parent_id - is a link to id, NULL - for root categories

I need to create a table with foreign key. My class doesn’t create this key. How can I change this class to create table with foreign key using “doctrine:schema:create”? What I have tried:

<?php
namespace Test\BackEndBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 * @ORM\Table(name="category")
 */
class Category
{
    /**
     * @ORM\Id
     * @ORM\Column(type="bigint", length=20)
     * @ORM\GeneratedValue(strategy="AUTO")
     * @var int
     */
    protected $id;

    /**
     * @ORM\Column(type="string", length="255")
     * @var string
     */
    protected $name;

    /**
     * @ORM\Column(name="parent_id", type="bigint", length=20, nullable="true")
     * @ORM\OneToMany(targetEntity="Category")
     * @ORM\JoinColumn(name="id", onDelete="CASCADE", onUpdate="CASCADE")
     * @var int
     */
    protected $parentId;

    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set name
     *
     * @param string $name
     */
    public function setName($name)
    {
        $this->name = $name;
    }

    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:54:27+00:00Added an answer on June 10, 2026 at 6:54 pm

    You reversed OneToMany with ManyToOne !

    See here: http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html#one-to-many-self-referencing

    In fact, you want that plural subcategories can have one parent cat !

    What you need:

    /**
     * @OneToMany(targetEntity="Category", mappedBy="parent")
     */
    private $children;
    
    /**
     * @ManyToOne(targetEntity="Category", inversedBy="children")
     * @JoinColumn(name="parent_id", referencedColumnName="id")
     */
    private $parent;
    // ...
    
    public function __construct() {
        $this->children = new \Doctrine\Common\Collections\ArrayCollection();
    }
    

    You can replace $parent for $parentId (and mappedBy=”parent” for mappedBy=”parentId”), but this is not a good coding convention ^^

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple table creating script in Postgres 9.1. I need it to create
I have simple table in Sybase -- Creating table 'SimpleText' CREATE TABLE [dbo].[SimpleText] (
I have a simple table in Sybase, let's say it looks as follows: CREATE
I have a simple table view which is editable. All I need the user
I have simple table with two columns: id INTEGER as a key, and data
I have a simple table with 2 colums: ID and Name . It looks
I have a simple self referencing table as depicted here: CREATE TABLE [dbo].[Project]( [ProjectId]
I have a simple table that has this structure: <table> <thead> <tr> <td>Some info</td>
I have a simple table maintaining messages between users. The table structure looks like
I have a simple table like this <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0 Transitional//EN

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.