2013-10-10 11 views
12

Ich muss einige unveränderliche Felder in separate Klasse verschieben, aber ich möchte nicht wirklich "Join" verwenden, weil ich jedes Mal alle Daten zusammen benötigen.Doctrine2 move unveränderliche Felder in separate Klasse

Gibt es eine Möglichkeit, einige Entitätsattribute als Klassen in derselben Tabelle zugeordnet zu haben?

Etwas wie:

/** 
* @ORM\Entity 
*/ 
class User { 
    /** 
    * @var int 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 
    ... 

    /** 
    * @var Address 
    * @ORM\... ?? 
    */ 
    protected $address 
} 

/** 
* @ORM\ValueObject ?? 
*/ 
class Address { 
    /** 
    * @var string 
    * @ORM\Column(type="string", name="address_zipcode", length=12) 
    */ 
    protected $zipcode; 

    /** 
    * @var string 
    * @ORM\Column(type="string", name="address_country_iso", length=3) 
    */ 
    protected $countryIso; 
    ... 
} 

Und Tabellenstruktur wäre:

CREATE TABLE User (
    `id` INT(11) NOT NULL auto_increment, 
    `address_zipcode` VARCHAR(12) NOT NULL, 
    `address_country_iso` VARCHAR(3) NOT NULL, 
    PRIMARY KEY (`id`) 
); 
+5

Wahrscheinlich [hier] (http://stackoverflow.com/questions/8440879/doctrine-2-value-objects) können Sie die Antwort für diese Frage finden. –

+1

Vielen Dank, genau das suche ich –

Antwort

0

Es wird nur sein mag, dass Sie sagte etwas wie.

Fügen Sie @PreUpdate und @PostLoad Hook in User hinzu.

/** 
* @ORM\Entity 
*/ 
class User { 
    /** 
    * @var int 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 
    ... 

    /** 
    * @var Address 
    * (NOTE: no @ORM annotation here) 
    */ 
    protected $address 

    /** 
    * @var string 
    * @ORM\Column(type="string") 
    */ 
    protected $addressZipCode; 

    /** 
    * @var string 
    * @ORM\Column(type="string") 
    */ 
    protected $addressCountryIso; 

    public function setAddress(Address $address) 
    { 
     $this->address = $address; 
    } 

    /** 
    * @ORM\PreUpdate 
    * 
    * set $addressZipCode and $addressCountryInfo when this object is to 
    * save so that doctrine can easily save these scalar values 
    */ 
    public function extractAddress() 
    { 
     $this->addressZipCode = $this->address->getZipCode(); 
     $this->addressCountryIso = $this->address->getAddressCountryIso(); 
    } 

    /** 
    * @ORM\PostLoad 
    * 
    * When the row is hydrated into this class, 
    * $address is not set because that isn't mapped. 
    * so simply, map it manually 
    */ 
    public function packAddress() 
    { 
     $this->address = new Address(); 
     $this->address->setZipCode($this->addressZipCode); 
     $this->address->setCountryIs($this->addressCountryIso); 
    } 
} 
1

Worum Sie bitten, heißt Value Objects.

Es gab ein offenes Problem in Jira DDC-93, um Unterstützung hinzuzufügen. Es ist aktuell als gelöst in Version 2.5 markiert, die gerade in der Beta veröffentlicht wurde.

Verwandte Themen