php app/console generate:bundle --namespace=Test/NewsBundle --format=annotation --structure
SET FOREIGN_KEY_CHECKS=0; CREATE TABLE `news` ( `id` INT NOT NULL AUTO_INCREMENT , `news_category_id` INT NOT NULL , `title` VARCHAR(255) NULL , `announce` TEXT NULL , `text` TEXT NULL , `pub_date` DATE NULL , PRIMARY KEY (`id`) , INDEX `pub_date` (`pub_date` ASC) , INDEX `fk_news_news_category` (`news_category_id` ASC) , CONSTRAINT `fk_news_news_category` FOREIGN KEY (`news_category_id` ) REFERENCES `news_category` (`id` ) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB; CREATE TABLE `news_category` ( `id` INT NOT NULL AUTO_INCREMENT , `name` VARCHAR(255) NULL , PRIMARY KEY (`id`) ) ENGINE = InnoDB CREATE TABLE `news_link` ( `id` int(11) NOT NULL AUTO_INCREMENT, `news_id` INT NOT NULL , `url` varchar(255) DEFAULT NULL, `text` varchar(255) DEFAULT NULL, PRIMARY KEY (`id`) , INDEX `fk_news_link_news1` (`news_id` ASC) , CONSTRAINT `fk_news_link_news1` FOREIGN KEY (`news_id` ) REFERENCES `news` (`id` ) ON DELETE NO ACTION ON UPDATE NO ACTION) ENGINE = InnoDB;
php app/console doctrine:mapping:import TestNewsBundle annotation
<?php namespace Test\NewsBundle\Entity; use Doctrine\ORM\Mapping as ORM; /** * Test\NewsBundle\Entity\News * * @ORM\Table(name="news") * @ORM\Entity */ class News { /** * @var integer $id * * @ORM\Column(name="id", type="integer", nullable=false) * @ORM\Id * @ORM\GeneratedValue(strategy="IDENTITY") */ private $id; /** * @var string $title * * @ORM\Column(name="title", type="string", length=255, nullable=true) */ private $title; /** * @var text $announce * * @ORM\Column(name="announce", type="text", nullable=true) */ private $announce; /** * @var text $text * * @ORM\Column(name="text", type="text", nullable=true) */ private $text; /** * @var date $pubDate * * @ORM\Column(name="pub_date", type="date", nullable=true) */ private $pubDate; /** * @var NewsCategory * * @ORM\ManyToOne(targetEntity="NewsCategory") * @ORM\JoinColumns({ * @ORM\JoinColumn(name="news_category_id", referencedColumnName="id") * }) */ private $newsCategory; }
php app/console doctrine:generate:entities TestNewsBundle
php app/console doctrine:generate:crud --entity=TestNewsBundle:News --route-prefix=news --with-write --format=annotation
php app/console doctrine:generate:crud --entity=TestNewsBundle:NewsCategory --route-prefix=newscategory --with-write --format=annotation
php app/console doctrine:generate:crud --entity=TestNewsBundle:NewsLink --route-prefix=newslink --with-write --format=annotation
function __toString() { return $this->getName(); }
<?php namespace Test\NewsBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilder; class NewsType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $builder->add('title', 'text', array('label' => '')) ->add('announce', 'textarea', array('label' => '')) ->add('text', 'textarea', array('label' => '')) ->add('pubDate', null, array('label' => ' ')) ->add('newsCategory', null, array('label' => '')); } public function getName() { return 'news'; } }
{% form_theme form 'form_table_layout.html.twig' %}
{% use 'form_table_layout.html.twig' %} {% form_theme form _self %} <h1> </h1> <form action="{{ path('news_create') }}" method="post" {{ form_enctype(form) }}> {{ form_widget(form) }} <p> <button type="submit"> </button> </p> </form> <ul class="record_actions"> <li> <a href="{{ path('news') }}"> </a> </li> </ul>
/** * @ORM\OneToMany(targetEntity="NewsLink", mappedBy="news", cascade={"all"}) */ protected $newsLinks; function __construct() { $this->newsLinks = new \Doctrine\Common\Collections\ArrayCollection(); }
php app/console doctrine:generate:entities TestNewsBundle
$builder->add('title', 'text', array('label' => '')) .... ->add('newsLinks', 'collection', array( 'label' => ' ', 'type' => new NewsLinkType(), 'allow_add' => true, 'allow_delete' => true, 'prototype' => true ));
<?php namespace Test\NewsBundle\Form; use Symfony\Component\Form\AbstractType; use Symfony\Component\Form\FormBuilder; class NewsLinkType extends AbstractType { public function buildForm(FormBuilder $builder, array $options) { $builder->add('url') ->add('text'); } public function getName() { return 'newsLinkType'; } public function getDefaultOptions(array $options) { return array( 'data_class' => 'Test\NewsBundle\Entity\NewsLink', ); } }
{% use 'form_table_layout.html.twig' %} {% form_theme form _self %} <script language="JavaScript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <form action="{{ entity.id ? path('news_update', { 'id': entity.id }) : path('news_create') }}" method="post" {{ form_enctype(form) }}> {{ form_errors(form) }} <table> {{ form_row (form.title) }} {{ form_row (form.announce) }} {{ form_row (form.text) }} {{ form_row (form.pubDate) }} {{ form_row (form.newsCategory) }} <tr> <td valign="top"> </td> <td> <!-- / --> {% macro linkRow(link) %} <tr> <td>{{ form_widget(link.url) }}</td> <td>{{ form_widget(link.text) }}</td> <td><a href="#" class="deleteRowLink">X</a></td> </tr> {% endmacro %} <!-- / --> <!-- #addLink --> <script type="text/html" id="nl">{{ _self.linkRow (form.newsLinks.get('prototype')) }} </script> <!-- --> <table id="linksTable"> <tr><td>Url</td><td> </td></tr> {% for key, link in form.newsLinks %} {{ _self.linkRow(link) }} {% endfor %} </table> <input type="button" id="addLink" value=" "> <script> $(function() { $("#addLink" ).click(function() { $('#linksTable tbody').append($('#nl').html().replace(/\$\$name\$\$/g, $('#linksTable tbody tr').length)); }); $("form a.deleteRowLink").live('click', function() { $(this).closest('tr').remove(); }); }); </script> </td> </tr> </table> {{ form_rest(form) }} <p><button type="submit"></button></p> </form>
<h1> </h1> {% include 'TestNewsBundle:News:form.html.twig' %} <ul class="record_actions"> <li> <a href="{{ path('news') }}"> </a> </li> </ul>
<h1> </h1> {% include 'TestNewsBundle:News:form.html.twig' with { 'form' : edit_form } %} <ul class="record_actions"> <li> <a href="{{ path('news') }}"> Back to the list </a> </li> <li> <form action="{{ path('news_delete', { 'id': entity.id }) }}" method="post"> {{ form_widget(delete_form) }} <button type="submit">Delete</button> </form> </li> </ul>
public function createAction() { $entity = new News(); $request = $this->getRequest(); $form = $this->createForm(new NewsType(), $entity); $form->bindRequest($request); if ($form->isValid()) { $em = $this->getDoctrine()->getEntityManager(); // foreach ($entity->getNewsLinks() as $link) { $link->setNews($entity); } $em->persist($entity); $em->flush(); return $this->redirect($this->generateUrl('news_show', array('id' => $entity->getId()))); } return array( 'entity' => $entity, 'form' => $form->createView()); }
public function updateAction($id) { $em = $this->getDoctrine()->getEntityManager(); $entity = $em->getRepository('TestNewsBundle:News')->find($id); if (!$entity) { throw $this->createNotFoundException('Unable to find News entity.'); } $beforeSaveLinks = $currentLinkIds = array(); foreach ($entity->getNewsLinks() as $link) $beforeSaveLinks [$link->getId()] = $link; $editForm = $this->createForm(new NewsType(), $entity); $deleteForm = $this->createDeleteForm($id); $request = $this->getRequest(); $editForm->bindRequest($request); if ($editForm->isValid()) { foreach ($entity->getNewsLinks() as $link) { $link->setNews($entity); // - ( id) if ($link->getId()) $currentLinkIds[] = $link->getId(); } $em->persist($entity); // - foreach ($beforeSaveLinks as $linkId => $link) if (!in_array( $linkId, $currentLinkIds)) $em->remove($link); $em->flush(); return $this->redirect($this->generateUrl('news_edit', array('id' => $id))); } return array( 'entity' => $entity, 'edit_form' => $editForm->createView(), 'delete_form' => $deleteForm->createView(), ); }
Source: https://habr.com/ru/post/125948/
All Articles