Symfony 3 Unique constraint error - symfony

I am getting the following error while adding a UniqueEntity constraint on the email field of my User entity.
Attempted to load class "Unique" from namespace
"\Symfony\Component\Validator\Constraints". Did you forget a "use"
statement for another namespace?
If I remove the validation the error disappears. The contents of my validation.yml file are as followed.
LB\CoreBundle\Entity\User:
constraints:
- Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity:
fields: [email]
groups: [registration,userEdit]
I have tried importing the validation constraint in my User.php file but the error still persists. Any help to get rid of this error is greatly appreciated.

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* #ORM\Entity
* #ORM\Table(name="cab_fos_user")
* #ORM\HasLifecycleCallbacks()
* #UniqueEntity("email")
* #ExclusionPolicy("all")
* #Vich\Uploadable
*/
class User extends BaseUser

Related

Symfony 3 : The annotation does not exist, or could not be auto-loaded

When I put my project into production , I get that error :
The annotation "#Symfony\Component\Validator\Constraints\type" in property does not exist, or could not be auto-loaded.
I don't understand the reason why, because when I work locally, everything is ok.
Here's my code :
namespace EC\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Validator\Constraints as Assert;
use EC\ToolsBundle\StrTools\StrTools;
/**
* User
*
* #ORM\Table(name="user")
* #ORM\Entity(repositoryClass="EC\UserBundle\Repository\UserRepository")
*/
class User extends BaseUser
{
/**
* #ORM\Column(name="nb_recette", type="integer", options={"unsigned"=true, "default" = 0})
* #Assert\type(type="integer")
*/
private $nb_recette = 0;
//...
}
Thanks for your help
try to change this:
* #Assert\type(type="integer")
to this:
* #Assert\Type(type="integer")
Annotations are case sensitive for this reason you retrieve an error
You have a typo, it should be #Assert\Type
Symfony implements PSR-4 for autoloading so if you take a look here you will notice that
All class names MUST be referenced in a case-sensitive fashion.

How update database with doctrine in Symfony2

I make an Entity with Entity generator, but now when I try command:
php app/console doctrine:schema:update --force
It throw following:
No Metadata Classes to process.
So, what I make wrong?
PS. I am also try write it self and use following command:
php app/console doctrine:generate:entities AppBundle/Entity/DataPage
And I saw this error:
[Doctrine\ORM\Mapping] \MappingException] Class "AppBundle\Entity\DataPage" is not a valid entity or mapped super class.
So there is code:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* DataPage
*/
class DataPage
{
/**
* #var integer
*/
private $id;
/**
* #var string
*/
private $sisalto;
/**
* #var string
*/
private $nimi;
}
EDIT
So, I try make this for exampe code from there
And I saw that same error:
[Doctrine\ORM\Mapping \MappingException]
Class "AppBundle\Entity\Product" is not a valid entity or mapped superclass.
/**
* DataPage
*/
class DataPage
[...]
should be
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="data_page")
*/
[...]
This is only the first step, than you have to annotate every single field of the entity that you want to appear onto db.
Please take a look here
I'm not sure if this helps anyone else, but I was getting the following error for the symfony Doctrine Product example code:
[Doctrine\ORM\Mapping\MappingException]
Class "AppBundle\Entity\Product" is not a valid entity or mapped super class.
Then I took a second look at my comments section, and it was a missing asterisk like the following code sample [The below is WRONG]:
/*
* #ORM\Entity
* #ORM\Table(name="product")
*/
class Product
Notice on line 1 there should be an extra '*' at the very end. I definitely did not think this would matter at all, but apparently it does. Also don't use tabs in front of your annotations. The Doctrine group just fixed that bug too.

Symfony UserBundle: override field name in database

In order to share the database with another, non-Symfony based application, I want to change some field names of the User table.
The custom entity is defined like this:
<?php
namespace Bcg\UserBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* #ORM\Entity
* #ORM\Table(name="User")
*/
class User extends BaseUser
...
I tried to override the name of the password field like this:
/**
* #ORM\Column(name="encrypted_password", type="string")
*/
protected $password;
but that results in an error:
MappingException: Property "password" in "Bcg\UserBundle\Entity\User" was already declared, but it must be declared only once
What am I missing?
Have you regenerated your schema using $ php app/console doctrine:schema:update --force command ?
In the end I gave up on this and changed the other application instead.

JMS Serializer: How to limit the depth of serialisation for an object graph

Maybe it is just my misunderstanding of this annotation however it does not seam to work as expected.
I have the following object graph
User
-> Company
-> Users
-> Groups
-> Permissions
As you can see there will be some recursion. JMS handles this quite well by not serialising the other user's company properties as well as not the current user.
However I want the serialization to stop at and include company.
I have tried this expecting that once the level $context->level = 2 it would stop
<?php
namespace FinalConcept\TimeTracker\EntitiesBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
/**
* FinalConcept\TimeTracker\EntitiesBundle\Entity
*
* #ORM\Table(name="users")
* #ORM\Entity(repositoryClass="FinalConcept\TimeTracker\EntitiesBundle\Repository\UserRepository")
*/
class User implements UserInterface, \Serializable
{
/**
* #ORM\ManyToOne(targetEntity="company", inversedBy="users")
* #ORM\JoinColumn(name="company_id", referencedColumnName="id")
* #JMS\MaxDepth(depth=1)
*/
private $company;
}
However this is not the case. Even stepping through the code has not shed any light on how to stop this.
I am happy to create a custom handler if I can only invoke it for a specific path i.e. User.Company
I also need this for User.Groups which has the following graph
User
-> Groups
-> Permissions
-> Users
-> Groups
-> users ....
Thanks in advance for any help how to limit the depth of serialization for an object graph
Because I did not have access to the latest version of the serializer I had to find a workaround to the #MaxDepth. This may also help you.
Use #JMS\ExclusionPolicy("all") on all entities that are connected.
Now use #JMS\Expose only on those properties that you want to have serialize.
On join relations only use this annotation in one direction. That will fix your problem.
namespace FinalConcept\TimeTracker\EntitiesBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as JMS;
/**
* FinalConcept\TimeTracker\EntitiesBundle\Entity
*
* #JMS\ExclusionPolicy("all")
* #ORM\Table(name="users")
* #ORM\Entity(repositoryClass="FinalConcept\TimeTracker\EntitiesBundle\Repository\UserRepository")
*/
class User implements UserInterface, \Serializable
{
/**
* #JMS\Expose
* #ORM\ManyToOne(targetEntity="company", inversedBy="users")
* #ORM\JoinColumn(name="company_id", referencedColumnName="id")
*/
private $company;
}
As of the latest version, using #MaxDepth() annotation and SerializationContext::create()->enableMaxDepthChecks() in the controller does the job.

symfony2 UniqueEntity constraint on username not working

I am trying to implement something as common as preventing creation of users with same username. The UniqueEntity constraint has worked for me on other entities, but I guess the user is special since it extends an already existing entity.
Code below shows clearly what I want to do. But it behaves as if the constraint wasn't there at all (of course ending up at a MySql error instead since DB won't allow duplicate entries of username).
Could it have something to do with the fact that the username property isn't declared in this file but merely inherited since I am extending the FOS\UserBundle\Model\User? If so how do I get around that?
// src/BizTV/UserBundle/Entity/User.php
namespace BizTV\UserBundle\Entity;
use BizTV\UserBundle\Validator\Constraints as BizTVAssert;
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
//use FOS\UserBundle\Entity\User as BaseUser; //deprecated
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use BizTV\BackendBundle\Entity\company as company;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
* #UniqueEntity(fields = "username", message = "En användare med det namnet finns redan, försök igen.")
*/
class User extends BaseUser implements AdvancedUserInterface
You have to set also at the property username the unique=true flag.
Something like this:
* #ORM\Column(name="username", unique=true)

Resources