2014-01-31 7 views
5

Ich habe Datenbanktabelle, die wie folgt aussieht:Eltern Kind Beziehung mit einem einzigen Entity In Lehre 2

 
+----+--------+--------------------+ 
| id | parent | description  | 
+----+--------+--------------------+ 
| 1 | null | P Cat 1   | 
| 2 | 1  | Child 1 of P Cat 1 | 
| 3 | 1  | Child 2 of P Cat 1 | 
| 4 | null | P Cat 2   | 
| 5 | 4  | Child 1 of P Cat 2 | 
| 6 | 4  | Child 2 of P Cat 2 | 
+----+--------+--------------------+ 

Wie kann ich eine Lehre 2-Einheit erstellen, die diese Spalten haben, aber ich brauche die übergeordnete Spalte zu referenzieren die "id" -Spalte als Elternteil. Natürlich hat ein Eltern-Datensatz einen "Eltern" -Spaltenwert null.

So schön habe ich

<?php 
namespace MyNamespace; 
use Doctrine\ORM\Mapping AS ORM; 
use Doctrine\Common\Collections\ArrayCollection; 
/** 
* @ORM\Entity 
* @ORM\Table(name="category") 
**/ 
class Category 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer", name="id") 
    * @ORM\GeneratedValue 
    */ 
    protected $id; 

    /** 
    * Creates a parent/child relationship on this entity. 
    * 
    * @ORM\ManyToOne(targetEntity="MyNamespace\Category",inversedBy="id") 
    * @ORM\JoinColumn(name="FK_parent_id", referencedColumnName="id", nullable=true) 
    */ 
    protected $parent = null; 

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

    /** 
    * Gets the Primary key value. 
    * 
    * @return integer 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Sets another category ID as the parent of this category. 
    */ 
    public function setParent(Category $category) 
    { 
     $this->parent = $category; 
    } 

    /** 
    * Clears the parent id and makes it null. 
    */ 
    public function clearParent() 
    { 
     $this->parent = null; 
    } 

    /** 
    * Sets the description. 
    * 
    * @param string $description 
    * @return Category 
    */ 
    public function setDescription($description) 
    { 
     $this->description = $description; 
     return $this; 
    } 

    /** 
    * Gets the description value. 
    * 
    * @return string 
    */ 
    public function getDescription() 
    { 
     return $this->description; 
    } 
} 

Unnötig zu sagen, dies nicht zu funktionieren scheinen. Die Fragen sind:

  1. Die SetParent() -Methode scheint nicht zu funktionieren, wie erwartet, wenn eine andere Entität als Eltern hinzugefügt wird.
  2. Ich brauche eine getChildren() -Methode für diese Entität. Wie kann ich das erreichen?
+0

Auschecken [dieses Beispiel] (http://docs.doctrine-project.org/en/2.0.x/reference/association-mapping.html#one-to-many-self-referencing). Es könnte auch hilfreich sein, wenn Sie über "das scheint nicht zu funktionieren" klarstellen, was nicht funktioniert. – AlexP

Antwort

11

Diese Arbeit sollte:

<?php 

use Doctrine\Common\Collections\ArrayCollection; 

/** @ORM\Entity */ 
class Category { 
    /** 
    * @ORM\OneToMany(targetEntity="Category", mappedBy="parent") 
    */ 
    protected $children; 

    /** 
    * @ORM\ManyToOne(targetEntity="Category", inversedBy="children") 
    * @ORM\JoinColumn(name="parent", referencedColumnName="id") 
    */ 
    private $parent; 

    public function __construct() 
    { 
     $this->children = new ArrayCollection(); 
    } 

    // Once you have that, accessing the parent and children should be straight forward 
    // (they will be lazy-loaded in this example as soon as you try to access them). IE: 

    public function getParent() { 
     return $this->parent; 
    } 

    public function getChildren() { 
     return $this->children; 
    } 

    // always use this to setup a new parent/child relationship 
    public function addChild(Category $child) { 
     $this->children[] = $child; 
     $child->setParent($this); 
    } 

    public function setParent(Category $parent) { 
     $this->parent = $parent; 
    } 

} 
+0

In diesem Fall referencedColumnName = "id" muss nicht in referencedColumnName = "children" geändert werden? Wer sind die ID-Felder? –

+1

@MarceloAymone Die ID und andere Felder werden in der Frage geschrieben, ich habe sie hier nicht kopieren und einfügen, aber ja, referenzierteColumnName = "ID" ist korrekt. – redreinard

0

dies in YAML zu tun, würde die Konfiguration in etwa so aussehen:

AppBundle\Entity\Category: 
    type: entity 
    table: category 
    repositoryClass: AppBundle\Repository\CategoryRepository 
    id: 
     id: 
      type: integer 
      id: true 
      generator: 
       strategy: AUTO 
    oneToMany: 
     children: 
      targetEntity: Category 
      mappedBy: parent 
    manyToOne: 
     parent: 
      targetEntity: Category 
      inversedBy: children 

Quelle: http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/association-mapping.html#one-to-many-self-referencing

0

Wenn Sie mit XML würde die Konfiguration wie folgt aussehen:

<?xml version="1.0" encoding="utf-8"?> 
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping" 
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"> 
    <entity repository-class="AppBundle\Repository\CategoryRepository" name="AppBundle\Entity\Category"> 
     <id name="id" type="integer" column="id"> 
      <generator strategy="AUTO"/> 
     </id> 
     <field name="description" type="string" column="description" length="255"/> 
     <many-to-one target-entity="Category" inversed-by="children" field="parent"> 
      <join-column name="parent" referenced-column-name="id"/> 
     </many-to-one> 
     <one-to-many target-entity="Category" mapped-by="parent" field="child"/> 
    </entity> 
</doctrine-mapping> 
Verwandte Themen