Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7983809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T11:04:32+00:00 2026-06-04T11:04:32+00:00

I have a Like entity with a getDislike() function in my Symfony2-Doctrine2 project. Once

  • 0

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;
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T11:04:39+00:00Added an answer on June 4, 2026 at 11:04 am

    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:

    $itemlikes = $item->getLikes();
    

    after the code that retrieves the Like entity, i.e.:

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

    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 try getReference() from EntityManager to see if it doesn’t provide the same error!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have entity model like this (using EclipseLink and JPA 2.0): @Entity class A
I have a Customer class defined like this: @Entity public class Customer { //...
I have a JPA object which has a many-to-many relationship like this: @Entity public
I'm working with a small model in Entity Framework 4.0. I'd like to have
I have an entity which looks something like this: (I'm coding to the web
I have an entity and it seems like using DateTime as the Id would
I have an entity which looks like this: Entityname = Country with the attributes
I have an Entity in Code First Entity framework that currently looks like this:
I have a variable declared like this in a class: Entity *array[BOARD_SIZE][BOARD_SIZE]; I need
I have a Person and an Organisation Entity: Person looks like this: public class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.