set FOSUSERBUNDLE with existing table user (in symfony2) - php

I am supposed to built a project with symfony2 for a back end, and angular2 for front end.
For handling user action(authentification,remember me ...) I'am trying to used FOSUSERBUNDLE.
Let me specify that I'am using and existing database which contains a table with many attributes like (id, category, title, name, email, number, adress, ...). Some of attributes are not the same User in FOSUSERBUNDLE.
My question is how can I set FOSUSERBUNDLE to make it work with my existing table without brook it?
When I extend my class customer to BaserUser of FOSUserbundle it broke and changed my table structure
below
<?php
// src/AppBundle/Entity/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 customer extends BaseUser
{
/**
* #var float
*
* #ORM\Column(name="scoring", type="float", precision=10, scale=0, nullable=false)
*/
private $scoring = '0';
/**
* #var integer
*
* #ORM\Column(name="id_type", type="integer", nullable=false)
*/
private $idType = '4';
/**
* #var integer
*
* #ORM\Column(name="id_categorie", type="integer", nullable=false)
*/
private $idCategorie = '0';
/**
* #var string
*
* #ORM\Column(name="autorisation_prelevement", type="string", nullable=false)
*/
private $autorisationPrelevement = 'N';
/**
* #ORM\Id
* #ORM\Column(type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string")
*/
protected $addresse;
public function __construct()
{
parent::__construct();
}
}

Related

symfony2 combine primary key and relation

I'm trying to get a legacy database into the doctrine mappings.
All the tables have a combined primary key. One ID and one "optios id".
The problem is that Optios ID always has to be set but the OneToOne relation with the same columns causes the column "Optios ID" to be set to null. I'm not sure what I'm doing wrong or is there a way around it?
PS: The 'Pack' relation is optional.
<?php
namespace CalendarBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity(repositoryClass="CalendarBundle\Repository\CategoryRepository")
* #ORM\Table(name="Categories")
*/
class Category
{
/**
* #ORM\Id
* #ORM\Column(type="integer", name="Category_id")
*/
private $id;
/**
* #ORM\Column(type="integer", name="Optios_id")
* #ORM\Id
*/
private $optiosId;
/**
* #ORM\Column(type="string", name="Name")
*/
private $name;
/**
* #ORM\Column(type="boolean", name="AvailableOnline")
*/
private $online;
/**
* #ORM\Column(type="integer", name="SequenceNumber", nullable=true)
*/
private $order;
/**
* #ORM\Column(type="integer", name="Parent_id")
*/
private $parentId;
/**
* One Category has Many Packs.
*
* #var Pack
*
* #ORM\OneToOne(targetEntity="Pack", inversedBy="category")
* #ORM\JoinColumns(
* #ORM\JoinColumn(name="Pack_id", referencedColumnName="Pack_id"),
* #ORM\JoinColumn(name="Optios_id", referencedColumnName="Optios_id"),
* )
*/
private $pack;
/**
* #ORM\Column(type="boolean", name="Deleted")
*/
private $deleted;

Doctrine : Does the placement of a property in a doctrine entity affect merge/persist?

I am using Doctrine 2.5.0 and I am fairly new to it.
I would like to know why the position of the properties and associations affects the saving of the entity.
I have an entity with some properties and some associations. I recently noticed this on at least 3 different entities that if a property is placed after an association, it would not be updated in the database (using merge and flush).
So, in the below entity, the name and other properties do not get updated.
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="products")
*/
class Product
{
/**
* #ORM\Id
* #ORM\Column(type="integer", length=8)
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\OneToOne(targetEntity="Shop")
* #ORM\JoinColumn(name="shopId", referencedColumnName="id")
*/
protected $shop;
/**
* #ORM\Column(type="string", length=255)
*/
protected $name;
/*Some other properties*/
/*Getters and Setters*/
}
But this gets updated. Notice the position of the $name (and all the other properties) and $shop.
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(
* name="products"
* )
*/
class Product
{
/**
* #ORM\Id
* #ORM\Column(type="integer", length=8)
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #ORM\Column(type="string", length=255)
*/
protected $name;
/*ALL other properties*/
/**
* #ORM\OneToOne(targetEntity="Shop")
* #ORM\JoinColumn(name="shopId", referencedColumnName="id")
*/
protected $shop;
/*Getters and Setters*/
}
I thought of changing the positions because in one of my entities I noticed that only the properties placed after the associations were not getting updated in the database.

Symfony3 class inheritance and db relationships

I have these 3 entitites
Users.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Users
*
* #ORM\Table(name="users")
* #ORM\Entity(repositoryClass="AppBundle\Repository\UsersRepository")
*/
class Users
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=255, unique=true)
*/
private $email;
/**
* #var string
*
* #ORM\Column(name="password", type="string", length=20)
*/
private $password;
/**
* #var string
*
* #ORM\Column(name="phone", type="string", length=20)
*/
private $phone;
/**
* #var string
*
* #ORM\Column(name="type", type="string")
*/
private $type;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #var int
*
* #ORM\Column(name="feedback", type="integer")
*/
private $feedback;
/**
* #var string
*
* #ORM\Column(name="picture", type="blob")
*/
private $picture;
/**
* #var int
*
* #ORM\Column(name="rating", type="integer", length=255)
*/
private $rating;
/**
* #var string
*
* #ORM\Column(name="info", type="text")
*/
private $info;
/**
* #var \DateTime
*
* #ORM\Column(name="datecreated", type="datetime")
*/
private $datecreated;
/**
* #ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
}
client.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* client
*
* #ORM\Table(name="client")
* #ORM\Entity(repositoryClass="AppBundle\Repository\clientRepository")
*/
class client extends Users
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var int
*
* #ORM\Column(name="numberofjobsposted", type="integer")
*/
private $numberofjobsposted;
/**
* #var string
*
* #ORM\Column(name="clienttype", type="string", length=255)
*/
private $clienttype;
}
sprovider.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* sprovider
*
* #ORM\Table(name="sprovider")
* #ORM\Entity(repositoryClass="AppBundle\Repository\sproviderRepository")
*/
class sprovider extends Users
{
/**
* #var int
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var array
*
* #ORM\Column(name="interestedin", type="simple_array")
*/
private $interestedin;
/**
* #var int
*
* #ORM\Column(name="numofsuccjobs", type="integer")
*/
private $numofsuccjobs;
/**
* #var string
*
* #ORM\Column(name="sprovidertype", type="string", length=255)
*/
private $sprovidertype;
/**
* #var string
*
* #ORM\Column(name="address", type="string", length=255)
*/
private $address;
/**
* #var string
*
* #ORM\Column(name="postcode", type="string", length=255)
*/
private $postcode;
}
So I achieved that the extends statement provides the Users properties in the client and sprovider tables in MySQL. That's awesome. What I want now is to make the relations so that when I add a new client for example, both the tables Users and client add a new user/client in MySQL, and they have same id too.
the type() property in the Users entity i would like to be optional for the type of user I create. Example : I create a new client and in the Users table in MySQL the type is set to "CLIENT".
I read this and so far I think it has to be ManyToMany relation but It's quite confusing to me.
How to make those relations in the entities and then how to use them in the controller? If possible, please provide an example.
I think you're confused about the reasons to use inheritance.
The idea is that you have base class, in this case User, and that can be extended to provide variations of that class, in this case client (you should capitalise this) and sprovider.
Ideally, you would not have a User table, only the other 2.
In doctrine, this is called a mapped super-class.
A mapped superclass is an abstract or concrete class that provides persistent entity state and mapping information for its subclasses, but which is not itself an entity. Typically, the purpose of such a mapped superclass is to define state and mapping information that is common to multiple entity classes.
see the documentation here
you can link properties using relationships, this is their example.
<?php
/** #MappedSuperclass */
class MappedSuperclassBase
{
/** #Column(type="integer") */
protected $mapped1;
/** #Column(type="string") */
protected $mapped2;
/**
* #OneToOne(targetEntity="MappedSuperclassRelated1")
* #JoinColumn(name="related1_id", referencedColumnName="id")
*/
protected $mappedRelated1;
// ... more fields and methods
}
/** #Entity */
class EntitySubClass extends MappedSuperclassBase
{
/** #Id #Column(type="integer") */
private $id;
/** #Column(type="string") */
private $name;
// ... more fields and methods
}

New Symfony2 User Class gives Fatal Error

I am trying to move from manually specified users in security.yml to database users. I have read http://symfony.com/doc/current/book/security.html#loading-users-from-the-database and implemented their example, then expanded on it using a friend's user class. When I try to run php app/console doctrine:generate:entities I get the following error: Class MyFreelancer\PortalBundle\Entity\User contains 7 abstract methods and must therefor be declared abstract or implement the remaining methods and then a list of methods that goes off the page.
My code is as follows:
<?php
namespace MyFreelancer\PortalBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\Criteria;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Symfony\Component\Validator\Constraints as Assert;
use JMS\Serializer\Annotation as Ser;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\Security\Core\User\Role;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* User
*
* #ORM\Entity
* #ORM\Table(name="users")
* #Ser\ExclusionPolicy("all")
*/
class User implements UserInterface, \Serializable
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
* #Ser\Expose
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="role_id", type="integer")
* #Assert\Range(min=1)
*/
private $roleId;
/**
* #var string
*
* #ORM\Column(name="username", type="string", length=100)
* #Assert\Length(min=3, max=100)
* #Ser\Expose()
*/
private $username;
/**
* #var string
*
* #ORM\Column(name="password", type="string", length=2048)
* #Assert\Length(min=8, max=2048)
*/
private $password;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=100)
* #Assert\Length(min=3, max=100)
* #Ser\Expose()
*/
private $name;
/**
* #var string
*
* #ORM\Column(name="surname", type="string", length=100)
* #Assert\Length(min=3, max=100)
* #Ser\Expose()
*/
private $surname;
/**
* #var string
*
* #ORM\Column(name="email", type="string", length=200)
* #Assert\Email(checkMX=true)
* #Ser\Expose()
*/
private $email;
/**
* #var string
*
* #ORM\Column(name="cell", type="string", length=20, nullable=true)
* #Assert\Length(min=10, max=20)
* #Ser\Expose()
*/
private $cell;
}
?>
I have looked at Symfony2 Fatal error in Users Entity Provider, but I do not know what has to go into those methods if I have to implement them manually. Also, isn't doctine:generate:entities supposed to do that for you? If declaring the class as abstract is easier, how do I do that? What does that entail?
While we are on the topic, how do I save/retrieve the save user roles to the database, such as (from security.yml)
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
This is actually a second question and if it cannot be answered here then I will open a new topic. Thank you to everyone who has read this question.
The cookbook article about the entity provider at http://symfony.com/doc/current/cookbook/security/entity_provider.html shows how to set this up correctly.

Doctrine 2.1 - Removing from collection

I am trying to remove comments from a parent entity, I remember doing this on my last website but now it's not working..
My entity - users
namespace Application\Entities;
use Doctrine\ORM\Mapping AS ORM,
Doctrine\Common\Collections\ArrayCollection;
/**
* Loan
*
* #ORM\Table(name="users")
* #ORM\Entity
*/
class Users{
/**
* #var integer $id
*
* #ORM\Column(type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var string $username
*
* #ORM\Column(type="string", length=45, nullable=false)
*/
private $username;
/**
* #var ArrayCollection
*
* #ORM\OneToMany(targetEntity="Comments", mappedBy="author", cascade={"persist", "remove"})
*/
private $comments;
public function getComments(){
return $this->comments;
}
and my comments table:
namespace Application\Entities;
use Doctrine\ORM\Mapping AS ORM,
Doctrine\Common\Collections\ArrayCollection;
/**
* Loan
*
* #ORM\Table(name="comments")
* #ORM\Entity
*/
class Comments{
/**
* #var integer $id
*
* #ORM\Column(type="integer", nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* #var integer $user_id
*
* #ORM\Column(type="integer", length=15, nullable=false)
*/
private $user_id
/**
* #var Loan
*
* #ORM\ManyToOne(targetEntity="Users", inversedBy="comments",cascade={"persist"})
* #ORM\JoinColumns({
* #ORM\JoinColumn(name="user_id", referencedColumnName="id")
* })
*/
private $author;
This is fine, it works and I get all collections called comments in the users repository..
Now, I usually do this when I need to delete:
$commentToDelete = $this->em->getRepository('Entities\Comments')->findOneById(375);
$userResults = $this->em->getRepository('Entities\Users')->findOneById(23);
$userResults->getComments()->removeElement($commentToDelete);
$this->em->flush();
Nothing deletes, neither it throws an exception to tell me it hasn't.
I doctrine flushed it too, checked the db, and it's still there..
UPDATE:
Straight after I removeElement, I looped through the user id = 23 dataset, and the comment data for id375 is not there... so it removed it from the collection but not from the DB, and I thought $em->flush() is supposed to do this?
Please advise
Thanks
You need to use
$em->remove($commentToDelete);
$em->flush();
Because the mapping is held in the comment you need to remove this entity to remove the reference before you flush which will save the state to the db.

Categories