Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8850705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:57:50+00:00 2026-06-14T12:57:50+00:00

i want to edit my Category entity based on Test ID in the form

  • 0

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;
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T12:57:51+00:00Added an answer on June 14, 2026 at 12:57 pm

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to edit .vimrc file from Vim and apply them without restarting Vim.
I want to edit in 'videos' table, if 'category' column has the value set
I want edit multiple strings at once by selection the list of strings needs
I want to edit links when the user changes the value of a <input
I want to edit the Velocity Report generated with Visual Studio 2010s scrum template.
I want to edit UITabbarItems during the view life time. The background is that
I want to edit the css of a div element during .resize() event and
I simply want to edit some commits with git rebase -i HEAD~2 inside the
Dynamically I want to edit/update hosts(etc/hosts) file to add domain. To edit hosts(etc/hosts) file
In my operation i want to edit an employee list. In this time am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.