0

我的Symfony2-Doctrine2项目中有一个带有getDislike()函数的Like实体。一旦我检索到 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.

为什么会出现前面的错误!我的代码中没有提到文章实体(当然,在学说方案中,许多赞与一篇文章之间存在关系,反之亦然)。

任何想法?根据要求,我添加了 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;
    }
4

1 回答 1

0

最后,我发现了错误。我解释了错误消息(“通过关系 'Acme\ArticleBundle\Entity\Article#likes' 找到了一个新实体”),所以我输入了代码:

$itemlikes = $item->getLikes();

在检索 Like 实体的代码之后,即:

$like =  $this->getLikeDislikeEntity($doctrine, $user, $entityType, $entityId);

然后,错误消失了!也许,问题是我在数据库中打开了同一行的两个副本(一个来自使用获取的列表getLikes(),另一个具有获取特定 Like 实例的函数,即 ie getLikeDislikeEntity)。我必须尝试查看它是否不提供相同的错误getReference()EntityManager

于 2012-05-24T18:24:32.520 回答