Search code examples
phpsymfonymismatch

Symfony 3 error : Case mismatch between loaded and declared class names


i've just got this error

Case mismatch between loaded and declared class names: MyApp\UserBundle\Entity\post vs MyApp\UserBundle\Entity\Post

i'm using two controllers to do a specific operation of delete and getting back to the old page

here is my button's code

<a href="{{ path('DeleteComment',{'idc':comment.id}) }}"></a>
                                <a href="{{ path('DeleteComment',{'idc':comment.id}) }}"> <i
                                            class="icon-trash"></i>Delete</a>

here is my routing's code :

get_view_post:
path:     /blog/get/one/post/{id}/
defaults: { _controller: "MyAppBlogBundle:Blog:getpost" }
DeleteComment:
path:     /blog/post/comment/delete/{idc}/
defaults: { _controller: "MyAppBlogBundle:Blog:DeleteComment" }

here is my controllers code :

public function DeleteCommentAction($idc)
{

    $em = $this->getDoctrine()->getManager();
    $comment = $em->getRepository('MyAppUserBundle:PostComment')->find($idc);
    $idPost =$comment->getIdPost();


    $em->remove($comment);
    $em->flush();


    return $this->redirectToRoute("get_view_post", array('id' => $idPost));
}


 public function getpostAction($id)
{
    $user = $this->getUser();
    $idu = $user->getId();
    $em = $this->getDoctrine()->getManager();
    $em1 = $this->getDoctrine()->getManager();
    $post = $em->getRepository('MyAppUserBundle:Post')->find($id);
    $idPost=$post->getId();
    $comment = $em1->getRepository('MyAppUserBundle:PostComment')->findBy(array('idPost' => $idPost));
    return $this->render('MyAppBlogBundle::afficherPostAvecComment.html.twig', array('posts' => $post,'comments'=>$comment,'idu'=>$idu));

}

i'm declaring my entities like this :

use MyApp\UserBundle\Entity\Post;
use MyApp\UserBundle\Entity\PostComment;

Here is my Entity

namespace MyApp\UserBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
/**
 * @ORM\Entity
 * @ORM\Table(name="post")
 */
class Post
{


/**
 * @var integer
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="IDENTITY")
 */
private $id;

/**
 * @var string
 * @ORM\Column(name="titre", type="string", length=100, nullable=false)
 */
private $titre;
/**
 * @var string
 * @ORM\Column(name="contenu", type="string", length=250, nullable=false)
 */
private $contenu;

/**
 * @var \DateTime
 * @ORM\Column(name="dateajout", type="datetime", nullable=true)
 */
private $dateajout ;

/**
 * @var integer
 * @ORM\ManyToOne(targetEntity="User")
 * @ORM\JoinColumn(name="id_utilisateur",referencedColumnName="id")
 */
private $idUser;

/**
 * @var integer
 * @ORM\Column(name="nbLike", type="integer", nullable=true)
 */
private $nbLike =0;
/**
 * @var integer
 * @ORM\Column(name="nbDislike", type="integer", nullable=true)
 */
private $nbDislike=0;
/**
 * @var integer
 * @ORM\Column(name="nbSignal", type="integer", nullable=true)
 */
private $nbSignal=0;

Solution

  • i did some changes in my code, as i see My IDE couldn't differentiate between Post, PostComment and my array post or furthermore his own method of recuperation _POST. you can see from the error above that it's based on hesitation between Post and post ,if you are using a latest version of Symfony, try to scan you whole project and change the names of your attributes or classes, believe it or not it creates some difficulties for the IDE when your project gets bigger

    and here is what made the error gone:

    • old
    /**
    * @var integer
    * @ORM\ManyToOne(targetEntity="post")
    * @ORM\JoinColumn(name="id_post",referencedColumnName="id")
    */
    private $idPost;
    
    • new
    /**
    * @var integer
    * @ORM\ManyToOne(targetEntity="Post")
    * @ORM\JoinColumn(name="id_post",referencedColumnName="id")
    */
    private $idPost;
    

    I was not giving the appropriate name of my Entity, so when I do any operation based on foreign keys, the IDE won't find any Entity of reference

    if you are using and old version of Symfony, you have to add a line of code in some file's configuration you can have a better explanation here:

    Symfony2 error : Case mismatch between loaded and declared class names: