「Php/Symfony/Symfony2/doctrine/crud」の版間の差分
ナビゲーションに移動
検索に移動
編集の要約なし |
編集の要約なし |
||
| 88行目: | 88行目: | ||
} | } | ||
} | } | ||
==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 %} | |||
2017年1月16日 (月) 14:37時点における版
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;
}
}
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 -%}
Blog list
| Id | Name | Description | Actions |
|---|---|---|---|
| <a href="{{ path('blog_show', { 'id': entity.id }) }}">テンプレート:Entity.id</a> | テンプレート:Entity.name | テンプレート:Entity.description |
|
{% endblock %}