facebook twitter hatena line email

Php/Symfony/Symfony2/doctrine/基本

提供: 初心者エンジニアの簡易メモ
2016年12月22日 (木) 14:39時点におけるAdmin (トーク | 投稿記録)による版 (ページの作成:「==db設定== app/config/parameters.ini parameters database_driver = pdo_mysql database_host = localhost database_name = project1 database_user...」)

(差分) ←前の版 | 最新版 (差分) | 次の版→ (差分)
移動: 案内検索

db設定

app/config/parameters.ini
parameters
   database_driver   = pdo_mysql
   database_host     = localhost
   database_name     = project1
   database_user     = user1
   database_password = *****

dbのschemaを作る

php app/console doctrine:database:create

or

create database project1;

テーブルクラス作成

-Acme/HelloBundle/Entity/User.php

namespace Acme\HelloBundle\Entity;
class User
{
   protected $name;
   protected $description;
}

generateでテーブルクラスを作った場合

dbとのマッピング処理などが追加された記述をして作成される

$ php app/console doctrine:generate:entity --entity="AcmeHelloBundle:User" --fields="name:string(255) description:text"

-Acme/HelloBundle/Entity/User.php

namespace Acme\HelloBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
 * User
 *
 * @ORM\Table()
 * @ORM\Entity
 */
class User
{
   /**
    * @var integer
    *
    * @ORM\Column(name="id", type="integer")
    * @ORM\Id
    * @ORM\GeneratedValue(strategy="AUTO")
    */
   private $id;
   /**
    * @var string
    *
    * @ORM\Column(name="name", type="string", length=255)
    */
   private $name;
   /**
    * @var string
    *
    * @ORM\Column(name="description", type="text")
    */
   private $description;
   
   /**
    * Get id
    *
    * @return integer-
    */
   public function getId()
   {
       return $this->id;
   }
   /**
    * Set name
    *
    * @param string $name
    * @return User2
    */
   public function setName($name)
   {
       $this->name = $name;
       return $this;
   }
   /**
    * Get name
    *
    * @return string-
    */
   public function getName()
   {
       return $this->name;
   }
   /**
    * Set description
    *
    * @param string $description
    * @return User2
    */
   public function setDescription($description)
   {
       $this->description = $description;
       return $this;
   }
   /**
    * Get description
    *
    * @return string-
    */
   public function getDescription()
   {
       return $this->description;
   }
}

generateしたテーブルクラスからdb側のtableを作る

$ php app/console doctrine:schema:update --force
CREATE TABLE `User2` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
 `description` longtext COLLATE utf8_unicode_ci NOT NULL,
 PRIMARY KEY (`id`)

) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

insert処理

$user = new \Acme\HelloBundle\Entity\User();
$user->setName('taro');
$user->setDescription('太郎です');
$em = $this->getDoctrine()->getEntityManager();
$em->persist($user);
$em->flush(); // ここでinsert処理がされる
select * from User;
+----+-----------+-------------------+
| id | name      | description       |
+----+-----------+-------------------+
|  1 | taro      | 太郎です          |
+----+-----------+-------------------+

select処理

$repository = $this->getDoctrine()
   ->getRepository('AcmeHelloBundle:User');
$user = $repository->find(1); // userクラスにデータを入れて取得
if ($user) {
   echo $user->getId(); // taro
   echo $user->getName(); // 太郎です
}

参考

http://docs.symfony.gr.jp/symfony2/book/doctrine.html