FOS Userbundle - declare own userID field - symfony

I have installed the FOS UserBundle.
The problem is: the "id" field of my User-Table is called userID.
I can't change it bc of other Tables and Programming that is dependent on the userID field.
If i try to login, i get an error:
Unrecognized field: id
The problem seems to lie in the call for the id field:
UserManager ->findUserBy (array('id' => 1))
Can I somehow override FOS UserBundle so that the findby() method transfers id to userID?
Or do i get it totally wrong and have to do it another way?

Field names in the entity don't have to be identical to the field names in your sql table. So you can use "id" as field in your doctrine entity and map it to a "userID" field in your sql table.
If you are using Doctrine annotations for your user entity like in the FOSUserBundle documentation, this could do the trick:
/**
* #ORM\Id
* #ORM\Column(name="userID", type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
I haven't tested it and there is a chance that it doesn't work, because the id field is always a bit "special", but it may be worth a try.

Generate your own UseBundle and add in define the entity something like this.
<?php
namespace Demo\UserBundle\Entity;
use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* #ORM\Entity(repositoryClass="Sciviz\UserBundle\Entity\UserRepository")
* #ORM\Table(name="fos_user")
*/
class User extends BaseUser {
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/** #ORM\Column(name="first_name", type="string", length=255, nullable=true) */
protected $first_name;
..................
Also see this tutorial
http://knpuniversity.com/screencast/fosuserbundle-ftw

your column name "USER_ID" can be case sensitive.
/**
* #ORM\Entity
* #ORM\Table(name="user-table")
*/
class User extends BaseUser
{
/**
* #ORM\Id
* #ORM\Column(type="integer", name="USER_ID")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
}

Related

How to set Doctrine Auto Increment Starting Value #ORM\GeneratedValue[starts with 100] using symfony2

How can I set the starting value for an Auto Incremented like 100,101....
My code is
/**
* #ORM\Name
* #ORM\GeneratedValue(strategy="SEQUENCE")
* #ORM\SequenceGenerator(sequenceName="name", initialValue=100)
* #ORM\Column(type="integer")
*/
protected $name;
while trying i am getting an error like this
[Doctrine\Common\Annotations\AnnotationException]
[Semantical Error] The annotation "#Doctrine\ORM\Mapping\Name" in property
Epita\CrmBundle\Entity\CandidateAnalysisCrm::$name does not exist, or could
not be auto-loaded.
please help me anyone. Thanks for advance
My entity is:
namespace Epita\CrmBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* category
*
* #ORM\Table(name="candidateanalysiscrm")
* #ORM\Entity
*/
class CandidateAnalysisCrm {
/**
* #ORM\Id
* #ORM\Column(type = "integer", name= "id")
* #ORM\GeneratedValue(strategy="AUTO")
*/
public $id;
/**
* #ORM\Column(type="integer", name="applicationinprogress",length=10,
nullable=true)
*/
protected $applicationinprogress;
/**
* #ORM\Name
* #ORM\GeneratedValue(strategy="SEQUENCE")
* #ORM\SequenceGenerator(sequenceName="name", initialValue=100)
* #ORM\Column(name="name", type="integer")
*/
protected $name;
}
You have several issues here:
First, as already mentioned there is no #Name doctrine annotation.
Second, the GeneratedValue annotation only applies if your attribute is annotated with the #Id annotation.
From the documentation
Specifies which strategy is used for identifier generation for an instance variable which is annotated by #Id. This annotation is optional and only has meaning when used in conjunction with #Id.
If this annotation is not specified with #Id the NONE strategy is used as default.
To fix it change your $name attributes annotations to:
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="SEQUENCE")
* #ORM\SequenceGenerator(sequenceName="name", initialValue=100)
* #ORM\Column(name="name", type="integer")
*/
protected $name;
}
If using the $name property as identifier is not an option for you, listening to doctrine events (e.g. prePersist) could be an alternative solution for your problem. But this is a totally different question.
I'm not sure, but i think that ORM\Name doesn't exist.
You have to write :
* #ORM\Column(name="column_name", type="string")

Inheritance issue in symfony2 and doctrine2

I have many ads entities (MotorAds, RealestateAds, ElectronicsAds, ...) that share some attributes like title and description. In order to avoid redefining these attributes for each Ads entity, one can use the mapped superclass methods as follows:
<?php
/** #MappedSuperclass */
class MappedSuperclassAds{
/**
* #var string
*
* #ORM\Column(name="title", type="string", length=255, nullable=false)
*/
private $title;
/**
* #var string
*
* #ORM\Column(name="description", type="text", nullable=false)
*/
private $description;
}
Then, the inheritance will do the job.
Now, what is the problem? The problem is that each Ads entity is related to its entity that defines the list of users that added the ads to their favorites. To do that (the MotorsAds entity for example),
1.linking the MotorsAds entity to its MotorsFavorite entity through that code:
/**
* #ORM\OneToMany(targetEntity="Minn\AdsBundle\Entity\MotorsFavorite",
* mappedBy="motors",cascade={"persist", "remove"})
* #ORM\JoinColumn(nullable=true)
*/
private $favorites;
2.Defining the MotorsFavorite entity as fellows:
<?php
namespace Minn\AdsBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* MotorsFavorite
*
* #ORM\Table(
* uniqueConstraints={#ORM\UniqueConstraint(name="unique_fav_motors",
* columns={"user_id", "motors_id"})})
* #ORM\Entity(repositoryClass="Minn\AdsBundle\Entity\MotorsFavoriteRepository")
* #ORM\HasLifecycleCallbacks()
*/
class MotorsFavorite {
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Minn\UserBundle\Entity\User")
* #ORM\JoinColumn(nullable=false)
*/
private $user;
/**
* #ORM\ManyToOne(targetEntity="Minn\AdsBundle\Entity\MotorsAds", inversedBy="favorites")
* #ORM\JoinColumn(nullable=false, onDelete="CASCADE")
*/
private $motors;
//...
}
As you can see, the linkage between the MotorAds and MotorFavorite is a hard linkage, which means that I have to create a Favorite entity for each Ads entity I create (FavoriteMotors, FavoriteRealestate, FavoriteElectronics, ...). This is a long and repetitive work.
So my question is:
1.Creating a super mapped class called SuperMappedFavorite which will only include the $id and $user attributes will reduce the repetitive work. But what about the the attribute $motors? $motors is hardly linked to the entity MotorsAds as you see here:#ORM\ManyToOne(targetEntity="Minn\AdsBundle\Entity\MotorsAds", inversedBy="favorites"). All the burden of the work is in the setters and getters of $motors.
2.Is it possible to make the target entity an interface like this:
<?php
// SuperMappedFavorite.php
// ...
#ORM\ManyToOne(targetEntity="Minn\AdsBundle\Favorite\FavoriteAwareInterface", inversedBy="favorites")
private $object;
// ...
and the MotorsAds entity will be implementing in this the FavoriteAwareInterface
If anyone has a good link/article regarding this kind of issue, I will be happy to have it.
Thanks.
Yes, you can set an interface as target entity, as described in the Symfony documentation.
The process is basically:
defining the interface (your Minn\AdsBundle\Favorite\FavoriteAwareInterface),
setting the interface in the parent entity (as you already did),
implementing the interface in a different entity (would be class MotorsFavorite implements FavoriteAwareInterface) – and yes, it can also be derived from a mapped superclass,
and then telling Doctrine to use your implementation through the doctrine.orm.resolve_target_entities config parameter.
See the documentation for details and a code example.

Symfony2 OnetoOne relations

I have two class, User and PersonalData. We need relation OnetoOne with Doctrine in Symfony2. In my code I tried this relations, but in MySQL doesnt appear the foreign key.
My code:
namespace TFC\UserBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* User
*/
/** #ORM\Entity */
class User
{
/**
* #Id #Column(type="integer") #GeneratedValue
*/
private $id;
/**
* #var string
*/
private $email;
}
use Doctrine\ORM\Mapping as ORM;
/**
* PersonalData
*/
/** #ORM\Entity */
class PersonalData
{
/** #Id #OneToOne(targetEntity="User") */
private $userId;
/**
* #var string
*/
private $firstName;
}
You have to prefix with #ORM all your annotations like this:
#ORM\Id
#ORM\Column(type="integer")
#ORM\GeneratedValue(strategy="AUTO")
This "Unidirectional association" needs to be placed on User entity (because I think you want to load that informations from the User object...) and you don't need to care about the foreign key because doctrine create it automatically.
So place a $personalData property on User Entity and apply there the association, then delete $userId from PersonalData and add the $id property like did in User.
Even if the documentation claims that is not necessary have you tried something like:
#JoinColumn(name="userId", referencedColumnName="id")
When you generate the entity do you get any message?

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
{

How to Inject Logged User into Property by Annotation

I have an entity called Item as above:
<?php
/**
* Item
* #ORM\Table(name="item")
* #ORM\Entity
*/
class Item {
/**
*
* #ORM\Column(name="id", type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var \Acme\UserBundle\Entity\User
* ???
*/
private $user;
.....
How can I inject logged User object into $user property via annotation in the easiest way?
If you are trying to save the created/updated timestamps and the user who performed these actions with your entity ... this is called blameable and timestampable behavior!
Have a look at Knp\DoctrineBehaviors including blameable and timestampable behaviors. (PHP 5.4+ needed)
Gedmo\DoctrineExtensions also provides blameable and timestampable. (PHP >=5.3.2)

Resources