我想根据Twig中表单中的测试ID编辑我的类别实体并将其绑定到数据库, 当我打开编辑时,它会显示我编辑的表单,但它们没有绑定数据库中的 id 测试,我不明白为什么,可能是我写错了:( 我的控制器:
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")
*/
公共函数 updateAction($id) {
$test =
$this->getDoctrine()->getRepository('LadelaOdeskTesterBundle:Test')->find($id);
if (!$test) {
throw $this->createNotFoundException('Unable to find Test entity.');
}
$success = 0;
$categoryList = array_map('trim', $this->getRequest()->get('category-new'));
$currentCats = $test->getCategories();
foreach ($currentCats as $current){
$test->removeCategorie($current);
}
$em = $this->getDoctrine()->getEntityManager();
foreach ($categoryList as $category) {
if (!empty($category)) {
$categoryName = new Category();
$categoryName->setName($category);
$categoryName->setTest($test);
$test->addCategorie($categoryName);
$em->persist($categoryName);
$success = ' Category ' . $category . ' was created';
}
}
$em->flush();
return $this->redirect($this->generateUrl('test.edit',array('id' => $id)));
return array(
'test' => $test,
);
}
}
我的树枝文件:
{% 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 Category
{
/**
* @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 string $color
*
* @ORM\Column(name="color", type="string", length=255)
*/
private $color;
/**
* @var Category category
*
* @ORM\ManyToOne(targetEntity="Test",inversedBy="categories")
* @ORM\JoinColumn(name="test_id", referencedColumnName="id")
*
*/
protected $test;
public function __construct()
{
$this->color = 'rgb(255,255,0)';
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Category
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set color
*
* @param string $color
* @return Category
*/
public function setColor($color)
{
$this->color = $color;
return $this;
}
/**
* Get color
*
* @return string
*/
public function getColor()
{
return $this->color;
}
/**
* Set test
*
* @param Ladela\OdeskTesterBundle\Entity\Test $test
* @return Category
*/
public function setTest(\Ladela\OdeskTesterBundle\Entity\Test $test )
{
$this->test = $test;
return $this;
}
/**
* Get test
*
* @return Ladela\OdeskTesterBundle\Entity\Test
*/
public function getTest()
{
return $this->test;
}
}
测试实体:
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 添加到类别对象。 添加这个:
$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
在 foreach 循环之前添加:
$currentCats = $test->getCategories();
foreach ($currentCats as $current){
$test->removeCategorie($current);
}
最终编辑:
不要为类别设置测试,而是添加要测试的类别,如下所示:
$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();