pele_mele:stack_exchange:stack_overflow:27972741
Différences
Ci-dessous, les différences entre deux révisions de la page.
| Les deux révisions précédentesRévision précédente | |||
| pele_mele:stack_exchange:stack_overflow:27972741 [2024/11/23 04:02] – supprimée - modification externe (Date inconnue) 127.0.0.1 | pele_mele:stack_exchange:stack_overflow:27972741 [2024/11/23 04:02] (Version actuelle) – ↷ Nom de la page changé de pele_mele:stack_exchange:stack_overflow:stackoverflow-27972741 à pele_mele:stack_exchange:stack_overflow:27972741 alexis | ||
|---|---|---|---|
| Ligne 1: | Ligne 1: | ||
| + | ====== Is there a better way to handle the Doctrine proxy object ====== | ||
| + | I have two classes linked with a one-to-one relation. | ||
| + | <code php> | ||
| + | class Client { | ||
| + | ... | ||
| + | /** | ||
| + | * @ORM\OneToOne(targetEntity=" | ||
| + | * @ORM\JoinColumn(name=" | ||
| + | */ | ||
| + | private $info; | ||
| + | ... | ||
| + | public function doSomething() { | ||
| + | if (!$this-> | ||
| + | return false; | ||
| + | } | ||
| + | return $this-> | ||
| + | } | ||
| + | ... | ||
| + | } | ||
| + | |||
| + | class ClientInfo { | ||
| + | ... | ||
| + | /** | ||
| + | * @ORM\Id | ||
| + | * @ORM\OneToOne(targetEntity=" | ||
| + | * @ORM\JoinColumn(name=" | ||
| + | */ | ||
| + | private $client; | ||
| + | ... | ||
| + | public function doSomething() { | ||
| + | return ' | ||
| + | } | ||
| + | ... | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | Those classes are loaded with database content with Doctrine. It is working perfectly when there is data in the database. But if there is not ClientInfo data, I have a // | ||
| + | |||
| + | So I changed the doSomething() method to take this into account. | ||
| + | |||
| + | <code php> | ||
| + | public function doSomething() { | ||
| + | if (!$this-> | ||
| + | return false; | ||
| + | } | ||
| + | try { | ||
| + | return $this-> | ||
| + | } catch (\Doctrine\ORM\EntityNotFoundException $e) { | ||
| + | return false; | ||
| + | } | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | But it does not feel right to me since it is tied with // | ||
| + | |||
| + | Is there a better way of doing that? | ||
| + | |||
| + | **EDIT 1** | ||
| + | |||
| + | I followed Nico Kaag suggestion but it does not change anything. | ||
| + | |||
| + | My constructor in my Client class look like this: | ||
| + | |||
| + | <code php> | ||
| + | public function __construct() { | ||
| + | $this-> | ||
| + | } | ||
| + | </ | ||
| + | |||
| + | If I do a '' | ||
| + | |||
| + | < | ||
| + | object(Proxies\__CG__\MyBundle\Entity\ClientInfo)[444] | ||
| + | public ' | ||
| + | object(Closure)[461] | ||
| + | public ' | ||
| + | object(Closure)[462] | ||
| + | public ' | ||
| + | private ' | ||
| + | ... | ||
| + | </ | ||
| + | |||
| + | **EDIT 2** | ||
| + | |||
| + | I finally changed what I have done. I removed the try..catch block and change the query to retrieve objects from database. Now I force the query to retrieve the // | ||
| + | |||
| + | This way, I can trust my test and if I forget to query both objects simultaneously, | ||
| + | |||
| + | <WRAP help> | ||
| + | See I have made classes for you. | ||
| + | |||
| + | <code php> | ||
| + | use Doctrine\ORM\Mapping AS ORM; | ||
| + | |||
| + | /** | ||
| + | * @ORM\Entity | ||
| + | */ | ||
| + | class client | ||
| + | { | ||
| + | /** | ||
| + | * @ORM\Id | ||
| + | * @ORM\Column(type=" | ||
| + | * @ORM\GeneratedValue(strategy=" | ||
| + | */ | ||
| + | private $id; | ||
| + | |||
| + | /** | ||
| + | * @ORM\OneToOne(targetEntity=" | ||
| + | * @ORM\JoinColumn(name=" | ||
| + | */ | ||
| + | private $clientInfo; | ||
| + | } | ||
| + | </ | ||
| + | <code php> | ||
| + | use Doctrine\ORM\Mapping AS ORM; | ||
| + | |||
| + | /** | ||
| + | * @ORM\Entity | ||
| + | */ | ||
| + | class client_info | ||
| + | { | ||
| + | /** | ||
| + | * @ORM\Id | ||
| + | * @ORM\Column(type=" | ||
| + | * @ORM\GeneratedValue(strategy=" | ||
| + | */ | ||
| + | private $id; | ||
| + | |||
| + | /** | ||
| + | * @ORM\OneToOne(targetEntity=" | ||
| + | */ | ||
| + | private $client; | ||
| + | } | ||
| + | </ | ||
| + | Try this, you will not get such issue. | ||
| + | |||
| + | Also I have used bi-directional relation with cardinality one-to-one, parent connection 0:1*- (parent optional), please see the diagram. | ||
| + | |||
| + | {{: | ||
| + | |||
| + | Suggetion : Use ORM designer tool for designing and extracting entity classes. | ||
| + | </ | ||
| + | <WRAP info> | ||
| + | [[https:// | ||
| + | </ | ||
