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}) }}"> <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;
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:
Once have look on the folder name of your userbundle file.
Since, you have mentioned UserBunlde in your namespace **(namespace MyApp\UserBundle\Entity;) then,
Your folder name must be UserBundle too.
If you have named as userBundle so that such error occurs.
Related
I keep getting the error in the title when I want to login using the FOSUserBundle on Symfony. The problem is, I already have an "id" for my User table from my database so I don't want to create an "id" field like they ask on the FOSUserBundle guide. I don't understand why it would give me this error when there is no more "id" field in my code.
Is this "id" field mandatory?
Here is the code of my User class (here called "Utilisateurs")`use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
/**
* Utilisateurs
*
* #ORM\Table(name="utilisateurs", indexes={#ORM\Index(name="FK_UTILISATEURS_id_sexe", columns={"id_sexe"}), #ORM\Index(name="FK_UTILISATEURS_id_niveau", columns={"id_niveau"})})
* #ORM\Entity
*/
class Utilisateurs extends BaseUser
{
public function __construct()
{
parent::__construct();
}
/**
* #var string
*
* #ORM\Column(name="nom", type="string", length=25, nullable=true)
*/
private $nom;
/**
* #var string
*
* #ORM\Column(name="prenom", type="string", length=25, nullable=true)
*/
private $prenom;
/**
* #var \DateTime
*
* #ORM\Column(name="date_naissance", type="date", nullable=true)
*/
private $dateNaissance;
/**
* #var string
*
* #ORM\Column(name="url_photo", type="string", length=100, nullable=true)
*/
private $urlPhoto;
/**
* #var integer
*
* #ORM\Column(name="id_utilisateur", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $idUtilisateur;
/**
* #var \Site\UserBundle\Entity\Sexes
*
* #ORM\ManyToOne(targetEntity="Site\UserBundle\Entity\Sexes")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id_sexe", referencedColumnName="id_sexe")
* })
*/
private $idSexe;
/**
* #var \Site\UserBundle\Entity\Niveaux
*
* #ORM\ManyToOne(targetEntity="Site\UserBundle\Entity\Niveaux")
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="id_niveau", referencedColumnName="id_niveau")
* })
*/
private $idNiveau;`
As you can see I already have an "id_utilisateur" field which is the id of this entity.
And here is the code of the entity information in XML: The XML Code
Also here is a screenshot of the error I get when I try to log in: The Error
I think the problem is that per convention the id field is often called just id and in some places FOS UserBundle is expecting exactly that, e.g. in the UserProvider.
There are a few ways you can get around this. For instance you could just write your own UserProvder (using the one linked above as a reference) where you substitute the id with your field. You might have to do this in other places as well.
The easier solution would be to just change your entity to something like this:
/**
* #var integer
*
* #ORM\Column(name="id_utilisateur", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
public function getId() { return $this->id; }
Similarly in xml this would look like this:
<id name="id" column="id_utilisateur" type="integer">
<generator strategy="IDENTITY" />
</id>
This way in your entity you will use the expected property and accessor method, but in the background it will map to the database field id_utilisateur, so you you don't have to make any changes to your database.
This should already solve your problems. When a new user is generated Doctrine will take map $user->getId() to user_table.id_utilisateur automatically. If your existing code is making use of the old get-method you could just keep it around and mark it as deprecated:
/**
* #deprecated Use getId() instead.
*/
public function getIdUtilisateur()
{
return $this->getId();
}
I have a Developers entity and table named CodeUserReference
class CodeUserReference
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
*#ORM\ManyToOne(targetEntity="Artel\CustomerBundle\Entity\Developers", inversedBy="newreference")
*/
protected $alluser;
/**
* #ORM\Column(type="string", length=255)
*/
protected $codereference;
and Developers
class Developers extends SUser
{
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=255, unique=false, nullable=false)
* #Assert\Length(min=3, max=255)
* #Assert\NotBlank
*/
protected $email;
/**
* #ORM\ManyToMany(targetEntity="Artel\CustomerBundle\Entity\CodeUserReference", inversedBy="alluser")
*/
protected $newreference;
I have get query in my Action
public function profileGetAction(Request $request)
{
$em = $this->getDoctrine()->getManager();
$code_user_reference = $em->getRepository('ArtelCustomerBundle:CodeUserReference')->findOneByCodereference($request->query->get('reference'));
$user_by_email = $em->getRepository('ArtelCustomerBundle:Developers')->findOneByEmail($request->query->get('email'));
if (!empty($code_user_reference) && empty($user_by_email))
{
$id = $code_user_reference->getAlluser()->getId();
$user_by_reference = $em->getRepository('ArtelCustomerBundle:Developers')->findOneById($id);
$user_by_reference_json = $em->getRepository('ArtelCustomerBundle:Developers')->createQueryBuilder('d')
->where('d.id= :id')
->groupBy('d.id')
->setParameter('id', $code_user_reference->getAlluser()->getId())
->getQuery()->getArrayResult();
echo json_encode( array('user' => $user_by_reference_json));
die;
}
If(empty($code_user_reference) && !empty($user_by_email))
.......
I get Id in code_user_reference, then I find Developers objects with this ID, then I create a QueryBuilder and in this QueryBuilder I find the developers objects again, for JSON table. I find this very hard, who knows easier practics ?
There is certanly mismapping.
if you have #ORM\ManyToOne from one site, there should be #ORM\OneToMany from other.
Or, if you need many-to-many relationship, there should be #ORM\ManyToMany on both entities.
Also, relation should be mapped at one side. If you have (targetEntity="Artel\CustomerBundle\Entity\Developers", inversedBy="newreference") it means it should be mapped here
something like
#ORM\ManyToOne(targetEntity="Artel\CustomerBundle\Entity\Developers", inversedBy="newreference")
#ORM\JoinColumn(name="id_developer", referencedColumnName="id")
and at the other side must be mappedBy instead of inversedBy
* #ORM\OneToMany(targetEntity="Artel\CustomerBundle\Entity\CodeUserReference", mappedBy="alluser")
I always get an exception with my entities. I tried to play with them. I checked on google which relation is the most appropriate for my entities... But I couldn't find the best configuration.
I'm making a website where you can create some albums.
A user can have multiple albums.So I have an entity Album and I have inside a user property :
/**
* Album
*
* #ORM\Table(name="album")
* #ORM\Entity(repositoryClass="Moodress\Bundle\AlbumBundle\Entity\AlbumRepository")
*/
class Album
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
** #ORM\ManyToOne(targetEntity="Moodress\Bundle\UserBundle\Entity\User")
** #ORM\JoinColumn(nullable=false)
*/
private $user;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #var \DateTime
*
* #ORM\Column(name="creationDate", type="datetime")
*/
private $creationDate;
/**
* #var \DateTime
*
* #ORM\Column(name="modificationDate", type="datetime")
*/
private $modificationDate;
/**
* #ORM\OneToMany(targetEntity="Moodress\Bundle\AlbumBundle\Entity\Picture", cascade={"persist"}, mappedBy="album")
*/
private $pictures;
/**
* Constructor
*/
public function __construct()
{
$this->creationDate = new \Datetime();
$this->modificationDate = new \Datetime();
}
// Get and set
}
However, When a user subscribe on the website, I create a default album called Upload that I want to keep in the user class.
This is what I tried to do :
/**
* User
*
* #ORM\Entity
* #ORM\Table(name="user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\OneToOne(targetEntity="Moodress\Bundle\AlbumBundle\Entity\Album", cascade={"persist"})
*/
protected $albumUpload;
// Get and set
}
I have this error :
Undefined index: album in /Users/Sandro/sites/moodress-website/Symfony/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php line 1608. This error appears directly when I serialize any entity that has a user object...
The error is caused by the association annotation of the $albumUpload property in the User class.
Doctrine assumes that you have a $album property in your Album entity when you use a #ORM\OneToOne(targetEntity="Moodress\Bundle\AlbumBundle\Entity\Album") association in your User entity.
Try
/**
** #ORM\ManyToOne(targetEntity="Moodress\Bundle\UserBundle\Entity\User", inversedBy="albums")
** #ORM\JoinColumn(nullable=false)
*/
private $user;
in your Album entity and
/**
* #ORM\OneToMany(targetEntity="Moodress\Bundle\AlbumBundle\Entity\Album", mappedBy="user", cascade={"persist"})
*/
protected $albums;
in your User entity. In addition please add $this->albums = new ArrayCollection() to User::__construct().
Now if you want to add and keep a default album for users you should implement this somewhere in your business logic. You could figure out a way to identify the default album in your collection of albums and prevent deletion especially for this item.
Hope this helps.
My error:
'Undefined index: album'
results from this annotation in the Album class.
/**
* #ORM\OneToMany(
* targetEntity="Moodress\Bundle\AlbumBundle\Entity\Picture",
* cascade={"persist"},
* mappedBy="album"
* )
*/
I have set mappedBy to album but there is no $album property in my Picture class.
I did add this to my Picture class and clear my cache.
/**
* #ORM\ManyToOne(
* targetEntity="Moodress\Bundle\AlbumBundle\Entity\Album",
* inversedBy="pictures"
* )
*/
protected $album;
It solved my problem
I just started working with symfony and doctrine. I have a simple entity which has one property is not tied with the database. This property should contain the contents of the xml file (I wanna make xml file, when doctrine add rows to the database).
/**
* Layouts
*
* #ORM\Table(name="layouts")
* #ORM\Entity
* #ORM\HasLifecycleCallbacks()
*/
class Layouts
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="SEQUENCE")
* #ORM\SequenceGenerator(sequenceName="layouts_id_seq", allocationSize=1, initialValue=1)
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255, nullable=false)
*/
private $name;
/**
* ???????
*/
private $template_body;
...
}
How to describe $template_body property? Without leaving the property description, I ran into a problem - the doctrine does not cause preUpdate method when I edit this property in the form.
You can do that my simply flagging a PreUpdate method in your class, which in turn begins working on your $template_body variable.
Please change
* #ORM\HasLifecycleCallbacks()
to
* #ORM\HasLifecycleCallbacks
and create a function like so..
/**
* #PreUpdate
*/
public function myUpdateFunction()
{
// Do stuff
}
I've got 2 entities: Type and Asset for which you can see the definition below.
In order to display a list of Assets in my web app I need to generate an Asset name which is a concatenation of Type->name and Asset->id (eg: cat-158 or dog-15)
I've just added a method getName() to the Asset Entity that fetches the Type name and joins it with the Asset id. Is there a way to do this with annotations so that the name is built using a join statement when fetching the Asset? Basically a property on the Asset Entity that is filled with the proper name/id combination when fetching the Asset, without fetching the type when calling getName()?
<?php
class Asset
{
/**
* #ORM\Id
* #ORM\GeneratedValue("IDENTITY")
* #ORM\Column(type="integer")
* #var integer
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="\Type")
* #var \Type
*/
private $type;
/**
* Constructs a name from the type and id
* #return string
*/
public function getName()
{
return $this->getType()->getName() . '-' . $this->getId();
}
// SNIP ...
}
class Type
{
/**
* #ORM\Id
* #ORM\GeneratedValue("IDENTITY")
* #ORM\Column(type="integer")
* #var integer
*/
private $id;
/**
* #ORM\Column(type="string", length=200, unique=true, nullable=false)
* #var string
*/
private $name;
// SNIP ...
}
Try this ?
Class Asset extends Type{
}