为什么我的symfony实体不受管理?

时间:2017-02-14 16:48:33

标签: php symfony doctrine-orm

我有一个Portfolio实体正在创建或更新,但我无法将其删除。 Symfony抛出了这个错误:

  

实体matthieu-appriou不受管理。如果实体是管理的   从数据库中提取或注册为新的   的EntityManager#坚持

这是我的实体:

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



}

以下是我用来重现此错误的代码示例:

<?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>");

  }

}

以下是链接到此实体的存储库:

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

}

你对这种行为有什么线索吗?非常感谢你。

1 个答案:

答案 0 :(得分:2)

当您从EntityManager通过getRepository()获取存储库时,您将直接从服务容器获取存储库。

您没有从您刷新的entityManager加载您的实体,因此它不受管理