Forms 根据其他实体的名称和ID编辑我的实体
我想根据Twig表单中的测试ID编辑我的类别实体,并将其绑定到数据库, 当我打开“编辑”时,它会显示我编辑的表单,但它们没有从数据库绑定id测试,我不明白为什么,可能是我写错了什么:( 我的控制器:Forms 根据其他实体的名称和ID编辑我的实体,forms,symfony,twig,symfony-2.1,Forms,Symfony,Twig,Symfony 2.1,我想根据Twig表单中的测试ID编辑我的类别实体,并将其绑定到数据库, 当我打开“编辑”时,它会显示我编辑的表单,但它们没有从数据库绑定id测试,我不明白为什么,可能是我写错了什么:( 我的控制器: class DefaultController extends Controller { function editAction($id) { $em = $this->getDoctrine()->getEntityManager(); $test = $em-&g
class DefaultController extends Controller
{
function editAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$test = $em->getRepository('LadelaOdeskTesterBundle:Test')->find($id);
if (!$test) {
throw $this->createNotFoundException('Unable to find Advertiser entity.');
}
return array(
'test' => $test,
);
}
/**
* @Route("/new/{id}/update", name="test.update", requirements={"id" = "\d+"})
* @Method("Post")
* @Template("LadelaOdeskTesterBundle:Default:edit.html.twig")
*/
公共函数更新操作($id)
{
我的小枝文件:
{% extends '::base.html.twig' %}
{% block body %}
{#<div class="wrap">#}
<ul id="breadcrumb">
<li><a href="{{path('homepage')}}" title="Home"><img src="{{ \
asset('bundles/ladelaodesktester/images/home.png') }}" alt="Home" class="home"
/></a> </li>
<li> Please add data to new test </li>
</ul>
<h2>Edit Test </h2>
{% for test in tests %}
<form action="{{ path('test.update',{'id': test.id }) }}" method="post">
Category 1<input type="text" name="category-new[]" >
<div id="customWidget">
<div id="colorSelector1"><div style="background-color: #00ff00"></div>
</div>
<div id="colorpickerHolder1"></div>
</div>
Category 2<input type="text" name="category-new[]" ><br>
Category 3<input type="text" name="category-new[]" ><br>
Category 4<input type="text" name="category-new[]" ><br>
Category 5<input type="text" name="category-new[]" ><br>
Category 6<input type="text" name="category-new[]" ><br>
Category 7<input type="text" name="category-new[]" ><br>
Category 8<input type="text" name="category-new[]" ><br>
Category 9<input type="text" name="category-new[]" ><br>
<input type="submit" value="Add">
</form>
{% endfor %}
</div>
<a href="{{ path('test.new')}}">Back to Test</a>
{#</div>#}
{% endblock %}
{% block javascripts %}
{{ parent() }}
<script src="{{ asset('bundles/ladelaodesktester/js/new.js') }}"
type="text/javascript"></script>
<script src="{{ asset('bundles/ladelaodesktester/js/colorpicker.js') }}"
type="text/javascript"></script>
{% endblock %}
测试实体:
class Test
{
/**
* @var integer $id
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string $name
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var \DateTime $created_at
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(name="created_at", type="datetime")
*/
private $created_at;
/**
* @Gedmo\Slug(fields={"name"})
* @ORM\Column(length=128,unique=true)
*/
private $slug;
/**
* @ORM\ManyToMany(targetEntity="Question")
* @ORM\JoinTable(name="test_questions",
* joinColumns={@ORM\JoinColumn(name="test_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="question_id",
referencedColumnName="id")}
* )
* @var Collection $questions
**/
protected $questions;
/**
* @ORM\ManyToMany(targetEntity="Result")
* @ORM\JoinTable(name="test_results",
* joinColumns={@ORM\JoinColumn(name="test_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="result_id",
referencedColumnName="id")}
* )
* @var Collection $results
**/
protected $results;
/**
* @ORM\OneToMany(targetEntity="Category", mappedBy="test")
*/
private $categories;
/**
* @var \DateTime $updated_at
*
* @Gedmo\Timestampable(on="update")
* @ORM\Column(name="updated_at", type="datetime")
*/
private $updated_at;
public function __construct()
{
$this->questions = new \Doctrine\Common\Collections\ArrayCollection();
$this->categories = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Test
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
public function getSlug()
{
return $this->slug;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set created_at
*
* @param \DateTime $createdAt
* @return Test
*/
public function setCreatedAt($createdAt)
{
$this->created_at = $createdAt;
return $this;
}
/**
* Get created_at
*
* @return \DateTime
*/
public function getCreatedAt()
{
return $this->created_at;
}
/**
* Set updated_at
*
* @param \DateTime $updatedAt
* @return Test
*/
public function setUpdatedAt($updatedAt)
{
$this->updated_at = $updatedAt;
return $this;
}
/**
* Get updated_at
*
* @return \DateTime
*/
public function getUpdatedAt()
{
return $this->updated_at;
}
/**
*
*/
public function getQuestions()
{
return $this->questions;
}
public function addQuestion( Question $question )
{
$this->questions[] = $question;
}
/**
* Set slug
*
* @param string $slug
* @return Test
*/
public function setSlug($slug)
{
$this->slug = $slug;
return $this;
}
/**
* Remove questions
*
* @param Question $questions
*/
public function removeQuestion( Question $questions)
{
$this->questions->removeElement($questions);
}
/**
* Add results
*
* @param Result $results
* @return Test
*/
public function addResult(Result $results)
{
$this->results[] = $results;
return $this;
}
/**
* Remove results
*
* @param Result $results
*/
public function removeResult(Result $results)
{
$this->results->removeElement($results);
}
/**
* Get results
*
* @return Collection
*/
public function getResults()
{
return $this->results;
}
public function countResults()
{
return $this->results->count();
}
public function countQuestions()
{
return $this->questions->count();
}
/**
* Add categories
*
* @param Ladela\OdeskTesterBundle\Entity\Category $categories
* @return Test
*/
public function addCategorie(\Ladela\OdeskTesterBundle\Entity\Category $categories)
{
$this->categories[] = $categories;
return $this;
}
/**
* Remove categories
*
* @param Ladela\OdeskTesterBundle\Entity\Category $categories
*/
public function removeCategorie(\Ladela\OdeskTesterBundle\Entity\Category $categories)
{
$this->categories->removeElement($categories);
}
/**
* Get categories
*
* @return Doctrine\Common\Collections\Collection
*/
public function getCategories()
{
return $this->categories;
}
}
在foreach循环>中,如果从未将$test添加到category对象。 添加以下内容:
$categoryName->setTest($test);
foreach循环应如下所示:
$em = $this->getDoctrine()->getManager(); // <---- Changed this
foreach ($categoryList as $category) {
if (!empty($category)) {
$categoryName = new Category();
$categoryName->setName($category);
$categoryName->setTest($test);
$em->persist($categoryName);
$success = ' Category ' . $category . ' was created';
} else {
$success = 'Test category-new may not be empty';
}
}
$em->flush(); // <---- Changed this
$em = $this->getDoctrine()->getManager();
foreach ($categoryList as $category) {
if (!empty($category)) {
$categoryName = new Category();
$categoryName->setName($category);
$categoryName->setTest($test);
$test->addCategorie($categoryName); // <---- This is added
$em->persist($categoryName);
$success = ' Category ' . $category . ' was created';
} else {
$success = 'Test category-new may not be empty';
}
}
$em->flush();
最终编辑:
不要为类别设置test,而是将类别添加到test,如下所示:
$em = $this->getDoctrine()->getManager(); // <---- Changed this
foreach ($categoryList as $category) {
if (!empty($category)) {
$categoryName = new Category();
$categoryName->setName($category);
$categoryName->setTest($test);
$em->persist($categoryName);
$success = ' Category ' . $category . ' was created';
} else {
$success = 'Test category-new may not be empty';
}
}
$em->flush(); // <---- Changed this
$em = $this->getDoctrine()->getManager();
foreach ($categoryList as $category) {
if (!empty($category)) {
$categoryName = new Category();
$categoryName->setName($category);
$categoryName->setTest($test);
$test->addCategorie($categoryName); // <---- This is added
$em->persist($categoryName);
$success = ' Category ' . $category . ' was created';
} else {
$success = 'Test category-new may not be empty';
}
}
$em->flush();
$em=$this->getDoctrine()->getManager();
foreach($categoryList作为$category){
如果(!空($category)){
$categoryName=新类别();
$categoryName->setName($category);
$categoryName->setTest($test);
$test->addCategorie($categoryName);//persist($categoryName);
$success='Category'.$Category.'已创建';
}否则{
$success='新测试类别不能为空';
}
}
$em->flush();
为什么会有$this->redirect
和foreach
循环?我把$this->redirect放在foreach循环之后,但它会将表单中的所有数据绑定到另一个字段,而不是我要编辑的表单ID字段。请使用测试和类别类更新您的问题。另外,请注意,您无法获得重新设置的信息据我所知,带有$this->getRequest()->get('category-new')的quest变量$this->getRequest()->request->get('category-new'))
I updated@jperovic,如果可以帮助的话,请=]我得到了请求,并将表单中的值放入数据库,这也给我带来了积极的影响(谢谢),但这不是一个解决方案,它用那个测试ID绑定了另一个类别,但没有替换现有的类别。我像你说的那样放置它,但它不起作用,它像第一次一样绑定类别..看看我的控制器我是如何编写的我更新了它flush()
应该在最后一个foreach循环之外。我将更新我的答案。检查该答案是否有效我将重建您的情况。我将编辑该答案如果我让它自己起作用以便进一步讨论请通过电子邮件与我联系,我的邮件地址可以在我的个人资料中找到
$em = $this->getDoctrine()->getManager();
foreach ($categoryList as $category) {
if (!empty($category)) {
$categoryName = new Category();
$categoryName->setName($category);
$categoryName->setTest($test);
$test->addCategorie($categoryName); // <---- This is added
$em->persist($categoryName);
$success = ' Category ' . $category . ' was created';
} else {
$success = 'Test category-new may not be empty';
}
}
$em->flush();