I have just installed FosUserBundle in a Symfony version 2.3.3 application.
I'm able to manage users, login, register and whatnot but changes I make to my User class in MyApp\UserBundle\Entity aren't being applied when I run app/console doctrine:schema:update
The database structure contains:
id,
username,
username_canonical,
email,
email_canonical,
enabled,
salt,
password,
last_login,
locked,
expired,
expires_at,
confirmation_token,
password_requested_at,
roles,
credentials_expired,
credentials_expire_at
The field that I have added, rate, isnt being created in the database structure no matter what I do.
Inside /app/AppKernel.php
new MyApp\UserBundle\MyAppUserBundle(),
new FOS\UserBundle\FOSUserBundle(),
Inside /app/config/config.yml
fos_user:
db_driver: orm
firewall_name: main
user_class: MyApp\UserBundle\Entity\User
registration:
confirmation:
enabled: true
And inside /src/MyApp/UserBundle/Entity/User.php
<?php
namespace MyApp\UserBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*
* #ORM\Table(name="fos_user")
* #ORM\Entity(repositoryClass="MyApp\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="token", type="string", length=255)
*/
private $token;
/**
* #var integer
*
* #ORM\Column(name="rate", type="integer")
*/
private $rate;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set token
*
* #param string $token
* #return User
*/
public function setToken($token)
{
$this->token = $token;
return $this;
}
/**
* Get token
*
* #return string
*/
public function getToken()
{
return $this->token;
}
/**
* Set rate
*
* #param integer $rate
* #return User
*/
public function setRate($rate)
{
$this->rate = $rate;
return $this;
}
/**
* Get rate
*
* #return integer
*/
public function getRate()
{
return $this->rate;
}
}
Don't forget the constructor, clear the cache and try php app/console doctrine:schema:update --force
namespace Acme\UserBundle\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
}
}
Need to add MyUserBundle mappings to your config.yml file:
doctrine:
orm:
mappings:
FOSUserBundle: ~
MyUserBundle: ~
Related
When a try update my database I got error:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
(symfony.#sql-d8c_55, CONSTRAINT FK_957A6479A233CB39 FOREIGN KEY
(klient_id) REFERENCES klient (id))
My class user:
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* Class User
* #package AppBundle\Entity
*
* #ORM\Table("fos_user")
* #ORM\Entity()
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Klient", inversedBy="users")
* #ORM\JoinColumn(nullable=false)
*/
private $klient;
/**
* #return mixed
*/
public function getKlient()
{
return $this->klient;
}
/**
* #param mixed $klient
*/
public function setKlient($klient)
{
$this->klient = $klient;
}
public function __construct()
{
parent::__construct();
}
}
class Klient
namespace AppBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Klient
*
* #ORM\Table(name="klient")
* #ORM\Entity(repositoryClass="AppBundle\Repository\KlientRepository")
*/
class Klient
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="nazwa", type="string", length=255, unique=true)
*/
private $nazwa;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\User", mappedBy="klient")
*/
private $users;
/**
* Get id
*
* #return int
*/
public function getId()
{
return $this->id;
}
/**
* Set nazwa
*
* #param string $nazwa
*
* #return Klient
*/
public function setNazwa($nazwa)
{
$this->nazwa = $nazwa;
return $this;
}
/**
* Get nazwa
*
* #return string
*/
public function getNazwa()
{
return $this->nazwa;
}
public function __construct()
{
$this->users = new ArrayCollection();
}
}
in my opinion you have this error because you already have datas in your database. When you try to add the foreign key on your user table, kcient_id is null. And in your definition, you specify nullable: false.
I suggest you proceed in two times.
Edit your annotation to nullable: true, update your database and link your client to klient
Re-edit your annotation to nullable: false, it should be ok
I have problem with validation and schema creation, error log is:
[Mapping] FAIL - The entity-class 'App\Api\Entity\User' mapping is invalid:
* The referenced column name 'id' has to be a primary key column on the target entity class 'App\Acme\Entity\User\BaseUser'.
* The join columns of the association 'createdBy' have to match to ALL identifier columns of the target entity 'App\Acme\Entity\User\BaseUser', however '' are missing.
* The referenced column name 'id' has to be a primary key column on the target entity class 'App\Acme\Entity\User\BaseUser'.
* The join columns of the association 'modifiedBy' have to match to ALL identifier columns of the target entity 'App\Acme\Entity\User\BaseUser', however '' are missing.
I am using Symfony 4, and I have problem with inheritance
src/Acme/Entity/Base/BaseUser.php
<?php
namespace App\Acme\Entity\User;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* #ORM\MappedSuperclass
* #UniqueEntity("email")
* Class Document
*/
class BaseUser implements UserInterface, \Serializable
{
/**
* #var int The id
*
* #ORM\Id
* #ORM\GeneratedValue
* #ORM\Column(type="integer")
*/
protected $id;
/**
* #var BaseUser
* #ORM\ManyToOne(targetEntity="App\Acme\Entity\User\BaseUser")
* #ORM\JoinColumn(name="created_by", referencedColumnName="id")
*/
protected $createdBy;
/**
* #var BaseUser
* #ORM\ManyToOne(targetEntity="\App\Acme\Entity\User\BaseUser")
* #ORM\JoinColumn(name="modified_by", referencedColumnName="id")
*/
protected $modifiedBy;
public function __construct( BaseUser $user)
{
$this->createdBy = $user;
$this->modifiedBy = $user;
}
/**
* #return int
*/
public function getId(): int
{
return $this->id;
}
/**
* #param int $id
*/
public function setId(int $id)
{
$this->id = $id;
}
and there is an entity that tries to extend this BaseUser
src/Api/Entity/Base/User.php
<?php
namespace App\Api\Entity;
use App\Acme\Entity\User\BaseUser;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
*/
class User extends BaseUser { ... }
the problem is somewhere in
/**
* #var BaseUser
* #ORM\ManyToOne(targetEntity="App\Acme\Entity\User\BaseUser")
* #ORM\JoinColumn(name="created_by", referencedColumnName="id")
*/
protected $createdBy;
/**
* #var BaseUser
* #ORM\ManyToOne(targetEntity="\App\Acme\Entity\User\BaseUser")
* #ORM\JoinColumn(name="modified_by", referencedColumnName="id")
*/
protected $modifiedBy;
as I delete them, all works fine.
My mapping file config/packages/doctrine.yaml
doctrine:
dbal:
# With Symfony 3.3, remove the `resolve:` prefix
url: '%env(DATABASE_URL)%'
orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
Api:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Api/Entity'
prefix: 'App\Api\Entity\'
alias: Api
Acme:
is_bundle: false
type: annotation
dir: '%kernel.project_dir%/src/Acme/Entity'
prefix: 'App\Acme\Entity\'
alias: Acme
Can you help please?
change to
/**
* #var int
*
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #ORM\Column(type="integer")
*/
private $id;
check your targetEntity Namespace. where is bundle...portion Symfony Bundle Name Doc
/**
* #var BaseUser
* #ORM\ManyToOne(targetEntity="yourBaseUser")
* #ORM\JoinColumn(name="created_by", referencedColumnName="id")
*/
protected $createdBy;
/**
* #var BaseUser
* #ORM\ManyToOne(targetEntity="yourBaseUser")
* #ORM\JoinColumn(name="modified_by", referencedColumnName="id")
*/
protected $modifiedBy;
I'm trying to add a custom property (psptCode) to my User entity, extending UserBundle.
This is my User class:
<?php
namespace AppBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use JMS\Serializer\Annotation as JMSSerializer;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\ArrayCollection;
use AppBundle\Model\UserInterface;
/**
* #ORM\Entity
* #ORM\Table(name="fos_user")
*
* #UniqueEntity("email")
* #UniqueEntity("username")
* #UniqueEntity("psptCode")
* #JMSSerializer\ExclusionPolicy("all")
* #JMSSerializer\AccessorOrder("custom", custom = {"id", "emailCanonical", "account"})
*/
class User extends BaseUser implements UserInterface
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
* #JMSSerializer\Expose
* #JMSSerializer\Type("string")
*/
protected $id;
/**
* #var string The canonical representation of the email of the user.
*
* #JMSSerializer\Expose
* #JMSSerializer\Type("string")
* #JMSSerializer\SerializedName("email")
* #JMSSerializer\Groups({"user_all", "user_summary"})
*/
protected $emailCanonical;
/**
* #var string
*
* #ORM\Column(type="string", name="pspt_code", length=16, nullable=true)
*/
private $psptCode;
/**
* #var Account The Pspt account related to this user, if any
*
* #ORM\OneToOne(targetEntity="Account")
* #ORM\JoinColumn(name="pspt_code", referencedColumnName="cli_id", nullable=true)
* #JMSSerializer\Expose
* #JMSSerializer\Type("AppBundle\Entity\Account")
* #JMSSerializer\Groups({"user_all"})
*/
protected $account;
public function __construct()
{
parent::__construct();
}
/**
* #param $pspt_code
*
* Set the cli_id to retrieve the account, if any
* #return User
*/
public function setPsptCode($pspt_code)
{
$this->psptCode = $pspt_code;
return $this;
}
/**
* Get psptCode
* #return string
*/
public function getPsptCode()
{
return $this->psptCode;
}
}
When I create the database schema, my custom property gets created, but when I try to store a value into my custom property, it isn't persisted into the database:
$user = $this->userManager->createUser();
$user->setEnabled(true);
$user->setUsername('peter');
$user->setEmail('peter#test.com');
$user->setPlainPassword('testpass');
$user->setPsptCode('XXXXXXXXXX');
$this->userManager->updateUser($user);
The user gets stored like this:
AppBundle\Entity\User {#4041
#id: 1
#emailCanonical: "peter#test.com"
-psptCode: null
#account: null
#username: "peter"
#usernameCanonical: "peter"
#email: "peter#test.com"
#enabled: true
#salt: null
#password: "$2y$15$OJ4ynZmpBaDYbK7N.d35m.FdcSHGpZG7Yemcwxrg4kHhKzIyP3XOO"
#plainPassword: null
#lastLogin: null
#confirmationToken: null
#passwordRequestedAt: null
#groups: null
#roles: []
}
So, I'm unable to store my user's custom property.
Please help!
I'm trying to create the database tables using Symfony command's using the following line :
php app/console doctrine:schema:create
I obtain the following error :
[Doctrine\ORM\Mapping\MappingException]
No identifier/primary key specified for Entity "Cupon\CiudadBundle\Entity\Ciudad". Every Entity must have an identifier/primary key.
And it's the above class :
namespace Cupon\TiendaBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/** #ORM\Entity */
class Tienda
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue
*/
protected $id;
/** #ORM\ManyToOne(targetEntity="Cupon\CiudadBundle\Entity\Ciudad") */
protected $ciudad;
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set ciudad
*
* #param \Cupon\CiudadBundle\Entity\Ciudad $ciudad
* #return Tienda
*/
public function setCiudad(\Cupon\CiudadBundle\Entity\Ciudad $ciudad = null)
{
$this->ciudad = $ciudad;
return $this;
}
/**
* Get ciudad
*
* #return \Cupon\CiudadBundle\Entity\Ciudad
*/
public function getCiudad()
{
return $this->ciudad;
}
public function __toString()
{
return $this->getNombre();
}
}
I don't understand whats really happen, because I put the ID for the class. Any help is appreciated.
Just check if the Ciudad Entity has an id
You should include the following annotations for example:
namespace Cupon\TiendaBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/** #ORM\Entity */
class Ciudad
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue
*/
protected $id;
//..
I'm making entities with Symfony2 and Doctrine2. I made some entities that represent a many-to-many relation between two of my entities.
An example of one of these entities :
/**
* #ORM\Entity
*/
class Contact_Conference_Invitation
{
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="Aurae\UserBundle\Entity\Contact")
*/
private $contact;
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="Aurae\ConferenceBundle\Entity\Conference")
*/
private $conference;
/**
* #var datetime dateInvitation
*
* #ORM\Column(name="dateInvitation", type="datetime")
*/
private $dateInvitation;
//Getters and setters
}
I have tried updating my sql schema, but the tables corresponding to these entities do not appear. Is there somewhere I have to declare them (config or such)? If not, what's wrong?
Thanks a lot
Edit : I had forgotten the namespace for these class, and that's why they were omitted by Doctrine. Another case closed :) thanks for the answers!
Assumptions ...
No, you don't need to declare them anywhere else than in your Entity directory.
What's the error message you got?
I guess you added
use Doctrine\ORM\Mapping as ORM;
on the top of your classes to let them be mapped.
I tried ...
I tried to generate your entities by adding a simple Contact & Conference entities and it's working fine.
Here are the code snippets:
Contact_Conference_Invitation.php
namespace Ahsio\StackBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
*/
class Contact_Conference_Invitation
{
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="Ahsio\StackBundle\Entity\Contact")
*/
private $contact;
/**
* #ORM\Id
* #ORM\ManyToOne(targetEntity="Ahsio\StackBundle\Entity\Conference")
*/
private $conference;
/**
* #var datetime dateInvitation
*
* #ORM\Column(name="dateInvitation", type="datetime")
*/
private $dateInvitation;
//Getters and setters
}
Contact.php
namespace Ahsio\StackBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
*/
class Contact
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #param $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
Conference.php
namespace Ahsio\StackBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
*/
class Conference
{
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #param $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* Get id
*
* #return integer
*/
public function getId()
{
return $this->id;
}
}
Here are the generated tables:
NB: I used a specific namespace for the entities generation to work fine, you need to change them.
Also don't forget to check that you have automapping enabled.
In your config.yml you should have
doctrine:
orm:
auto_mapping: true
Came across this question because my entities weren't generated as well, this was my issue, it could save some time to people struggling with the same issue.
I don't see the definition of your ManyToMany relation in the sample of code you provided.
As an example, here's a ManyToMany relationship I implemented for a project
Entity Project.php
/**
* #var Provider[]
*
* #ORM\ManyToMany(targetEntity="Provider", mappedBy="projects")
*/
protected $providers = null;
Entity Provider.php
/**
* #var Project[]
*
* #ORM\ManyToMany(targetEntity="Project", inversedBy="providers")
* #ORM\JoinTable(name="PROVIDER_PROJECT")
*/
protected $projects = null;
As you can see, here you define the join table for your ManyToMany relationship.
Of course those entities are specific for my particular project but you get the idea and you can adapt it easily for your needs.