我的 Symfony2-Doctrine2 项目中有一个赞实体,其中包含 getDislike()功能。一旦我检索到一个Like实体并在echo中调用getDislike()函数,就不会打印任何内容。
echo '<br />'. $like->getDislike(); //Nothing is printed (1, 0, true, false)
echo '<br />'. $like->getId(); // the entity id is printed
如果我在if条件中使用相同的实体调用相同的函数,
if($like->getDislike() == FALSE){ //throw an exception
....}
然后返回以下错误:
A new entity was found through the relationship 'Acme\ArticleBundle\Entity\Article#likes' that
was not configured to cascade persist operations for entity: Acme\ArticleBundle\Entity
\Like@00000000653f908100000000749fcbfe. Explicitly persist the new entity or configure cascading
persist operations on the relationship. If you cannot find out which entity causes the problem
implement 'Acme\ArticleBundle\Entity\Like#__toString()' to get a clue.
为什么会发生上述错误!我的代码中没有提到Article实体(当然,在学说方案中有许多关系,如同一篇文章,反之亦然)。
有什么想法吗? 根据请求,我添加Like实体文件:
namespace Acme\ArticleBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Acme\ArticleBundle\Entity\Like
*
* @ORM\Table()
* @ORM\Entity
*/
class Like
{
/**
* @ORM\ManyToOne(targetEntity="Article")
* @ORM\JoinColumn(name="article_id", referencedColumnName="id")
*/
protected $article;
/**
* @ORM\Column(type="boolean")
*/
protected $dislike;
//.... other fields
/**
* Set dislike
*
* @param boolean $dislike
*/
public function setDislike($dislike)
{
$this->dislike = $dislike;
}
/**
* Get dislike
*
* @return boolean
*/
public function getDislike()
{
return $this->dislike;
}
//.... other methods
}
这是文章实体:
namespace Acme\ArticleBundle\Entity;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Acme\ArticleBundle\Entity\Article
*
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Acme\ArticleBundle\Repository\ArticleRepository")
*/
class Article
{
/**
*
* @ORM\OneToMany(targetEntity="Like", mappedBy="article")
*/
protected $likes;
//.... other fields
public function __construct() {
...
$this->likes = new \Doctrine\Common\Collections\ArrayCollection();
...
}
}
这是控制器代码:
public function likeDislikeItemAction(){
if ($this->get('security.context')->isGranted('IS_AUTHENTICATED_FULLY')){
$user = $this->get('security.context')->getToken()->getUser();
$entityType = "AcmeArticleBundle:Article"; // This static for debugging
$entityId = 1; // This static for debugging
$doctrine = $this->getDoctrine();
$item = $doctrine
->getRepository($entityType)
->findOneById($entityId);
if(is_object($item)){
$itemlikes = $item->getLikes();
$totvotes = sizeof($itemlikes);
$numlikes = $this->getNumLikes($itemlikes);
$numdislikes = $totvotes - $numlikes;
$dislike_value = 'like'; // This static for debugging
if($dislike_value == 'like') $isDislike = FALSE;
else $isDislike = TRUE;
$like = $this->getLikeDislikeEntity($doctrine, $user, $entityType, $entityId);
if(is_object($like)) {
$test = $like->getDislike();
echo 'test val ' . $test;
}
}
}
return NULL;
}
private function getLikeDislikeEntity($doctrine, $user, $entityType, $entityId){
if ($entityType == "AcmeArticleBundle:Article") {
return $doctrine->getRepository("AcmeArticleBundle:Like")
->findOneBy(array('author' => $user->getId(), 'article' =>$entityId));
}
return null;
}
答案 0 :(得分:0)
最后,我发现了错误。我解释了错误消息(“通过关系'Acme \ ArticleBundle \ Entity \ Article#likes'找到了一个新实体),所以我把代码放在了:
$itemlikes = $item->getLikes();
在检索Like实体的代码之后,即:
$like = $this->getLikeDislikeEntity($doctrine, $user, $entityType, $entityId);
然后,错误消失了!
也许,问题是我在数据库中打开同一行的两个副本(一个来自使用getLikes()
获得的列表,另一个带有获取特定Like实例的函数,即getLikeDislikeEntity
) 。我必须从getReference()
尝试EntityManager
,看它是否提供相同的错误!