Sonata user bundle and FosUserBundle - symfony

At the beginning I installed FosUserBundle and extended the base user class:
namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #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", length=255)
*
* #Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
* #Assert\Length(
* min=3,
* max=255,
* minMessage="The name is too short.",
* maxMessage="The name is too long.",
* groups={"Registration", "Profile"}
* )
*/
protected $name;
/**
* #ORM\OneToMany(targetEntity="House", mappedBy="User")
*/
private $houses;
. . .
after that I installed sonata admin bundle,sonata easy extend and sonata user bundle. The problem now is that when I try to go to /register page
I have this error:
Neither the property "name" nor one of the methods "name()", "getname()"/"isname()" or "__call()" exist and have public access in class "Symfony\Component\Form\FormView" in FOSUserBundle:Registration:register_content.html.twig at line 36
I am bit confused, which entity user should I implement (FosUSer or Sonata)?

Related

Get FosUser authenticated to persist into Easy Admin Entity ( USER LOG)

I have An entity Product Manged By EasyAdmin, i ALSO USE fos UserbUNDLE for user Management.
Everything works fine but when i add a new user in easyadmin i have a dropdown of user but i want to have automatically the authenticated user.
I have defined an one to Many relation between User and Product in an attribut of the class Product.
/**
* #ORM\ManyToOne(targetEntity="User")
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
*/
private $user;
Product Entity:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\Validator\Constraints as Assert;
/**
* Product
*
* #ORM\Table(name="product")
* #ORM\Entity(repositoryClass="AppBundle\Repository\ProductRepository")
* #Vich\Uploadable
*/
class Product
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="reference", type="string", length=150)
*/
private $reference;
/**
* #ORM\ManyToOne(targetEntity="Type")
* #ORM\JoinColumn(name="type_id", referencedColumnName="id")
*/
private $type;
/**
* #ORM\Column(type="boolean")
*/
public $status;
/**
* #var string
*
* #ORM\Column(name="titre", type="string", length=150)
*/
private $titre;
User :
<?php
namespace AppBundle\Entity;
use FOS\UserBundle\Model\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
}
}
How to get it automatically?
Can we have more details ? Both entities and controller because i think your not using the good targetEntity
i had quite the same issue,i mean,in some cases i was needing to get ONLY THE CURRENT LOGGED USER INFO INSIDE MY SELECT, and sometimes i was needing to get the full list of users from the database
maybe the solution i found could help you:
Get the logged user info in the easyadmin select
to make it simple you have to override the easyadmin formbuilder and ask only the currently logged user info
$formBuilder->add('user', EntityType::class, array(
'class' => 'AppBundle:User',
'query_builder' => function (EntityRepository $er) {
return $er->createQueryBuilder('u')
->where('u.id = :id')->setParameter('id',$this->getUser()->getId());
},
));
return $formBuilder;

Case mismatch between loaded and declared class names: "Doctrine\ORM\Mapping\ManytoOne" vs "Doctrine\ORM\Mapping\ManyToOne"

I am getting error of Case mismatch between loaded and declared class names: "Doctrine\ORM\Mapping\ManytoOne" vs "Doctrine\ORM\Mapping\ManyToOne" with User Entity only. Same Code with other entities is working fine.
Here is my Comp Entity
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Comp
*
* #ORM\Table(name="comp")
* #ORM\Entity(repositoryClass="AppBundle\Repository\CompanyRepository")
*/
class Comp
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var integer
* #ORM\ManytoOne(targetEntity="User")
* #ORM\JoinColumn(name="user_id",referencedColumnName="id", nullable=true)
*/
private $user;
My User Entity is
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="user")
* #ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
*/
class User extends BaseUser
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
and I am getting error of
Case mismatch between loaded and declared class names: "Doctrine\ORM\Mapping\ManytoOne" vs "Doctrine\ORM\Mapping\ManyToOne"
while updating schema
The same code is working file with other entities. I manage to make foreign keys. Cannot understand what is going wrong with User field only.
try to change this:
* #ORM\ManytoOne(targetEntity="User")
to this:
* #ORM\ManyToOne(targetEntity="User")
Annotations are case sensitive for this reason you retrieve an error

Validate the combination of 2 filed in same Entity

I have an entity called Note , for every Note ther's etudiant_id and evaluation_id .
I want that the combination of etudiant_id and evaluation_id should be unique .
For exmple when i give him a Note object1(etudiant_id=1,evaluation_id=1) and Note2 object2(etudiant_id=1,evaluation_id=1) : this should throw an exception. but
in case of :
Note object1(etudiant_id=1,evaluation_id=2) and Note2 object2(etudiant_id=1,evaluation_id=1) : this will not throw anything .
By the way i tried :* #UniqueEntity(fields={"evaluation","etudiant"}) but this didnt work correcte.
Here's my Note Entity :
<?php
namespace EcoleBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* Note
* #ORM\Entity
* #UniqueEntity(fields={"evaluation","etudiant"})
* #ORM\Table(name="note")
* #ORM\Entity(repositoryClass="EcoleBundle\Repository\NoteRepository")
*/
class Note
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var float
*
* #ORM\Column(name="valeur", type="float", nullable=true)
*/
private $valeur;
/**
* #ORM\ManyToOne(targetEntity="EcoleBundle\Entity\Evaluation", inversedBy="notes")
*/
private $evaluation;
/**
* #ORM\ManyToOne(targetEntity="EcoleBundle\Entity\Etudiant", inversedBy="notes")
*/
private $etudiant;
You should have only one #Entity annotation. Try to substitute with this and clean the cache
/**
* Note
* #ORM\Entity(repositoryClass="EcoleBundle\Repository\NoteRepository")
* #UniqueEntity(fields={"evaluation","etudiant"})
* #ORM\Table(name="note")
*/

FOSUserBundle and custom fields in user entity

I'm using FOSUserBundle with custom fields in my user class.
<?php
namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
* #ORM\Entity(repositoryClass="AppBundle\Entity\UserRepository")
*/
class User extends BaseUser {
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string", length=255)
*
* #Assert\NotBlank(message="Please enter your name.", groups={"Registration", "Profile"})
* #Assert\Length(min="3", max="255", groups={"Registration", "Profile"})
*/
protected $name;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Message", mappedBy="fromUser")
*/
protected $sentMessages;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Message", mappedBy="toUser")
*/
protected $receivedMessages;
/**
* #ORM\Column(type="date")
*/
protected $dateRegistered;
/**
* #ORM\Column(type="string", length=250, nullable=true)
*
*/
protected $banReason;
public function __construct() {
parent::__construct();
$this->dateRegistered = new \DateTime();
}
/* getters and setters */
}
I want to show a personal message to the locked users, filling $banReason field.
I found that error.user variable contains part of the user entity, but my field is set to null instead of it's real value.
Sorry, but i'm kinda noob in hooks and services. :-(
Have a nice day!

symfony2 how to override the User class with FosuserBundle?

My problem is quite simple but i search for an hour without succeed, i would like to add some constraint to the UserEntity. For example, i would like to limit the length of a username
I think the best way is to not touche FOS in Vendor. I have create my own userBundle with my own layout.html etc ... But i cannot override attribut who is already existing in FosuserBundle (it's working for the layout overriding btw my userBundle is a child of FOS)
the funny thing is "id" has no problem for the overriding
My User entity :
<?php
namespace Diane\UserBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Diane\UserBundle\Entity\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="username", type="string", length=10)
*/
protected $username;
}
Fos User model :
<?php
namespace FOS\UserBundle\Model;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
abstract class User implements UserInterface, GroupableInterface
{
protected $id;
/**
* #var string
*/
protected $username;
...
}
i have already try to remove :
/**
* #var string
*/
Error message :
Doctrine\ORM\Mapping\MappingException] Duplicate definition of column
'username' on entity 'Diane\UserBundle\Entity\User' in a field or
discriminator column mapping.
Thanks for any idea you could give me
Try AttributeOverrides Doctrine annotation:
/**
* User
*
* #ORM\Table()
* #ORM\Entity(repositoryClass="Diane\UserBundle\Entity\UserRepository") / class User extends
*
* #ORM\AttributeOverrides({
* #ORM\AttributeOverride(name="username",
* column=#ORM\Column(
* name = "username",
* type = "string",
* length = 10
* )
* )
* })
*
*/
class User extends BaseUser
{

Resources