2013-10-10 9 views
12

Ho bisogno di spostare alcuni campi immutabili in una classe separata, ma in realtà non voglio usare "join", perché ho bisogno di tutti i dati insieme ogni volta.Doctrine2 sposta i campi immutabili nella classe separata

È possibile avere alcuni attributi di entità come classi associate alla stessa tabella?

Qualcosa di simile:

/** 
* @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; 
    ... 
} 

E la struttura della tabella sarebbe:

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

Probabilmente [qui] (http://stackoverflow.com/questions/8440879/doctrine-2-value-objects) è possibile trovare la risposta a questa domanda. –

+1

Mille grazie, questo è esattamente quello che sto cercando –

risposta

0

Se oggetto archivio whant senza registrazione:

/** 
* @ORM\Column(name="adress", type="object") 
*/ 

Sarà serializzare/deserializzare automaticamente a una text

http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/types.html

E aggiungere setter con il tipo che si whant di memorizzare

public function setAdress(Address $adress) 
{ 
    $this->adress = $adress; 

    return $this; 
} 

classe Indirizzo non avrà bisogno di alcun annotazioni @ORM

0

Sarà proprio come quella che hai detto qualcosa di simile.

Aggiungi @PreUpdate e @PostLoad hook in User.

/** 
* @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

Quello che stai chiedendo è chiamato Value Objects.

C'è stato un problema aperto in Jira DDC-93 per aggiungere supporto. È attualmente contrassegnato come Risolto nella versione 2.5, che è stato appena rilasciato in Beta.

Problemi correlati