我的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;
}