2011年10月17日月曜日

Model (Doctrine) について①

Doctrine は, PHPのORM (Object-relational mapping : オブジェクト指向言語におけるオブジェクトと、MySQLやPostgreSQLの様な、リレーショナルデータベースにおけるレコードとを対照させるもの) で、Symfonyで利用されている。

詳しくは、Databases and Doctrine ("The Model")を参照してください。

データベースの情報を設定

まず、データベースに接続するためには, app/config/parameters.ini でホストなどを指定する。

  1. [parameters]  
  2.     database_driver="pdo_mysql"  
  3.     database_host="localhost"  
  4.     database_name="hoge"  
  5.     database_user="moge"  
  6.     database_password="piyo"  

以下のコマンドを叩けば、app/config/parameters.ini の情報に基づいてデータベースを作成してくれる。
php app/console doctrine:database:create

Entity Class の作成

エンティティを、src/Acme/StoreBundle/Entity/ のような場所 (〇〇Bundle/Entity下) に作成する。(このクラス自体は、データを格納するだけ)

まず、以下のコマンドを実行する。
php app/console doctrine:generate:entity --entity="AcmeStoreBundle:Product" --fields="name:string(255) price:float description:text"

すると、ORM情報をアノテーションとして設定されているエンティティクラスが作成される (以下)。
また、Getter と Setter も用意してくれます。

  1. // src/Acme/StoreBundle/Entity/Product.php  
  2. namespace Acme\DemoBundle\Entity;  
  3.   
  4. use Doctrine\ORM\Mapping as ORM;  
  5.   
  6. /** 
  7.  * Acme\DemoBundle\Entity\Product 
  8.  * 
  9.  * @ORM\Table() 
  10.  * @ORM\Entity 
  11.  */  
  12. class Product  
  13. {  
  14.     /** 
  15.      * @var integer $id 
  16.      * 
  17.      * @ORM\Column(name="id", type="integer") 
  18.      * @ORM\Id 
  19.      * @ORM\GeneratedValue(strategy="AUTO") 
  20.      */  
  21.     private $id;  
  22.   
  23.     /** 
  24.      * @var string $name 
  25.      * 
  26.      * @ORM\Column(name="name", type="string", length=255) 
  27.      */  
  28.     private $name;  
  29.   
  30.     /** 
  31.      * @var float $price 
  32.      * 
  33.      * @ORM\Column(name="price", type="float") 
  34.      */  
  35.     private $price;  
  36.   
  37.     /** 
  38.      * @var text $description 
  39.      * 
  40.      * @ORM\Column(name="description", type="text") 
  41.      */  
  42.     private $description;  
  43.   
  44.   
  45.     /** 
  46.      * Get id 
  47.      * 
  48.      * @return integer  
  49.      */  
  50.     public function getId()  
  51.     {  
  52.         return $this->id;  
  53.     }  
  54.   
  55.     /** 
  56.      * Set name 
  57.      * 
  58.      * @param string $name 
  59.      */  
  60.     public function setName($name)  
  61.     {  
  62.         $this->name = $name;  
  63.     }  
  64.   
  65.     /** 
  66.      * Get name 
  67.      * 
  68.      * @return string  
  69.      */  
  70.     public function getName()  
  71.     {  
  72.         return $this->name;  
  73.     }  
  74.   
  75.     /** 
  76.      * Set price 
  77.      * 
  78.      * @param float $price 
  79.      */  
  80.     public function setPrice($price)  
  81.     {  
  82.         $this->price = $price;  
  83.     }  
  84.   
  85.     /** 
  86.      * Get price 
  87.      * 
  88.      * @return float  
  89.      */  
  90.     public function getPrice()  
  91.     {  
  92.         return $this->price;  
  93.     }  
  94.   
  95.     /** 
  96.      * Set description 
  97.      * 
  98.      * @param text $description 
  99.      */  
  100.     public function setDescription($description)  
  101.     {  
  102.         $this->description = $description;  
  103.     }  
  104.   
  105.     /** 
  106.      * Get description 
  107.      * 
  108.      * @return text  
  109.      */  
  110.     public function getDescription()  
  111.     {  
  112.         return $this->description;  
  113.     }  
  114. }  

ORM情報をYAML等のコンフィグファイルで設定したい場合は、--format=yml のオプションを加えることで可能。(その場合のコンフィグファイルは、src/Acme/StoreBundle/Resources/config/doctrine/Product.orm.yml となる)

ちなみに、SQLのキーワードとなってる名前をフィールドにつけようとすると、エラーとなるらしい。その場合は、
  1. /** @Column(name="`number`", type="integer") */  
  2. private $number;  
のようにエスケープが必要らしいです。(Reserved SQL keywords documentation より)


テーブル/スキーマの作成

次の以下のコマンドを実行すれば、自動的にテーブル/スキーマの作成を行なってくれる。
php app/console doctrine:schema:update --force

とりあえず、今回はココまでっ!!

0 コメント:

コメントを投稿

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites More