gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-31 21:01:08 25 4
gpt4 key购买 nike

我有一个 Portfolio 实体,它可以很好地创建或更新,但我无法删除它。 Symfony 抛出这个错误:

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

这是我的实体:

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

}

您对这种行为有任何线索吗?非常感谢。

最佳答案

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

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

关于php - 为什么我的 symfony 实体不受管理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42231697/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com