I have a Like entity with a getDislike() function in my Symfony2-Doctrine2 project. Once I retrieve a Like entity and I call the getDislike() function in an echo, nothing is printed.
echo '<br />'. $like->getDislike(); //Nothing is printed (1, 0, true, false)
echo '<br />'. $like->getId(); // the entity id is printed
If I call the same function with the same entity in an if condition,
if($like->getDislike() == FALSE){ //throw an exception
....}
then the following error is returned:
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.
Why the preceding error happens! The Article entity is not mentioned in my code (of course there is a relationship many Like to one Article and vice versa in the doctrine scheme).
Any idea?
Upon request, I add the Like entity file:
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
}
Here is the article Entity:
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();
...
}
}
And here is the controller code:
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;
}
Finally, I’ve found the error. I interpreted the error message (“A new entity was found through the relationship ‘Acme\ArticleBundle\Entity\Article#likes’“) and so I put the code:
after the code that retrieves the Like entity, i.e.:
Then, the error disappeared!
Maybe, the problem is that I’m opening two copy of the same row in the DB (one from the list obtained with
getLikes()and another with the function to get the specific Like instance, i.e.getLikeDislikeEntity). I have to trygetReference()fromEntityManagerto see if it doesn’t provide the same error!