在我的Symdony2项目中,我有两个相关实体:" Reglement和" Article"。这应该是多对一的关系,因为每个" Reglement"可以有很多"文章",每个"文章"可以属于一个" Reglement"。 此外,我需要一个用户界面来管理Reglement和Articles。因此,在添加Reglement时,用户应该能够添加它所属的许多文章。 我已经通过在我的学说中建立一对多的关系来实现这一目标。一切都像魅力一样,包括在Symfony2中构建自定义表单类型的用户界面(我已使用"集合"表单字段类型,以允许用户添加"文章"在& #34; Reglement)。我唯一的问题是我只能保存数据库中的最后一篇文章!!
这是我的" Reglement"实体源代码:
<?php
namespace GestionEnvironnementale\ISO14001Bundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Reglement
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="GestionEnvironnementale\ISO14001Bundle\Entity\ReglementRepository")
*/
class Reglement
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="domaineApplication", type="string", length=255)
*/
private $domaineApplication;
/**
* @var string
*
* @ORM\Column(name="texteLegislatif", type="text")
*/
private $texteLegislatif;
/**
* @var string
*
* @ORM\Column(name="contenuText", type="text")
*/
private $contenuText;
/**
* @ORM\OneToMany(targetEntity="GestionEnvironnementale\ISO14001Bundle\Entity\Article", cascade={"persist"}, mappedBy="reglement")
*/
private $articles;
public function __construct()
{
$this->articles = new \Doctrine\Common\Collections\ArrayCollection();
}
public function getArticles()
{
return $this->articles;
}
public function setArticles(ArrayCollection $articles)
{
foreach ($articles as $article) {
$article->addReglement($this);
}
$this->articles = $articles;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set domaineApplication
*
* @param string $domaineApplication
* @return Reglement
*/
public function setDomaineApplication($domaineApplication)
{
$this->domaineApplication = $domaineApplication;
return $this;
}
/**
* Get domaineApplication
*
* @return string
*/
public function getDomaineApplication()
{
return $this->domaineApplication;
}
/**
* Set texteLegislatif
*
* @param string $texteLegislatif
* @return Reglement
*/
public function setTexteLegislatif($texteLegislatif)
{
$this->texteLegislatif = $texteLegislatif;
return $this;
}
/**
* Get texteLegislatif
*
* @return string
*/
public function getTexteLegislatif()
{
return $this->texteLegislatif;
}
/**
* Set contenuText
*
* @param string $contenuText
* @return Reglement
*/
public function setContenuText($contenuText)
{
$this->contenuText = $contenuText;
return $this;
}
/**
* Get contenuText
*
* @return string
*/
public function getContenuText()
{
return $this->contenuText;
}
/**
* Add articles
*
* @param \GestionEnvironnementale\ISO14001Bundle\Entity\Article $articles
* @return Reglement
*/
public function addArticle(\GestionEnvironnementale\ISO14001Bundle\Entity\Article $articles)
{
$this->articles[] = $articles;
return $this;
}
/**
* Remove articles
*
* @param \GestionEnvironnementale\ISO14001Bundle\Entity\Article $articles
*/
public function removeArticle(\GestionEnvironnementale\ISO14001Bundle\Entity\Article $articles)
{
$this->articles->removeElement($articles);
}
}
这是我的文章实体源代码:
<?php
namespace GestionEnvironnementale\ISO14001Bundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Article
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="GestionEnvironnementale\ISO14001Bundle\Entity\ArticleRepository")
*/
class Article
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="GestionEnvironnementale\ISO14001Bundle\Entity\Reglement", inversedBy="articles")
* @ORM\JoinColumn(nullable=false)
*/
private $reglement;
/**
* @var string
*
* @ORM\Column(name="exigenceArticle", type="string", length=255)
*/
private $exigenceArticle;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set exigenceArticle
*
* @param string $exigenceArticle
* @return Article
*/
public function setExigenceArticle($exigenceArticle)
{
$this->exigenceArticle = $exigenceArticle;
return $this;
}
/**
* Get exigenceArticle
*
* @return string
*/
public function getExigenceArticle()
{
return $this->exigenceArticle;
}
/**
* Set reglement
*
* @param \GestionEnvironnementale\ISO14001Bundle\Entity\Reglement $reglement
* @return Article
*/
public function setReglement(\GestionEnvironnementale\ISO14001Bundle\Entity\Reglement $reglement)
{
$this->reglement = $reglement;
return $this;
}
/**
* Get reglement
*
* @return \GestionEnvironnementale\ISO14001Bundle\Entity\Reglement
*/
public function getReglement()
{
return $this->reglement;
}
public function addReglement(Reglement $reglement)
{
if (!$this->reglements->contains($reglement)) {
$this->reglements->add($reglement);
}
}
}
这是我的登记表:
<?php
namespace GestionEnvironnementale\ISO14001Bundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ReglementType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('domaineApplication')
->add('texteLegislatif')
->add('contenuText')
->add('articles', 'collection', array(
'type' => new ArticleType(),
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'by_reference' => false,
));
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'GestionEnvironnementale\ISO14001Bundle\Entity\Reglement'
));
}
/**
* @return string
*/
public function getName()
{
return 'gestionenvironnementale_iso14001bundle_reglementtype';
}
}
和我的文章形式:
<?php
namespace GestionEnvironnementale\ISO14001Bundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class ArticleType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('exigenceArticle')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'GestionEnvironnementale\ISO14001Bundle\Entity\Article'
));
}
/**
* @return string
*/
public function getName()
{
return 'gestionenvironnementale_iso14001bundle_article';
}
}
这是我的ReglementController源代码的一部分:
public function ajouterReglementAction()
{
$reglement = new Reglement();
$form = $this->createForm(new ReglementType(), $reglement);
$request = $this->getRequest();
if ($request->getMethod() == 'POST') {
$form->bind($request);
if ($form->isValid()) {
$reglement->getArticles()->clear();
$em = $this->getDoctrine()->getManager();
$em->persist($reglement);
$em->flush();
foreach ($form->get('articles')->getData() as $ac) {
$ac->setReglement($reglement);
$em->persist($ac);
}
$em->flush();
$this->get('session')->getFlashBag()->add('info', 'Reglement bien enregistré');
return $this->redirect( $this->generateUrl('reglement_voir', array('id' => $reglement->getId())));
}
}
return $this->render('ISO14001Bundle:Reglements:ajouter.html.twig', array(
'form' => $form->createView()
));
}
最终表单查看源代码:
<div class="well">
<form method="post" {{ form_enctype(form) }}>
{{ form_widget(form) }}
<br/> <input type="submit" value="Envoyer" class="btn btn-primary" />
</form>
<script src="{{ asset('js/jquery-2.1.1.min.js') }}"></script>
<script type="text/javascript">
$(document).ready(function() {
var $container = $('div#gestionenvironnementale_iso14001bundle_reglementtype_articles');
var $lienAjout = $('<a href="#" id="ajout_article" class="btn">Ajouter un article</a>');
$container.append($lienAjout);
$lienAjout.click(function(e) {
ajouterArticle($container);
e.preventDefault();
return false;
});
var index = $container.find(':input').length;
if (index == 0) {
ajouterArticle($container);
} else {
$container.children('div').each(function() {
ajouterLienSuppression($(this));
});
}
function ajouterArticle($container) {
var $prototype = $($container.attr('data-prototype').replace(/__name__label__/g, 'Article n°' + (index+1))
.replace(/\$\$name\$\$/g, index));
ajouterLienSuppression($prototype);
$container.append($prototype);
index++;
}
function ajouterLienSuppression($prototype) {
$lienSuppression = $('<a href="#" class="btn btn-danger">Supprimer</a>');
$prototype.append($lienSuppression);
$lienSuppression.click(function(e) {
$prototype.remove();
e.preventDefault();
return false;
});
}
});
我希望你能帮助我;)
答案 0 :(得分:0)
大家好,我找到了问题的解决方案 ^ _ ^
错误位于表单视图的第27行,必须有__name__
而不是$$name$$
,如下所示:
function ajouterArticle($container) {
var $prototype = $($container.attr('data-prototype').replace(/__name__label__/g, 'Article n°' + (index+1))
.replace(/__name__/g, index));
ajouterLienSuppression($prototype);
$container.append($prototype);
index++;
}
祝你好运;)