I want to extend default user entity attributes. In database after extending FOSUserBundle with SonataUserBunle there are 2 tables for storing users: fos_user and fos_user_user. I want to extend fos_user
Here is app/AppKernel.php:
new Sonata\UserBundle\SonataUserBundle('FOSUserBundle'),
new Application\Sonata\UserBundle\ApplicationSonataUserBundle(),
in app/config.yml file I set:
fos_user:
db_driver: orm<br>
firewall_name: main<br>
user_class: Acme\DemoBundle\Entity\User<br>
group:<br>
group_class: Application\Sonata\UserBundle\Entity\Group<br>
sonata_user:
security_acl: true
class:
user: Acme\DemoBundle\Entity\User
admin:
user:
class: Acme\DemoBundle\Admin\UserAdmin
parameters:
sonata.user.admin.user.class: Blogger\BlogBundle\Admin\UserAdmin
sonata.user.admin.user.entity: Blogger\BlogBundle\Entity\User
doctrine:
orm:
auto_mapping: auto
SonataUserBundle is created in src/Application/Sonata/UserBundle
To override SonataUserBundle, i extended UserAdmin by creating another UserAdmin class in src/Acme/DemoBundle/Admin/UserAdmin and all works fine
Now I want to extend User entity (fos_user table) to add new attributes.
Here is my Acme/Demo/Entity/User.php that I want to extend the default User entity
namespace Acme\DemoBundle\Entity;
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/*
* #ORM\Column(type="string", name="newAttribute")
*/
protected $newAttribute;
public function __construct()
{
parent::__construct();
// your own logic
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
What should I do to extend default fos_user table with php app/console doctrine:generate:entities Acme/DemoBundle
I've read many posts about this issue but none of the solutions helped me. I tried to extend Model class instead of Entity class but also nothing changed
You don't have to.
new Sonata\UserBundle\SonataUserBundle('FOSUserBundle')
Here your are telling the SonataUserBundle to use the FOSUserBundle as well.
Then in your User entity you set:
use Sonata\UserBundle\Entity\BaseUser as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="User")
*/
class User extends BaseUser
Because you have said to the SonataUserBundle "use the fosuserbundle" and you extend from the sonata user bundle user entity he's gonna "merge" those 2 models into 1 and add your custom fields from in your user entity as well.
Related
I'm using Symfony 3.4 and knp doctrine behaviors for translation.
My entity Article looks like:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
/**
* Article
*
* #ORM\Table(name="article")
* #ORM\Entity(repositoryClass="AppBundle\Repository\ArticleRepository")
*/
class Article
{
use ORMBehaviors\Translatable\Translatable;
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
//...
}
Then I have entity ArticleTranslation
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Knp\DoctrineBehaviors\Model as ORMBehaviors;
/**
* #ORM\Entity
*/
class ArticleTranslation
{
use ORMBehaviors\Translatable\Translation;
/**
* #ORM\Column(type="string", length=128)
*/
protected $headline;
//...
}
Now my app throws me an error:
Unable to find the association target class of "headline" in AppBundle\Entity\Article.
It expects a relation between Article and ArticleTranslation. There is a sentence in the documentation:
The default naming convention (or its customization via trait methods) avoids you to manually handle entity associations. It is handled automatically by the TranslationSubscriber.
Why does this happen? What am I missing?
edit
bin/console doctrine:schema:update
[OK] Nothing to update - your database is already in sync with the current entity metadata.
bin/console debug:config knp_doctrine_behaviors
knp_doctrine_behaviors:
translatable: true
blameable: false
geocodable: false
loggable: false
sluggable: false
soft_deletable: false
sortable: false
timestampable: false
tree: false
I'm using this in sonata admin, with a2lix translatable.
ArticleAdmin.php:
<?php
namespace AppBundle\Admin;
use Sonata\AdminBundle\Admin\AbstractAdmin;
use Sonata\AdminBundle\Datagrid\ListMapper;
use Sonata\AdminBundle\Datagrid\DatagridMapper;
use A2lix\TranslationFormBundle\Form\Type\TranslationsType;
final class ArticleAdmin extends AbstractAdmin
{
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->add('headline', TranslationsType::class);
}
//...
}
Try to use
$formMapper->add('translations', TranslationsType::class);
I had the same problem. Probably the field name needs to be exactly translations.
I am wondering if there is a way to change (or define inside) annotations in a child class inheriting a MappedSuperClass, for example, let say we have a class BaseUser (mappedsuperclass), a child class User :
<?php
...
use Doctrine\ORM\Mapping as ORM;
use JMS\Serializer\Annotation as Serializer;
...
/**
* #ORM\MappedSuperclass
*/
class BaseUser
{
/**
* #ORM\Column(name="firstname", type="string", length=100)
* #Serializer\SerializedName("First_Name")
* #Serializer\Expose
* #Serializer\Type("string")
* #Serializer\Groups({"Basic"})
*/
protected $firstName;
}
/**
* #ORM\Entity
*/
class User extends BaseUser
{
/**
* #ORM\Column(name="sign", type="string", length=50)
*/
private $sign;
}
What I would like to do is either defining the "Serializer" annotations from User class directly (but let the property firstName to be defined in the BaseUser class), OR, overriding the definition of Serialize from the User class.
I did not found anything about this topic, has someone already figured it out? Thanks
You can tell the JMS Serializer what to expose or not in your config.
app/config/config.yml:
jms_serializer:
metadata:
directories:
- { path: %kernel.root_dir%/Resources/FOSUserBundle/serializer, namespace_prefix: 'FOS\UserBundle' }
app/Resources/FOSUserBundle/serializer/Model.User.yml:
FOS\UserBundle\Model\User:
exclusion_policy: ALL
properties:
id:
expose: true
email:
expose: true
username:
expose: true
enabled:
expose: true
locked:
expose: true
Source: https://github.com/schmittjoh/JMSSerializerBundle/issues/78#issuecomment-31831236
I have one table for which I am creating two entities.
One entity is used in my one core bundle which is used for many projects.
For new project I am creating new bundle and I want to add one more column in that entity.
Can anyone please guide me how can I override entity in other bundle.
You can extend your original User entity to something like this.
namespace XXXX;
use Doctrine\ORM\Mapping as ORM;
use XXXXX as BaseUser;
/**
* #ORM\Entity
* #ORM\Table(name="User")
*/
class User extends BaseUser
{
/**
* #var string
*
* #ORM\Column(name="newProperty", type="string", nullable=false)
*/
private $newProperty;
.....
.....
}
This will have all properties from your class BaseUser and you can have additional properties in this new class that you are trying to create. You can add set and get methods here too.
I want to expose only a few properties of my User class, using JMSSerializerBundle and FOSRestBundle.
It seems like the serializer bundle is not reading my configuration file.
My User class is in src/AppBundle/Entity/User and it extends the FOSUserBundle user class.
Here is my User class:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use Symfony\Component\Validator\Constraints as Assert;
/**
* User
*
* #ORM\Table(name="backoffice_user")
* #ORM\Entity(repositoryClass="AppBundle\Entity\Repository\UserRepository")
*/
class User extends BaseUser
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="lastname", type="string", length=70)
*/
private $lastname;
/**
* #var string
*
* #ORM\Column(name="firstname", type="string", length=70)
*/
private $firstname;
}
This is my app/config.yml file
jms_serializer:
metadata:
debug: true
auto_detection: true
And the yml mappgin file in src/AppBundle/Resources/config/serializer/Entity.User.yml :
AppBundle\Entity\User:
exclusion_policy: ALL
exclude: true
properties:
email:
exclude: true
expose: true
The file is not read (or at least not taken into account), because my API returns me all fields of my entity.
Am I forgetting something ?
Your mapping file is not loaded, because the serializer applies the rules to the class where the properties are defined. In your case - that is FOS\UserBundle\Model\User. What you need is to override the Third-Party Metadata - a brief sample can be seen in bundle's documentation
In your config.yml, jms_serializer should probably look like this:
jms_serializer:
metadata:
auto_detection: true
directories:
FOSUserBundle:
namespace_prefix: "FOS\\UserBundle"
path: "#AppBundle/Resources/config/serializer"
Inside directory serializer you should have a file named Model.User.yml with configuration like this:
FOS\UserBundle\Model\User:
exclusion_policy: ALL
# add your desired configuration below.
In JMSSerializer you need to have one file for each class of the hierarchy.
So, the exclusion policy only apply to properties id, lastname and firstname; for BaseUser it use the default rule, which is to include everything.
To override a configuration file from another bundle, you first need to setup the folder for the namespace in the configuration with:
jms_serializer:
metadata:
directories:
FOSUB:
namespace_prefix: "FOS\\UserBundle"
path: "%kernel.root_dir%/serializer/FOSUB"
And add the serializer config file in app/serializer/FOSUB/Model.User.yml. See the official docs for other configurations option.
i have downloaded FOSUserBundle and configure it but i am facing the issue in updating the schema using following command
php app/console doctrine:schema:update
i am getting following exception for my existing database
[Doctrine\DBAL\Schema\SchemaException]
The table with name 'hub.tb_business_info' already exists.
tb_business_info is my existing table which was created by doctrine
so how would i create the User table in my existing database
Updated , here is my ORM defination
use FOS\UserBundle\Entity\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
public function __construct()
{
parent::__construct();
// your own logic
}
}