facebook twitter hatena line email

「Php/Symfony/Symfony2/doctrine/crud」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(ページの作成:「==dbに合わせたcontrollerを作成する(crud)== $ php app/console doctrine:generate:entity --entity="AcmeHelloBundle:Blog" --fields="name:string(255) description:te...」)
 
 
(同じ利用者による、間の11版が非表示)
行1: 行1:
 
==dbに合わせたcontrollerを作成する(crud)==
 
==dbに合わせたcontrollerを作成する(crud)==
 +
entityを作成した後controllerを作成するサンプルを以下に
 +
 +
==entityをまずgenerateで作成==
 
  $ php app/console doctrine:generate:entity --entity="AcmeHelloBundle:Blog" --fields="name:string(255) description:text"
 
  $ php app/console doctrine:generate:entity --entity="AcmeHelloBundle:Blog" --fields="name:string(255) description:text"
  
src/Acme/HelloBundle/Entity/Blog.php
+
作られたphp
 +
 
 +
-src/Acme/HelloBundle/Entity/Blog.php
 
  namespace Acme\HelloBundle\Entity;
 
  namespace Acme\HelloBundle\Entity;
 
  use Doctrine\ORM\Mapping as ORM;
 
  use Doctrine\ORM\Mapping as ORM;
行51: 行56:
 
     {
 
     {
 
         $this->name = $name;
 
         $this->name = $name;
 
 
         return $this;
 
         return $this;
 
     }
 
     }
行72: 行76:
 
     {
 
     {
 
         $this->description = $description;
 
         $this->description = $description;
 
 
         return $this;
 
         return $this;
 
     }
 
     }
行85: 行88:
 
     }
 
     }
 
  }
 
  }
 +
 +
モデルからsql発行
 +
# スキーマ作成時
 +
php app/console doctrine:schema:create
 +
# スキーマ更新時
 +
php app/console doctrine:schema:update  --force
 +
 +
==crudでcontrollerやviewを作成する==
 +
$ php app/console doctrine:generate:crud --entity="AcmeHelloBundle:Blog"
 +
 +
作られた2つのphpと2つのtwig
 +
 +
*src/Acme/HelloBundle/Controller/BlogController.php
 +
*src/Acme/HelloBundle/Tests/Controller/BlogControllerTest.php
 +
*src/Acme/HelloBundle/Resources/views/Blog/index.html.twig
 +
*src/Acme/HelloBundle/Resources/views/Blog/show.html.twig
 +
 +
 +
以下詳細
 +
 +
-src/Acme/HelloBundle/Controller/BlogController.php
 +
namespace Acme\HelloBundle\Controller;
 +
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
 +
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
 +
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
 +
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
 +
use Acme\HelloBundle\Entity\Blog;
 +
/**
 +
  * Blog controller.
 +
  *
 +
  * @Route("/blog")
 +
  */
 +
class BlogController extends Controller
 +
{
 +
    /**
 +
    * Lists all Blog entities.
 +
    *
 +
    * @Route("/", name="blog")
 +
    * @Method("GET")
 +
    * @Template()
 +
    */
 +
    public function indexAction()
 +
    {
 +
        $em = $this->getDoctrine()->getManager();
 +
        $entities = $em->getRepository('AcmeHelloBundle:Blog')->findAll();
 +
        return array(
 +
            'entities' => $entities,
 +
        );
 +
    }
 +
    /**
 +
    * Finds and displays a Blog entity.
 +
    *
 +
    * @Route("/{id}", name="blog_show")
 +
    * @Method("GET")
 +
    * @Template()
 +
    */
 +
    public function showAction($id)
 +
    {
 +
        $em = $this->getDoctrine()->getManager();
 +
        $entity = $em->getRepository('AcmeHelloBundle:Blog')->find($id);
 +
        if (!$entity) {
 +
            throw $this->createNotFoundException('Unable to find Blog entity.');
 +
        }
 +
        return array(
 +
            'entity'      => $entity,
 +
        );
 +
    }
 +
}
 +
 +
-src/Acme/HelloBundle/Tests/Controller/BlogControllerTest.php
 +
namespace Acme\HelloBundle\Tests\Controller;
 +
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
 +
class BlogControllerTest extends WebTestCase
 +
{
 +
    /*
 +
    public function testCompleteScenario()
 +
    {
 +
        // Create a new client to browse the application
 +
        $client = static::createClient();
 +
        // Go to the list view
 +
        $crawler = $client->request('GET', '/blog/');
 +
        $this->assertEquals(200, $client->getResponse()->getStatusCode(), "Unexpected HTTP status code for GET /blog/");
 +
        // Go to the show view
 +
        $crawler = $client->click($crawler->selectLink('show')->link());
 +
        $this->assertEquals(200, $client->getResponse()->getStatusCode(), "Unexpected HTTP status code");
 +
    }
 +
    */
 +
}
 +
 +
-src/Acme/HelloBundle/Resources/views/Blog/index.html.twig
 +
<pre>
 +
{% extends '::base.html.twig' %}
 +
{% block body -%}
 +
    <h1>Blog list</h1>
 +
    <table class="records_list">
 +
        <thead>
 +
            <tr>
 +
                <th>Id</th>
 +
                <th>Name</th>
 +
                <th>Description</th>
 +
                <th>Actions</th>
 +
            </tr>
 +
        </thead>
 +
        <tbody>
 +
        {% for entity in entities %}
 +
            <tr>
 +
                <td><a href="{{ path('blog_show', { 'id': entity.id }) }}">{{ entity.id }}</a></td>
 +
                <td>{{ entity.name }}</td>
 +
                <td>{{ entity.description }}</td>
 +
                <td>
 +
                <ul>
 +
                    <li>
 +
                        <a href="{{ path('blog_show', { 'id': entity.id }) }}">show</a>
 +
                    </li>
 +
                </ul>
 +
                </td>
 +
            </tr>
 +
        {% endfor %}
 +
        </tbody>
 +
    </table>
 +
    {% endblock %}
 +
</pre>
 +
 +
-src/Acme/HelloBundle/Resources/views/Blog/show.html.twig
 +
<pre>
 +
{% extends '::base.html.twig' %}
 +
{% block body -%}
 +
    <h1>Blog</h1>
 +
    <table class="record_properties">
 +
        <tbody>
 +
            <tr>
 +
                <th>Id</th>
 +
                <td>{{ entity.id }}</td>
 +
            </tr>
 +
            <tr>
 +
                <th>Name</th>
 +
                <td>{{ entity.name }}</td>
 +
            </tr>
 +
            <tr>
 +
                <th>Description</th>
 +
                <td>{{ entity.description }}</td>
 +
            </tr>
 +
        </tbody>
 +
    </table>
 +
        <ul class="record_actions">
 +
    <li>
 +
        <a href="{{ path('blog') }}">
 +
            Back to the list
 +
        </a>
 +
    </li>
 +
</ul>
 +
{% endblock %}
 +
</pre>
 +
 +
==ルーティングを手動で追加==
 +
-src/Acme/HelloBundle/Resources/config/routing.yml
 +
blog_show:
 +
    path:    /blog/{id}
 +
    defaults: { _controller: AcmeHelloBundle:Blog:show }
 +
blog:
 +
    path:    /blog
 +
    defaults: { _controller: AcmeHelloBundle:Blog:index }
 +
 +
"blog_show"と"blog"はtwigのpathと同じものを入れる
 +
例:<a href="{{ path('blog_show', { 'id': entity.id }) }}">show</a>
 +
 +
==参考==
 +
http://tech.quartetcom.co.jp/symfony2-intro/2013/12/12/symfony2-intro-2/

2017年1月17日 (火) 16:39時点における最新版

dbに合わせたcontrollerを作成する(crud)

entityを作成した後controllerを作成するサンプルを以下に

entityをまずgenerateで作成

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

作られたphp

-src/Acme/HelloBundle/Entity/Blog.php

namespace Acme\HelloBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
 * Blog
 *
 * @ORM\Table("blogs")
 * @ORM\Entity
 */
class Blog
{
   /**
    * @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 Blog
    */
   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 Blog
    */
   public function setDescription($description)
   {
       $this->description = $description;
       return $this;
   }
   /**
    * Get description
    *
    * @return string
    */
   public function getDescription()
   {
       return $this->description;
   }
}

モデルからsql発行

# スキーマ作成時
php app/console doctrine:schema:create
# スキーマ更新時
php app/console doctrine:schema:update  --force

crudでcontrollerやviewを作成する

$ php app/console doctrine:generate:crud --entity="AcmeHelloBundle:Blog"

作られた2つのphpと2つのtwig

  • src/Acme/HelloBundle/Controller/BlogController.php
  • src/Acme/HelloBundle/Tests/Controller/BlogControllerTest.php
  • src/Acme/HelloBundle/Resources/views/Blog/index.html.twig
  • src/Acme/HelloBundle/Resources/views/Blog/show.html.twig


以下詳細

-src/Acme/HelloBundle/Controller/BlogController.php

namespace Acme\HelloBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Acme\HelloBundle\Entity\Blog;
/**
 * Blog controller.
 *
 * @Route("/blog")
 */
class BlogController extends Controller
{
   /**
    * Lists all Blog entities.
    *
    * @Route("/", name="blog")
    * @Method("GET")
    * @Template()
    */
   public function indexAction()
   {
       $em = $this->getDoctrine()->getManager();
       $entities = $em->getRepository('AcmeHelloBundle:Blog')->findAll();
       return array(
           'entities' => $entities,
       );
   }
   /**
    * Finds and displays a Blog entity.
    *
    * @Route("/{id}", name="blog_show")
    * @Method("GET")
    * @Template()
    */
   public function showAction($id)
   {
       $em = $this->getDoctrine()->getManager();
       $entity = $em->getRepository('AcmeHelloBundle:Blog')->find($id);
       if (!$entity) {
           throw $this->createNotFoundException('Unable to find Blog entity.');
       }
       return array(
           'entity'      => $entity,
       );
   }
}

-src/Acme/HelloBundle/Tests/Controller/BlogControllerTest.php

namespace Acme\HelloBundle\Tests\Controller;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
class BlogControllerTest extends WebTestCase
{
   /*
   public function testCompleteScenario()
   {
       // Create a new client to browse the application
       $client = static::createClient();
       // Go to the list view
       $crawler = $client->request('GET', '/blog/');
       $this->assertEquals(200, $client->getResponse()->getStatusCode(), "Unexpected HTTP status code for GET /blog/");
       // Go to the show view
       $crawler = $client->click($crawler->selectLink('show')->link());
       $this->assertEquals(200, $client->getResponse()->getStatusCode(), "Unexpected HTTP status code");
   }
   */
}

-src/Acme/HelloBundle/Resources/views/Blog/index.html.twig

{% extends '::base.html.twig' %}
{% block body -%}
    <h1>Blog list</h1>
    <table class="records_list">
        <thead>
            <tr>
                <th>Id</th>
                <th>Name</th>
                <th>Description</th>
                <th>Actions</th>
            </tr>
        </thead>
        <tbody>
        {% for entity in entities %}
            <tr>
                <td><a href="{{ path('blog_show', { 'id': entity.id }) }}">{{ entity.id }}</a></td>
                <td>{{ entity.name }}</td>
                <td>{{ entity.description }}</td>
                <td>
                <ul>
                    <li>
                        <a href="{{ path('blog_show', { 'id': entity.id }) }}">show</a>
                    </li>
                </ul>
                </td>
            </tr>
        {% endfor %}
        </tbody>
    </table>
    {% endblock %}

-src/Acme/HelloBundle/Resources/views/Blog/show.html.twig

{% extends '::base.html.twig' %}
{% block body -%}
    <h1>Blog</h1>
    <table class="record_properties">
        <tbody>
            <tr>
                <th>Id</th>
                <td>{{ entity.id }}</td>
            </tr>
            <tr>
                <th>Name</th>
                <td>{{ entity.name }}</td>
            </tr>
            <tr>
                <th>Description</th>
                <td>{{ entity.description }}</td>
            </tr>
        </tbody>
    </table>
        <ul class="record_actions">
    <li>
        <a href="{{ path('blog') }}">
            Back to the list
        </a>
    </li>
</ul>
{% endblock %}

ルーティングを手動で追加

-src/Acme/HelloBundle/Resources/config/routing.yml

blog_show:
   path:     /blog/{id}
   defaults: { _controller: AcmeHelloBundle:Blog:show }
blog:
   path:     /blog
   defaults: { _controller: AcmeHelloBundle:Blog:index }

"blog_show"と"blog"はtwigのpathと同じものを入れる

例:<a href="{{ path('blog_show', { 'id': entity.id }) }}">show</a>

参考

http://tech.quartetcom.co.jp/symfony2-intro/2013/12/12/symfony2-intro-2/