title: Why my symfony entity is not managed?
body: <p>I have a <code>Portfolio</code> entity which is working <strong>well to create or update</strong> but I can't delete it. Symfony throws this error:</p>
  
  <blockquote>
    <p>Entity matthieu-appriou is not managed. An entity is managed if its
    fetched from the database or registered as new through
    EntityManager#persist</p>
  </blockquote>
  
  <p>Here is my entity:</p>
  
  <pre><code>&lt;?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-&gt;setVisible(false);
      $this-&gt;expertises = new ArrayCollection();
    }
  
  
    public function __toString() {
      return $this-&gt;getSlug();
    }
  
    /**
     * Get id
     *
     * @return int
     */
    public function getId()
    {
      return $this-&gt;id;
    }
  
  
    public function getSlug()
    {
      return $this-&gt;getTalent()-&gt;getSlug();
    }
  
    /**
     * Set visible
     *
     * @param boolean $visible
     *
     * @return Portfolio
     */
    public function setVisible($visible)
    {
      $this-&gt;visible = $visible;
  
      return $this;
    }
  
    /**
     * Get visible
     *
     * @return bool
     */
    public function getVisible()
    {
      return $this-&gt;visible;
    }
  
    /**
     * @return mixed
     */
    public function getTalent()
    {
      return $this-&gt;talent;
    }
  
    /**
     * @param mixed $talent
     */
    public function setTalent($talent)
    {
      $this-&gt;talent = $talent;
      $talent-&gt;setPortfolio($this);
    }
  
    /**
     * @return mixed
     */
    public function getImage()
    {
      return $this-&gt;image;
    }
  
    /**
     * @param mixed $image
     */
    public function setImage($image)
    {
      if ($image) {
        $image-&gt;setParentType('portfolio');
      }
      $this-&gt;image = $image;
    }
  
    /**
     * @return string
     */
    public function getMainCustomers()
    {
      return $this-&gt;mainCustomers;
    }
  
    /**
     * @param string $mainCustomers
     */
    public function setMainCustomers($mainCustomers)
    {
      $this-&gt;mainCustomers = $mainCustomers;
    }
  
    /**
     * @return string
     */
    public function getMainAgencies()
    {
      return $this-&gt;mainAgencies;
    }
  
    /**
     * @param string $mainAgencies
     */
    public function setMainAgencies($mainAgencies)
    {
      $this-&gt;mainAgencies = $mainAgencies;
    }
  
    /**
     * @return string
     */
    public function getDescriptionTitle()
    {
      return $this-&gt;descriptionTitle;
    }
  
    /**
     * @param string $descriptionTitle
     */
    public function setDescriptionTitle($descriptionTitle)
    {
      $this-&gt;descriptionTitle = $descriptionTitle;
    }
  
    /**
     * @return string
     */
    public function getDescriptionText()
    {
      return $this-&gt;descriptionText;
    }
  
    /**
     * @param string $descriptionText
     */
    public function setDescriptionText($descriptionText)
    {
      $this-&gt;descriptionText = $descriptionText;
    }
  
  
    public function addExpertise($expertise)
    {
      $this-&gt;expertises[] = $expertise;
      return $this;
    }
  
    public function removeExpertise($expertise)
    {
      $this-&gt;expertises-&gt;removeElement($expertise);
    }
  
    public function getExpertises()
    {
      return $this-&gt;expertises;
    }
  
    public function setExpertises($expertises)
    {
      $this-&gt;expertises = $expertises;
    }
  
  
    /**
     * @return mixed
     */
    public function getCreated()
    {
      return $this-&gt;created;
    }
  
    /**
     * @param mixed $created
     */
    public function setCreated($created)
    {
      $this-&gt;created = $created;
    }
  
    /**
     * @return \DateTime
     */
    public function getUpdated()
    {
      return $this-&gt;updated;
    }
  
    /**
     * @param \DateTime $updated
     */
    public function setUpdated($updated)
    {
      $this-&gt;updated = $updated;
    }
  
    /**
     * @return \DateTime
     */
    public function getFeaturedTime()
    {
      return $this-&gt;featuredTime;
    }
  
    /**
     * @param \DateTime $featuredTime
     */
    public function setFeaturedTime($featuredTime)
    {
      $this-&gt;featuredTime = $featuredTime;
    }
  
    /**
     * @return string
     */
    public function getPublications()
    {
      return $this-&gt;publications;
    }
  
    /**
     * @param string $publications
     */
    public function setPublications($publications)
    {
      $this-&gt;publications = $publications;
    }
  
    /**
     * @return mixed
     */
    public function getSubmissionTime()
    {
      return $this-&gt;submissionTime;
    }
  
    /**
     * @param mixed $submissionTime
     */
    public function setSubmissionTime($submissionTime)
    {
      $this-&gt;submissionTime = $submissionTime;
    }
  
    /**
     * @return string
     */
    public function getFolioLabel()
    {
      return $this-&gt;folioLabel;
    }
  
    /**
     * @param string $folioLabel
     */
    public function setFolioLabel($folioLabel)
    {
      $this-&gt;folioLabel = $folioLabel;
    }
  
  
  
  
  
    public function getType()
    {
      return 'portfolio';
    }
  
    /**
     * @return \DateTime
     */
    public function getMostRecentProjectDate()
    {
      return $this-&gt;mostRecentProjectDate;
    }
  
    /**
     * @param \DateTime $mostRecentProjectDate
     */
    public function setMostRecentProjectDate($mostRecentProjectDate)
    {
      $this-&gt;mostRecentProjectDate = $mostRecentProjectDate;
    }
  
    public function setTranslatableLocale($locale)
    {
      $this-&gt;locale = $locale;
    }
  
  
  
  }
  </code></pre>
  
  <p>And here is the code sample I use to reproduce this error:</p>
  
  <pre><code>&lt;?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-&gt;getDoctrine()-&gt;getManager();
      $pr = $this-&gt;get('creasenso.portfolio_repo');
  
      $p = $pr-&gt;find(32);
  
      $em-&gt;remove($p);
      $em-&gt;flush();
  
  
      return new Response("&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;hr /&gt;Done&lt;/body&gt;&lt;/html&gt;");
  
    }
  
  }
  </code></pre>
  
  <p>Here is the repository linked to this entity:</p>
  
  <pre><code>&lt;?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-&gt;qb = $this-&gt;createQueryBuilder('p');
    }
  
    public function isVisible()
    {
      $this-&gt;qb
        -&gt;andWhere('p.visible = :visible')
        -&gt;setParameter(':visible', 1);
    }
  
    public function getAllPublicPortfolioCount()
    {
      $this-&gt;init();
      $this-&gt;isVisible();
      $this-&gt;qb-&gt;select('COUNT(p)');
  
      return $this-&gt;qb-&gt;getQuery()-&gt;getSingleScalarResult();
    }
  
    public function getQueryBuilder()
    {
      return $this-&gt;qb;
    }
  
  }
  </code></pre>
  
  <p>Do you have any clue about this behavior? Thank you very much.</p>