Symfony2「blogチュートリアル(4) テーブルスキーマとエンティティクラス」
2011年12月12日Symfony2「blogチュートリアル(4) テーブルスキーマとエンティティクラス」
Postモデルの作成
今回は、Doctrineを利用したモデルの作成
【モデルの作成】
generate:dctrine:entityコマンドで、モデルを生成
モデルの生成
$ php app/console generate:doctrine:entity –entity=MyBlogBundle:Post –format=annotation –fields=”title:string(255) body:text createdAt:datetime updatedAt:datetime”
生成結果
datetime updatedAt:datetime”
Xdebug requires Zend Engine API version 220060519.
The Zend Engine API version 220090626 which is installed, is newer.
Contact Derick Rethans at http://xdebug.org/docs/faq#api for a later version of Xdebug.Welcome to the Doctrine2 entity generator
This command helps you generate Doctrine2 entities.
First, you need to give the entity name you want to generate.
You must use the shortcut notation like AcmeBlogBundle:Post.The Entity shortcut name [MyBlogBundle:Post]:
Determine the format to use for the mapping information.
Configuration format (yml, xml, php, or annotation) [annotation]:
Instead of starting with a blank entity, you can add some fields now.
Note that the primary key will be added automatically (named id).Available types: array, object, boolean, integer, smallint,
bigint, string, text, datetime, datetimetz, date, time, decimal, float.New field name (press
to stop adding fields): Do you want to generate an empty repository class [no]?
Summary before generation
You are going to generate a “MyBlogBundle:Post” Doctrine2 entity
using the “annotation” format.Do you confirm generation [yes]?
Entity generation
Generating the entity code: OK
You can now start using the generated code!
生成されたPost.php
$ vim src/My/BlogBundle/Entity/Post.php
<?php
namespace My\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* My\BlogBundle\Entity\Post
*
* @ORM\Table()
* @ORM\Entity
*/
class Post
{
/**
* @var integer $id
*
* @ORM\Column(name=”id”, type=”integer”)
* @ORM\Id
* @ORM\GeneratedValue(strategy=”AUTO”)
*/
private $id;/** * @var string $title * * @ORM\Column(name="title", type="string", length=255) */ private $title; /** * @var text $body * * @ORM\Column(name="body", type="text") */ private $body; /** * @var datetime $createdAt * * @ORM\Column(name="createdAt", type="datetime") */ private $createdAt; /** * @var datetime $updatedAt * * @ORM\Column(name="updatedAt", type="datetime") */ private $updatedAt; /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set title * * @param string $title */ public function setTitle($title) { $this->title = $title; } /** * Get title * * @return string */ public function getTitle() { return $this->title; } /** * Set body * * @param text $body */ public function setBody($body) { $this->body = $body; } /** * Get body * * @return text */ public function getBody() { return $this->body; } /** * Set createdAt * * @param datetime $createdAt */ public function setCreatedAt($createdAt) { $this->createdAt = $createdAt; } /** * Get createdAt * * @return datetime */ public function getCreatedAt() { return $this->createdAt; } /** * Set updatedAt * * @param datetime $updatedAt */ public function setUpdatedAt($updatedAt) { $this->updatedAt = $updatedAt; } /** * Get updatedAt * * @return datetime */ public function getUpdatedAt() { return $this->updatedAt; }
}
【スキーマの生成】
先ほど生成されたエンティティクラスのアノテーションに記載されたフィールド情報を元に、スキーマを生成
スキーマ生成
$ php app/console doctrine:schema:create
生成結果
Xdebug requires Zend Engine API version 220060519.
The Zend Engine API version 220090626 which is installed, is newer.
Contact Derick Rethans at http://xdebug.org/docs/faq#api for a later version of Xdebug.ATTENTION: This operation should not be executed in a production environment.
Creating database schema…
Database schema created successfully!
結果確認
$ mysql -p
mysql> use blogsymfony2;
mysql> show tables;
+————————+
| Tables_in_blogsymfony2 |
+————————+
| Post |
+————————+
1 row in set (0.00 sec)mysql> show columns from Post;
+———–+————–+——+—–+———+—————-+
| Field | Type | Null | Key | Default | Extra |
+———–+————–+——+—–+———+—————-+
| id | int(11) | NO | PRI | NULL | auto_increment |
| title | varchar(255) | NO | | NULL | |
| body | longtext | NO | | NULL | |
| createdAt | datetime | NO | | NULL | |
| updatedAt | datetime | NO | | NULL | |
+———–+————–+——+—–+———+—————-+
5 rows in set (0.00 sec)