タイトルのように、既存のテーブルからEntityを作成したい場合は、どうするのかなーと思ってた所、こんな記事がありました。→ How to generate Entities from an Existing Database
Symfony2の記事を書いてます。他のブログもよろ〜
php app/console doctrine:database:create
php app/console doctrine:generate:entity --entity="AcmeStoreBundle:Product" --fields="name:string(255) price:float description:text"
php app/console doctrine:schema:update --force
php app/console doctrine:generate:entities Acme
use Acme\StoreBundle\Entity\Product;
use Symfony\Component\HttpFoundation\Response;
public function createAction()
{
// Productインスタンスの作成
$product = new Product();
// 値をセット
$product->setName('A Foo Bar');
$product->setPrice('19.99');
$product->setDescription('Lorem ipsum dolor');
// EntityManager オブジェクトの取得
$em = $this->getDoctrine()->getEntityManager();
// データの保存
$em->persist($product);
$em->flush();
return new Response('Created product id '.$product->getId());
}
$product = $this->getDoctrine()
->getRepository('AcmeStoreBundle:Product')
->find($id);
// IDからデータを取得
$product = $repository->find($id);
// 指定したカラムの値からデータを1つ取得
$product = $repository->findOneById($id);
$product = $repository->findOneByName('foo');
// 同様に、指定したカラムの値からデータを取得 (複数)
$products = $repository->findByPrice(19.99);
// 細かい検索条件を付ける場合は以下 (findOneByは1つ、findByは複数取得)
$product = $repository->findOneBy(array('name' => 'foo', 'price' => 19.99));
$product = $repository->findBy(
array('name' => 'foo'),
array('price' => 'ASC')
);
// 全て取得
$products = $repository->findAll();
// 自分でクエリーを生成することも簡単
$em = $this->getDoctrine()->getEntityManager();
$query = $em->createQuery(
'SELECT p FROM AcmeStoreBundle:Product p WHERE p.price > :price ORDER BY p.price ASC'
)->setParameter('price', '19.99');
$products = $query->getResult();
// クエリービルダーを使用することもできる (createQueryBuilder)
$repository = $this->getDoctrine()
->getRepository('AcmeStoreBundle:Product');
$query = $repository->createQueryBuilder('p')
->where('p.price > :price')
->setParameter('price', '19.99')
->orderBy('p.price', 'ASC')
->getQuery();
$products = $query->getResult();
public function updateAction($id)
{
// データを取得
$em = $this->getDoctrine()->getEntityManager();
$product = $em->getRepository('AcmeStoreBundle:Product')->find($id);
if (!$product) {
throw $this->createNotFoundException('No product found for id '.$id);
}
// データに値をセット
$product->setName('New product name!');
// データを更新
// $productの中身を変更して、そのまま flush() すれば更新される。(参照渡しでエンティティを取得しているっぽいね。)
// よって、persist() とかいちいち呼ぶ必要が無いです。
$em->flush();
// 削除はこうやる
$em->remove($product);
$em->flush();
return $this->redirect($this->generateUrl('homepage'));
}
// src/Acme/StoreBundle/Entity/Product.php
namespace Acme\StoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity(repositoryClass="Acme\StoreBundle\Repository\ProductRepository")
*/
class Product
{
//...
}
php app/console doctrine:generate:entities Acme
// src/Acme/StoreBundle/Repository/ProductRepository.php
namespace Acme\StoreBundle\Repository;
use Doctrine\ORM\EntityRepository;
class ProductRepository extends EntityRepository
{
public function findAllOrderedByName()
{
return $this->getEntityManager()
->createQuery('SELECT p FROM AcmeStoreBundle:Product p ORDER BY p.name ASC')
->getResult();
}
}
[parameters]
database_driver="pdo_mysql"
database_host="localhost"
database_name="hoge"
database_user="moge"
database_password="piyo"
php app/console doctrine:database:create
php app/console doctrine:generate:entity --entity="AcmeStoreBundle:Product" --fields="name:string(255) price:float description:text"
// src/Acme/StoreBundle/Entity/Product.php
namespace Acme\DemoBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Acme\DemoBundle\Entity\Product
*
* @ORM\Table()
* @ORM\Entity
*/
class Product
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var float $price
*
* @ORM\Column(name="price", type="float")
*/
private $price;
/**
* @var text $description
*
* @ORM\Column(name="description", type="text")
*/
private $description;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*/
public function setName($name)
{
$this->name = $name;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set price
*
* @param float $price
*/
public function setPrice($price)
{
$this->price = $price;
}
/**
* Get price
*
* @return float
*/
public function getPrice()
{
return $this->price;
}
/**
* Set description
*
* @param text $description
*/
public function setDescription($description)
{
$this->description = $description;
}
/**
* Get description
*
* @return text
*/
public function getDescription()
{
return $this->description;
}
}
/** @Column(name="`number`", type="integer") */ private $number;のようにエスケープが必要らしいです。(Reserved SQL keywords documentation より)
php app/console doctrine:schema:update --force