$kernel->loadClassCache();
php app/console generate:bundle --namespace=Demos/BlogBundle
DemosBlogBundle:
resource: "@DemosBlogBundle/Controller/"
type: annotation
prefix: /
<?php
namespace Demos\BlogBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="post")
*/
class Post {
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255)
*/
protected $title;
/**
* @ORM\Column(type="text")
*/
protected $body;
/**
* @ORM\Column(type="datetime")
*/
protected $created_date;
/**
* @ORM\Column(type="datetime")
*/
protected $updated_date;
}
php app/console doctrine:generate:entities Demos/BlogBundle/Entity/Post
php app/console doctrine:schema:update --force
/**
* @Route("/create")
*/
public function createAction() {
$post = new Post();
$post->setTitle('Demo Blog');
$post->setBody('Hello Symfony 2');
$post->setCreatedDate(new \DateTime("now"));
$post->setUpdatedDate(new \DateTime('now'));
$em = $this->getDoctrine()->getEntityManager();
$em->persist($post);
$em->flush();
return new Response('Created product id ' . $post->getId());
}
use Demos\BlogBundle\Entity\Post;
use Symfony\Component\HttpFoundation\Response;
<?php
/**
* @Route("/show/{id}")
*/
public function showAction($id)
{
$post = $this->getDoctrine()->getRepository('DemosBlogBundle:Post')->find($id);
if (!$post) {
throw $this->createNotFoundException(' !');
}
$html = <<<HTML
<h1>{$post->getTitle()}</h1>
<p>{$post->getBody()}</p>
<hr/>
<small> {$post->getCreatedDate()->format("Ymd H:i:s")}</small>
HTML;
return new Response($html);
}
?>
return array('name' => $name);
/**
* @Route("/show/{id}")
* @Template()
*/
public function showAction($id)
{
$post = $this->getDoctrine()->getRepository('DemosBlogBundle:Post')->find($id);
if (!$post) {
throw $this->createNotFoundException(' !');
}
return array('post' => $post);
}
Source: https://habr.com/ru/post/125469/