メモ > 技術 > フレームワーク: Symfony3 > データベースを使う
データベースを使う
データーベースと Doctrine | SymDoc - PHP フレームワーク Symfony3 日本語ドキュメント Wiki
http://symdoc.kwalk.jp/doc/book/doctrine
超入門 Symfony3 : (6) Doctrine ORM【前編】 | シムノート
http://symnote.kwalk.jp/blog/2015-12-12/%E8%B6%85%E5%85%A5%E9%96%80_symfony3_doctrine_orm_%E5%89%8D%...
■データベースを作成
データベースと接続ユーザを作成
$ mysql -u root -p
mysql> GRANT ALL PRIVILEGES ON symfony.* TO webmaster@localhost IDENTIFIED BY '1234';
mysql> FLUSH PRIVILEGES;
mysql> CREATE DATABASE symfony DEFAULT CHARACTER SET utf8mb4;
mysql> QUIT;
■テスト用データを登録
CREATE TABLE products(
id INT UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'ID',
name VARCHAR(255) NOT NULL COMMENT '名前',
price INT NOT NULL COMMENT '価格',
PRIMARY KEY(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT '商品';
INSERT INTO products VALUES(NULL, '商品1', 100);
INSERT INTO products VALUES(NULL, '商品2', 200);
INSERT INTO products VALUES(NULL, '商品3', 300);
SELECT * FROM products;
■Symfonyからデータを扱う
my_project\app\config\parameters.yml
parameters:
database_driver: pdo_mysql
database_host: localhost
database_port: null
database_name: symfony
database_user: webmaster
database_password: 1234
my_project\src\AppBundle\Entity\Product.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="products")
*/
class Product
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=100)
*/
protected $name;
/**
* @ORM\Column(type="integer")
*/
protected $price;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
*
* @return Car
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set price
*
* @param integer $price
*
* @return Product
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price
*
* @return int
*/
public function getPrice()
{
return $this->price;
}
}
my_project\src\AppBundle\Controller\ProductController.php
<?php
namespace AppBundle\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use AppBundle\Entity\Product;
class ProductController extends Controller
{
/**
* @Route("/products/")
*/
public function indexAction()
{
/*
$product = new Product();
$product->setName('商品');
$product->setPrice(100);
$em = $this->getDoctrine()->getManager();
$em->persist($product);
$em->flush();
*/
$products = $this->getDoctrine()->getRepository('AppBundle:Product')->findAll();
return $this->render('products/index.html.twig', ['products' => $products]);
}
}
my_project\app\Resources\views\products\index.html.twig
{% extends 'base.html.twig' %}
{% block body %}
<h1>商品</h1>
<ul>
{% for product in products %}
<li>No.{{ product.id }} / {{ product.name }} / {{ product.price }}円</li>
{% endfor %}
</ul>
{% endblock %}
http://192.168.33.10/app_dev.php/products/