Paul Etienney
Paul Etienney

Reputation: 445

Why my symfony entity is not managed?

I have a Portfolio entity which is working well to create or update but I can't delete it. Symfony throws this error:

Entity matthieu-appriou is not managed. An entity is managed if its fetched from the database or registered as new through EntityManager#persist

Here is my entity:

<?php

namespace CreasensoBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use SensoBundle\Entity\Talent;
use Doctrine\Common\Collections\ArrayCollection;

use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Translatable\Translatable;

use JMS\Serializer\Annotation\MaxDepth;
use JMS\Serializer\Annotation\Exclude;

/**
 * Portfolio
 *
 * @ORM\Entity
 * @ORM\Table(name="portfolio")
 * @ORM\Entity(repositoryClass="CreasensoBundle\Repository\PortfolioRepository")
 */
class Portfolio
{
  /**
   * @var int
   *
   * @ORM\Column(name="id", type="integer")
   * @ORM\Id
   * @ORM\GeneratedValue(strategy="AUTO")
   */
  private $id;

  /**
   * @var bool
   *
   * @ORM\Column(name="visible", type="boolean", nullable=true)
   */
  private $visible;


  /**
   * @Exclude
   * @ORM\OneToOne(targetEntity="SensoBundle\Entity\Talent", cascade={"persist", "remove"}, inversedBy="portfolio")
   * @ORM\JoinColumn(nullable=true, onDelete="SET NULL")
   */
  private $talent;


  /**
   * @Exclude
   * @ORM\OneToOne(targetEntity="SensoBundle\Entity\Image", cascade={"persist", "remove"})
   * @ORM\JoinColumn(nullable=true, onDelete="SET NULL")
   */
  private $image;

  /**
   * @var string
   *
   * @ORM\Column(name="folio_label", type="string", length=400, nullable=true)
   * @Gedmo\Translatable
   */
  private $folioLabel;

  /**
   * @var string
   *
   * @ORM\Column(name="main_customers", type="string", length=400, nullable=true)
   */
  private $mainCustomers;


  /**
   * @var string
   *
   * @ORM\Column(name="main_agencies", type="string", length=400, nullable=true)
   */
  private $mainAgencies;


  /**
   * @var string
   *
   * @ORM\Column(name="publications", type="string", length=700, nullable=true)
   */
  private $publications;

  /**
   * @var string
   *
   * @ORM\Column(name="description_title", type="string", length=400, nullable=true)
   * @Gedmo\Translatable
   */
  private $descriptionTitle;

  /**
   * @var string
   *
   * @ORM\Column(name="description_content", type="text", nullable=true)
   * @Gedmo\Translatable
   */
  private $descriptionText;


  /**
   * @MaxDepth(2)
   * @ORM\ManyToMany(targetEntity="SensoBundle\Entity\Expertise", cascade={"persist"})
   */
  private $expertises;


  /**
   * @var \DateTime $created
   *
   * @Gedmo\Timestampable(on="create")
   * @ORM\Column(type="datetime")
   */
  private $created;

  /**
   * @var \DateTime $updated
   *
   * @Gedmo\Timestampable(on="update")
   * @ORM\Column(type="datetime")
   */
  private $updated;

  /**
   * @var \DateTime $updated
   *
   * @ORM\Column(type="datetime", nullable=true)
   */
  private $mostRecentProjectDate;

  /**
   * @var \DateTime $featuredTime
   * @ORM\Column(type="datetime", nullable=true)
   */
  private $featuredTime;


  /**
   * @var \DateTime $submissionTime
   * @ORM\Column(type="datetime", nullable=true)
   */
  private $submissionTime;


  /**
   * @Gedmo\Locale
   */
  protected $locale;

  public function __construct()
  {
    $this->setVisible(false);
    $this->expertises = new ArrayCollection();
  }


  public function __toString() {
    return $this->getSlug();
  }

  /**
   * Get id
   *
   * @return int
   */
  public function getId()
  {
    return $this->id;
  }


  public function getSlug()
  {
    return $this->getTalent()->getSlug();
  }

  /**
   * Set visible
   *
   * @param boolean $visible
   *
   * @return Portfolio
   */
  public function setVisible($visible)
  {
    $this->visible = $visible;

    return $this;
  }

  /**
   * Get visible
   *
   * @return bool
   */
  public function getVisible()
  {
    return $this->visible;
  }

  /**
   * @return mixed
   */
  public function getTalent()
  {
    return $this->talent;
  }

  /**
   * @param mixed $talent
   */
  public function setTalent($talent)
  {
    $this->talent = $talent;
    $talent->setPortfolio($this);
  }

  /**
   * @return mixed
   */
  public function getImage()
  {
    return $this->image;
  }

  /**
   * @param mixed $image
   */
  public function setImage($image)
  {
    if ($image) {
      $image->setParentType('portfolio');
    }
    $this->image = $image;
  }

  /**
   * @return string
   */
  public function getMainCustomers()
  {
    return $this->mainCustomers;
  }

  /**
   * @param string $mainCustomers
   */
  public function setMainCustomers($mainCustomers)
  {
    $this->mainCustomers = $mainCustomers;
  }

  /**
   * @return string
   */
  public function getMainAgencies()
  {
    return $this->mainAgencies;
  }

  /**
   * @param string $mainAgencies
   */
  public function setMainAgencies($mainAgencies)
  {
    $this->mainAgencies = $mainAgencies;
  }

  /**
   * @return string
   */
  public function getDescriptionTitle()
  {
    return $this->descriptionTitle;
  }

  /**
   * @param string $descriptionTitle
   */
  public function setDescriptionTitle($descriptionTitle)
  {
    $this->descriptionTitle = $descriptionTitle;
  }

  /**
   * @return string
   */
  public function getDescriptionText()
  {
    return $this->descriptionText;
  }

  /**
   * @param string $descriptionText
   */
  public function setDescriptionText($descriptionText)
  {
    $this->descriptionText = $descriptionText;
  }


  public function addExpertise($expertise)
  {
    $this->expertises[] = $expertise;
    return $this;
  }

  public function removeExpertise($expertise)
  {
    $this->expertises->removeElement($expertise);
  }

  public function getExpertises()
  {
    return $this->expertises;
  }

  public function setExpertises($expertises)
  {
    $this->expertises = $expertises;
  }


  /**
   * @return mixed
   */
  public function getCreated()
  {
    return $this->created;
  }

  /**
   * @param mixed $created
   */
  public function setCreated($created)
  {
    $this->created = $created;
  }

  /**
   * @return \DateTime
   */
  public function getUpdated()
  {
    return $this->updated;
  }

  /**
   * @param \DateTime $updated
   */
  public function setUpdated($updated)
  {
    $this->updated = $updated;
  }

  /**
   * @return \DateTime
   */
  public function getFeaturedTime()
  {
    return $this->featuredTime;
  }

  /**
   * @param \DateTime $featuredTime
   */
  public function setFeaturedTime($featuredTime)
  {
    $this->featuredTime = $featuredTime;
  }

  /**
   * @return string
   */
  public function getPublications()
  {
    return $this->publications;
  }

  /**
   * @param string $publications
   */
  public function setPublications($publications)
  {
    $this->publications = $publications;
  }

  /**
   * @return mixed
   */
  public function getSubmissionTime()
  {
    return $this->submissionTime;
  }

  /**
   * @param mixed $submissionTime
   */
  public function setSubmissionTime($submissionTime)
  {
    $this->submissionTime = $submissionTime;
  }

  /**
   * @return string
   */
  public function getFolioLabel()
  {
    return $this->folioLabel;
  }

  /**
   * @param string $folioLabel
   */
  public function setFolioLabel($folioLabel)
  {
    $this->folioLabel = $folioLabel;
  }





  public function getType()
  {
    return 'portfolio';
  }

  /**
   * @return \DateTime
   */
  public function getMostRecentProjectDate()
  {
    return $this->mostRecentProjectDate;
  }

  /**
   * @param \DateTime $mostRecentProjectDate
   */
  public function setMostRecentProjectDate($mostRecentProjectDate)
  {
    $this->mostRecentProjectDate = $mostRecentProjectDate;
  }

  public function setTranslatableLocale($locale)
  {
    $this->locale = $locale;
  }



}

And here is the code sample I use to reproduce this error:

<?php

// namespace and use ...

/**
 * Tool controller.
 *
 * @Route("/admin/test")
 */
class TestController extends Controller
{

  /**
   *
   * @Route("/testTwo", name="testTwo")
   * @Method({"GET", "POST"})
   */
  public function indexTwoAction(Request $request)
  {
    $em = $this->getDoctrine()->getManager();
    $pr = $this->get('creasenso.portfolio_repo');

    $p = $pr->find(32);

    $em->remove($p);
    $em->flush();


    return new Response("<html><head></head><body><hr />Done</body></html>");

  }

}

Here is the repository linked to this entity:

<?php

namespace CreasensoBundle\Repository;

use Doctrine\ORM\EntityRepository;

/**
 * PortfolioRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class PortfolioRepository extends EntityRepository
{

  protected $qb;


  public function init()
  {
    $this->qb = $this->createQueryBuilder('p');
  }

  public function isVisible()
  {
    $this->qb
      ->andWhere('p.visible = :visible')
      ->setParameter(':visible', 1);
  }

  public function getAllPublicPortfolioCount()
  {
    $this->init();
    $this->isVisible();
    $this->qb->select('COUNT(p)');

    return $this->qb->getQuery()->getSingleScalarResult();
  }

  public function getQueryBuilder()
  {
    return $this->qb;
  }

}

Do you have any clue about this behavior? Thank you very much.

Upvotes: 4

Views: 9074

Answers (1)

Mawcel
Mawcel

Reputation: 2007

You are getting your repository directly from the service container when you should be getting it from your EntityManager through getRepository().

You are not loading your entity from the entityManager that you flush so it is not managed

Upvotes: 6

Related Questions