Search code examples
phpformssymfonytwigsymfony-2.1

Edit my entity based on and ID of other Entity


i want to edit my Category entity based on Test ID in the form in Twig and bind it to database, when i open my edit it displays me the forms i edit them but they don't bind the id test from database , i can't understand why, may be something i wrote wrong :( my controller :

 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")
 */

public function 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,
    );
}
 }

my twig file :

{% 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 %}

Category entity:

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;
}
}

Test entity:

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;
}
}

Solution

  • in the foreach loop > if you never add the $test to the category object. add this:

    $categoryName->setTest($test);
    

    the foreach loop should look like this:

    $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
    

    Add this before the foreach loop:

    $currentCats = $test->getCategories();
    foreach ($currentCats as $current){
        $test->removeCategorie($current);
    }
    

    FINAL EDIT :

    Instead of setting test for the category add the category to test, like 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();