Symfony UserBundle: override field name in database - symfony

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.

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.

Symfony 3 Unique constraint error

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

Check whether a newly created object is unique by several fields

I have to check a submitted form against the existing database to make sure it is not duplicated by the combination of several fields, say supplier_id, invoice_no and amount.
Is there a built-in method or should I write a code myself? If so - what are guidelines: where to put it, what are the good practices?
At the moment I am extending the CRUD controller and overwriting the createAction adding the condition there. Not sure whether this method is a good practice.
Example:
<?php
namespace AppBundle\Entity\User;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* #ORM\Entity
* #UniqueEntity({"name", "email"}, message="This value is in a database.")
*/
class User
{
/**
* #var string
*/
protected $name;
/*
* #var string
*/
protected $email;
...
}

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.

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