we are combining FOSUserBundle with LiipOneallBundle and sometimes we do not have an email for the user. in this case we would like to leave the email at NULL and therefore also the canonical email should be NULL. unique constraints allow multiple NULL values so it shouldnt cause issues with the unique constraint.
I think it may be possible using the AttributeOverrides annotation to override the nullable flag and changing the validation.
However, I'm not sure about making it optional by default in the bundle: some of the features of the bundle simply cannot work without an email.
AttributeOverrides did the job:
use Doctrine\ORM\MappingAttributeOverrides;
use Doctrine\ORM\Mapping\AttributeOverride;
/**
* @ORM\Entity
* @ORM\Table(name="fos_user")
*
* @AttributeOverrides({
* @AttributeOverride(name="emailCanonical",
* column=@ORM\Column(
* name="emailCanonical",
* type="string",
* length=255,
* unique=false
* )
* )
* })
*/
class User extends BaseUser
{
This is more problematic with the MongoDB ODM backend though.
There doesn't seem to be an AttributeOverrides annotation that can be used there.
As a workaround I've been patching Resources/config/doctrine/User.mongodb.xml to get rid of the unique index on emailCanonical.
Why? Some of the users have no email address associated with them at all, and several objects in the MongoDB collection cannot have the same emailCanonical value (NULL). This differs from the MySQL behavior, where multiple NULL values are not considered duplicates.
AttributeOverrides is not working in Symfony 2.3.4 and FosUserBundle 1.2
Tired to override emailCanonical field as @triggertoo did but I'm getting an exception:
[Doctrine\ORM\Mapping\MappingException]
Invalid field override named 'emailCanonical' for class 'Lukit\FleetBundle\Entity\User'
AttributeOverrides should not be related at all to the version of Symfony, as it does not depend on Symfony at all.
Btw, you should not be using FOSUserBundle 1.2 for symfony 2.3 but FOSUserBundle 1.3 (1.2 is for symfony 2.0 and will not work with any later versions because of the Symfony 2.1 Form changes)
Hi all,
I'm having the same issue for my project. I'm using FOSUB to manage my users, but not all of them have an email address and I would like to make the email property nullable.
I used the solution given by triggertoo few months ago and it works until I tried to upgrade my application to SF2.4
By now I have the following error :
fatal error: uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Invalid field override named 'email' for class 'xxx'
Do you have any suggestion to override the User class mapping?
Same issue when I'm try to execute "doctrine:generate:entities".
"Invalid field override named 'email' for class 'My\User\Class'"
Same problem here with the AttributeOverride.
If you put a print_r($this->fieldMappings) in the setAttributeOverride() method of Doctrine\ORM\Mapping\ClassMetadaInfo, it seems to be triggered for each class extending FOSUser (in my case I have a Class A extends Class B extends FOSUser, which triggers the method 3 times). The print_r first display FOSUser fields, then for each extending classes, its own fields.
Thing is it should be triggered only once in a normal case (with fieldMappings containing all the fields), no idea why it does not.
Did anyone find a solution to this?
up :)
Up. I reach the same conclusion as @NV-iloweb
Override attributes is done via annotations, but when you try to update the following command gets the essence of the exception:
php app/console doctrine:generate:entities AppUserBundle:User --no-backup
[Doctrine\ORM\Mapping\MappingException]
Invalid field override named 'username' for class 'App\UserBundle\Entity\User'.
And when you try to update the table no problem:
php app/console doctrine:schema:update --force
Updating database schema...
Database schema updated successfully! "4" query was executed
namespace App\UserBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* @ORM\Table(name="users")
* @ORM\Entity(repositoryClass="App\UserBundle\Entity\UserRepository")
* @ORM\AttributeOverrides({
* @ORM\AttributeOverride(name="username",
* column=@ORM\Column(
* nullable = true
* )
* ),
* @ORM\AttributeOverride(name="usernameCanonical",
* column=@ORM\Column(
* name = "username_canonical",
* nullable = true
* )
* ),
* @ORM\AttributeOverride(name="email",
* column=@ORM\Column(
* nullable = true
* )
* ),
* @ORM\AttributeOverride(name="emailCanonical",
* column=@ORM\Column(
* name = "email_canonical",
* nullable = true
* )
* )
* })
*/
class User extends BaseUser
{
...
}
composer.json
"require": {
"doctrine/orm": "~2",
"doctrine/doctrine-bundle": "~1",
...
}
Why is the table updated but failed to generate the entity?
Same problem, still no solution?
Up. I'm having the same problem - I rely on a social register service to provide access to an application and I need to create unique users based on their tokens, not on their email. Email is optional and most of the time is null.
I have tried everything so far - the closest I came to my business requirement is the situation described above by @12th
Entity Yaml config file:
GabiU\UsersBundle\Entity\User:
type: entity
table: fos_user
id:
id:
type: integer
generator:
strategy: AUTO
attributeOverride:
email:
unique: true
nullable: true
ignoreNull: true
emailCanonical:
unique: true
nullable: true
ignoreNull: true
password:
nullable: true
php app/console doctrine:schema:update --dump-sql
ALTER TABLE fos_user CHANGE email email VARCHAR(255) DEFAULT NULL, CHANGE email_canonical email_canonical VARCHAR(255) DEFAULT NULL;
CREATE UNIQUE INDEX UNIQ_957A6479E7927C74 ON fos_user (email);
php app/console doctrine:generate:entities GabiU --no-backup
Generating entities for namespace "GabiU"
[Doctrine\ORM\Mapping\MappingException]
Invalid field override named 'email' for class 'GabiU\UsersBundle\Entity\User'.
basically, if I get it right, the only way to make sure this works without warnings is to stop inheriting BaseUser from FOS and create it myself (by copy paste)?
any help is much appreciated :)
Also, I have found this in Doctrine's documentation: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/inheritance-mapping.html#attribute-override
If I get it right, this means I should change the type of the baseUser from entity to type: mappedSuperclass in order to make these overwrites work. I don't know how good is this idea, given FOSUB is a vendor bundle and the modification could be overwritten by a simple composer update.
Bumping. Same issue here when trying to run doctrine:generate:entities having overridden the emailCanonical column:
[Doctrine\ORM\Mapping\MappingException]
Invalid field override named 'emailCanonical' for class 'AppBundle\Entity\User'.
A fix for this issue is rather simple. If you use version 1.3.6 of the FOSUserBundle, make sure you _use_
FOS\UserBundle\Entity\User
as the superclass for your subclass, instead of _using_
FOS\UserBundle\Model\User
If you change that, you can simply use
<attribute-overrides>
<attribute-override name="email">
<field column="email" nullable="true"></field>
</attribute-override>
<attribute-override name="emailCanonical">
<field column="email_canonical" nullable="true"></field>
</attribute-override>
</attribute-overrides>
in your User.orm.xml (or find your PHP / YML equivalent in the docs)
@Craimasjien I already use FOS\UserBundle\Entity\User as the superclass for my subclass. I tried using Model\User just in case you got them the wrong way round but the error is presented either way.
Still having this issue! Using FOSUserBundle 2.0@dev and Symfony 2.7.
extending FOSUSer User Model getting this error:
[Doctrine\ORM\Mapping\MappingException] Invalid field override named 'username' for class 'Admin\CustomerBundle\Entity\Customer'.
Symfony 3.0.1 still having this issue
extending FOS\UserBundle\Entity\User will do the work for now, but need to think about a mere working solution.
I just try, I have the same issue.
@AienTech said:
extending FOS\UserBundle\Entity\User will do the work for now, but need to think about a mere working solution.
FOS\UserBundle\Entity\User does not exist (anymore?). We're stuck with FOS\UserBundle\Model\User. How do we solve the "generate:entities" bug? (Schema update works fine)
PS: Is this bug related to https://github.com/doctrine/doctrine2/issues/3433 ? Because if so, according to that, it should be fixed and I'm a little bit more confused......
How do we solve the "generate:entities" bug? (Schema update works fine)
@xDaizu this has nothing to do with this ticket
@stof you're certainly right, it doesn't, but it has to do with the workaround... and since no one is talking about the fix, the workaround will have to do for the time being... won't it? :sweat_smile:
So did anyone come up with proper solution or workaround?
@murnieza For now, there is no proper solution.
But, as @stof pointed it, this issue is directly related to doctrine and the AttributeOverrides behavior, as it should be reproducible for any model that is not part of a 3rd party bundle (is there someone that already given a try between two "local" model?).
My workaround is to call setEmail() systematically from the setUsername() method (or constructor), with the username as value (it's sufficient to my need).
Also, as you are trying to make the email field nullable, you can set a default value that you can use as "null", i.e. ('empty' == $user->getEmail()) ? 'Has no email' : 'Has email'; instead of (null === $user->getEmail()).
It's quick and dirty, but should do the trick.
+1
@stof @murnieza
I am currently using two entity managers in my doctrine conf named 'default' and 'global'.
I have the same error [Doctrine\ORM\Mapping\MappingException] Invalid field override named 'email'
when doctrine try to search for mapping of the FOSUserBundle as it is not present in the chain.
My solution is to change the mapping for only the second entity manager named 'global' from:
FOSUserBundle: ~
to
FOSUserBundle:
type: xml
dir: '%kernel.root_dir%/../vendor/friendsofsymfony/user-bundle/Resources/config/doctrine-mapping'
prefix: FOS\UserBundle\Model
is_bundle: false
I solved the problem with generate:entities by commenting out the attributeOverride just to generate the entity and then uncomment it.
I have the same issue with symfony 3.1 and doctrine 2.5, If I override the field with annotations, give me:
Invalid field override named emailCanonical for class AppBundle\Entity\Usuario
And if I define the field in the entity:
Duplicate definition of column 'email_canonical' on entity 'AppBundle\Entity\Usuario' in a field or discriminator column mapping
any updates?
I am having the same major problem at the moment. Symfony 2.8 and FOSUserBundle v1.3.7.
The problem is I can not even run doctrine:schema:update as this already throws the error.
Uncaught exception 'Doctrine\ORM\Mapping\MappingException' with message 'Invalid field override named 'email' - i'm extending FOS\UserBundle\Entity\User
use FOS\UserBundle\Entity\User as FosBaseUser;
* @ORM\Entity
*
* @ORM\AttributeOverrides({
* @ORM\AttributeOverride(name="email",
* column=@ORM\Column(
* nullable = true
* )
* ),
* @ORM\AttributeOverride(name="emailCanonical",
* column=@ORM\Column(
* name = "email_canonical",
* nullable = true
* )
* )
* })
class User extends FosBaseUser
Is there still no fix or workaround?
Same with Symfony 3.3, Doctrine ORM 2.5 and FOSUserBundle 2.0
Same problem here but it's definitely between AttributeOverride and the generate entities command that something goes wrong.
Our quickfix for now is to disable AttributeOverride annotation before generating entities getters and setters.
The annotations AttributeOverride are the right fix to extend User or Group attribute defined in doctrine-mapping XML files.
Thanks @ThomasLabstep .
So I guess updating the database with doctrine:schema:update --force is ok .
Then do you know if FOSUserBundle works fully, or at least, as much as before you put AttributeOverride annotations ?
Without override:
ALTER TABLE fos_group CHANGE name name VARCHAR(255) NOT NULL;
CREATE UNIQUE INDEX UNIQ_4B019DDB5E237E06 ON fos_group (name);
With override:
Nothing to update - your database is already in sync with the current entity metadata.
My annotation looks like this:
* @ORM\AttributeOverrides({
* @ORM\AttributeOverride(name="name",
* column=@ORM\Column(
* nullable = true,
* unique = false,
* length = 255
* )
* )
* })
And then
php app/console doctrine:generate:entities YourCompany
Invalid field override named 'name' for class 'YourCompany\UserBundle\Entity\Group'
Ok, thanks.
It's more tricky to put email and username nullable + unique = false.
My main concern was the general feature of FOSUser if we do allow empty email (+canonical) and username(+canonical). Is everything (reset pass, activate,update,create ...) working well with these annotations ?
That's what I'm testing now.
Yes you should all FOSUserBundle controller endpoints and services methods with your change. It might break.
The relevant issue is also described here:
https://github.com/doctrine/doctrine2/issues/4734
OK I found the fix deep buried into Doctrine2 github tickets.
https://github.com/doctrine/doctrine2/issues/3433
You need to modify manually a file in vendor to fix the issue. It actually generated getters and setters that were previously ignored entirely by the generate entity command.
In Doctrine\Common\Persistence\Mapping\StaticReflectionService
Replace:
public function getParentClasses($class)
{
return [];
}
By:
public function getParentClasses($class)
{
if ( ! class_exists($class)) {
throw MappingException::nonExistingClass($class);
}
return class_parents($class);
}
Compare
And
Going back up, the problem is here:
Doctrine\Bundle\DoctrineBundle\Command\GenerateEntitiesDoctrineCommand::execute
protected function execute(InputInterface $input, OutputInterface $output)
{
$manager = new DisconnectedMetadataFactory($this->getContainer()->get('doctrine'));
...
DisconnectedMetadataFactory won't return the parentClasses because it used Static instead of Runtime implementation.
Ok, awesome.
We have a workaround until a fix for it.
I am facing the same problem. What is the best way to use the workaround? I dont want to implement it after every composer update/install. Whats the best practice or do we have to wait until doctrine fixes it?
@meistersoda concerning me, the only thing I had to do is to remove
@AttributeOverrides({
@AttributeOverride(name="emailCanonical",
column=@ORM\Column(
name="emailCanonical",
type="string",
length=255,
unique=false
)
)
})
every time I want to regenerate entities.
It works then when you want to update your database schema.
@fredericlam That's so simple I didn't even think about it.
Thanks! ;)
March 2018
Still have this issue.
my work around is by removing the @AttributeOverrides like what @armandobasha did.
Hope they fix this issue.
+up
Most helpful comment
AttributeOverrides did the job:
use Doctrine\ORM\MappingAttributeOverrides;
use Doctrine\ORM\Mapping\AttributeOverride;