Symfony2で利用されているDoctrineについて
今回からは、Symfonyで利用されているORMである Doctrine について紹介したいと思います。
Doctrineとは、Symfonyで利用されているORMです。つまり、DBから取得した情報を、特定のクラスと関連付けてオブジェクト化する仕組みです。この仕組みにより、直接オブジェクトとして保存や取得することができます。
以下がDoctrineを使ったCreate(生成)、Read(読み取り)、Update(更新)、Delete(削除)の簡単な例です。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | <span class="token comment">/** </span></code><code class=" language-php"><span class="token comment"> *</span></code><code class=" language-php"><span class="token comment"> @ORM\Entity</span></code><code class=" language-php"> </code><code class=" language-php"><span class="token comment"> * @ORM\Table(name="user") */</span> <span class="token keyword">class</span> <span class="token class-name">User</span> <span class="token punctuation">{</span> <span class="token comment">/** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */</span> <span class="token keyword">private</span> <span class="token variable">$id</span><span class="token punctuation">;</span> <span class="token comment">/** * @var string */</span> <span class="token keyword">private</span> <span class="token variable">$name</span><span class="token punctuation">;</span> <span class="token comment">/** * @var string */</span> <span class="token keyword">private</span> <span class="token variable">$email</span><span class="token punctuation">;</span> <span class="token comment">/** * @var string */</span> <span class="token keyword">private</span> <span class="token variable">$password</span><span class="token punctuation">;</span> <span class="token comment">/** * @ORM\Column(type="datetime") */</span> <span class="token keyword">private</span> <span class="token variable">$createdAt</span><span class="token punctuation">;</span> <span class="token comment">/** * @ORM\ManyToOne(targetEntity="Group", inversedBy="users") * @ORM\JoinColumn(name="group_id", referencedColumnName="id") */</span> <span class="token keyword">private</span> <span class="token variable">$group</span><span class="token punctuation">;</span> <span class="token comment">/** * @return mixed */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">id</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">/** * @param mixed $id */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">setId</span><span class="token punctuation">(</span><span class="token variable">$id</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">id</span> <span class="token operator">=</span> <span class="token variable">$id</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">/** * @return mixed */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">name</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">/** * @param mixed $name */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">setName</span><span class="token punctuation">(</span><span class="token variable">$name</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">name</span> <span class="token operator">=</span> <span class="token variable">$name</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">/** * @return mixed */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">getEmail</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">email</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">/** * @param mixed $email */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">setEmail</span><span class="token punctuation">(</span><span class="token variable">$email</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">email</span> <span class="token operator">=</span> <span class="token variable">$email</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">/** * @return mixed */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">getPassword</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">password</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">/** * @param mixed $password */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">setPassword</span><span class="token punctuation">(</span><span class="token variable">$password</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">password</span> <span class="token operator">=</span> <span class="token variable">$password</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">/** * @return mixed */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">getCreatedAt</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">createdAt</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">/** * @param mixed $createdAt */</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">setCreatedAt</span><span class="token punctuation">(</span><span class="token variable">$createdAt</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">createdAt</span> <span class="token operator">=</span> <span class="token variable">$createdAt</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">} </span> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <span class="token comment">// src/AppBundle/Entity/Group.php</span> <span class="token comment">/** * @ORM\Entity * @ORM\Table(name="group") */</span> <span class="token keyword">class</span> <span class="token class-name">Group</span> <span class="token punctuation">{</span> <span class="token comment">/** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */</span> <span class="token keyword">private</span> <span class="token variable">$id</span><span class="token punctuation">;</span> <span class="token comment">/** * @var string */</span> <span class="token keyword">private</span> <span class="token variable">$name</span><span class="token punctuation">;</span> <span class="token comment">/** * @ORM\Column(nullable=true) * @ORM\Column(type="text") */</span> <span class="token keyword">private</span> <span class="token variable">$description</span><span class="token punctuation">;</span> <span class="token comment">/** * @ORM\OneToMany(targetEntity="User", mappedBy="group") */</span> <span class="token keyword">private</span> <span class="token variable">$users</span><span class="token punctuation">;</span> <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function">__construct</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span> <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token property">users</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ArrayCollection</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token punctuation">}</span> |
Create(生成)
1 2 3 4 5 6 7 8 9 10 11 | <span class="token variable">$group</span> <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Group</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$group</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">setName</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'Administrator'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$group</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">setDescription</span><span class="token punctuation">(</span><span class="token single-quoted-string string">'Users with highest privileges in the system'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token variable">$em</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">getDoctrine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">getManager</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Tells Doctrine you want to save the group (no queries yet)</span> <span class="token variable">$em</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">persist</span><span class="token punctuation">(</span><span class="token variable">$group</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// This line actually executes the SQL query</span> <span class="token variable">$em</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> |
Idを調べる
1 | <span class="token variable">$group</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">getId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> |
データーベースからデーターを調べる
1 2 3 | <span class="token variable">$group</span> <span class="token operator">=</span> <span class="token variable">$this</span><span class="token operator">-</span><span class="token operator">></span><span class="token function">getDoctrine</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span><span class="token function">getRepository</span><span class="token punctuation">(</span>'AppBundle<span class="token punctuation">:</span>Group<span class="token punctuation">)</span> <span class="token operator">-</span><span class="token operator">></span><span class="token function">find</span><span class="token punctuation">(</span><span class="token variable">$groupId</span><span class="token punctuation">)</span><span class="token punctuation">;</span> |
データーベースのデーターをUpdate(更新)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <span class="token comment">// Fetch the group</span> $em <span class="token operator">=</span> $this<span class="token punctuation">-></span><span class="token function">getDoctrine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">-></span><span class="token function">getManager</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> $group <span class="token operator">=</span> $em<span class="token punctuation">-></span><span class="token function">getRepository</span><span class="token punctuation">(</span><span class="token lifetime-annotation symbol">'AppBundle:Group)-</span><span class="token operator">></span><span class="token function">find</span><span class="token punctuation">(</span>$groupId<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// If the group does not exist, throw an exception</span> <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token operator">!</span>$group<span class="token punctuation">)</span> <span class="token punctuation">{</span> throw $this<span class="token punctuation">-></span><span class="token function">createNotFoundException</span><span class="token punctuation">(</span> <span class="token lifetime-annotation symbol">'Group</span> not found' <span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token punctuation">}</span> <span class="token comment">// Update data</span> $group<span class="token operator">></span><span class="token function">setDescription</span><span class="token punctuation">(</span><span class="token lifetime-annotation symbol">'New</span> group description<span class="token punctuation">.</span><span class="token lifetime-annotation symbol">');</span> $em<span class="token punctuation">-></span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> |
データーベースからデーターをDelete(削除)
1 2 3 4 5 6 7 | <span class="token comment">// Fetch group</span> $em <span class="token operator">=</span> $this<span class="token punctuation">-></span><span class="token function">getDoctrine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">-></span><span class="token function">getManager</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> $group <span class="token operator">=</span> $em<span class="token punctuation">-></span><span class="token function">getRepository</span><span class="token punctuation">(</span><span class="token lifetime-annotation symbol">'AppBundle:Group)-</span><span class="token operator">></span><span class="token function">find</span><span class="token punctuation">(</span>$groupId<span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// Remove group</span> $em<span class="token punctuation">-></span><span class="token function">remove</span><span class="token punctuation">(</span>$group<span class="token punctuation">)</span><span class="token punctuation">;</span> $em<span class="token punctuation">-></span><span class="token function">flush</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> |
Doctrineで情報取得するまでの流れは、以下のようになっています。
- アプリからDoctrineを呼び出し、情報取得の依頼をする。
- データーベースの情報が記述されているマッピング定義を読み込む。
- Doctrineがデーターベースにアクセスし、リクエストに対応する情報を取得する。
- マッピング定義 から、リクエストに対応するエンティティクラス を特定し、読み込む。
- データーベースから取得した情報をプロパティに設定し、インスタンス化する。
- インスタンス化したエンティティを呼び出し元に返す。
Author Profile
スターフィールド編集部
SHARE