i am using symfony 2 and doctrine to create enitity class with a foreign key
here is my entity class , now the issue is when i check the table fos_user in mysql database , i don't see travel_access_token and travel_id as a foreign key and even if i change the value of travel_id in fos_user table database is not raising any issues.
i have one to one unidirectional relation as only one user can have one token only
<?php
namespace Travel\HomeBundle\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(name="facebook_id", type="string", length=255, nullable=true) */
protected $facebook_id;
/** #ORM\Column(name="facebook_access_token", type="string", length=255, nullable=true) */
protected $facebook_access_token;
/**
#ORM\Column(name="travel_id", type="string", length=255, nullable=true)
#ORM\OneToOne(targetEntity="Travel\HomeBundle\Entity\Client")
#ORM\JoinColumn(name="travel_id", referencedColumnName="id")
*/
protected $travel_id;
/**
#ORM\Column(name="travel_access_token", type="string", length=255, nullable=true)
#ORM\OneToOne(targetEntity="Travel\HomeBundle\Entity\Client")
#ORM\JoinColumn(name="travel_access_token", referencedColumnName="secret")
*/
protected $travel_access_token;
public function __construct()
{
parent::__construct();
// your own logic
}
}
?>
As "targetEntity" try use just the names of entities. Like this:
#ORM\OneToOne(targetEntity="Client")
Related
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!
I am working on a project where there I am forced to use cascade={"remove","persist"} because of the problem described here.
Reading through the documentation, to quote:
Even though automatic cascading is convenient it should be used with care. Do not blindly apply cascade=all to all associations as it will unnecessarily degrade the performance of your application. For each cascade operation that gets activated Doctrine also applies that operation to the association, be it single or collection valued.
And I see that the same can be fixed if I use
$em->persist($entity);
In my persistence services, which I am already calling. However, doctrine doesn't work as expected. Here are my entities.
Entity/Employee.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping AS ORM;
/**
* #ORM\Entity
* #ORM\Table(uniqueConstraints={#ORM\UniqueConstraint(
* name="UNIQ_EMPLOYEE_ID_NAME_ADDRESS_STATE_CITY_COUNTRY",
* columns={"id","name","city","state","country"}
* )})
*/
class Employee
{
/**
* #ORM\Id
* #ORM\Column(type="integer", options={"unsigned":true})
* #ORM\GeneratedValue(strategy="NONE")
*/
private $id;
/**
* #ORM\Column(type="string", length=255, nullable=false)
*/
private $name;
/**
* #ORM\Column(type="text", nullable=false)
*/
private $address;
/**
* #ORM\Column(type="string", length=255, nullable=false)
*/
private $city;
/**
* #ORM\Column(type="string", length=255, nullable=false)
*/
private $state;
/**
* #ORM\Column(type="string", length=255, nullable=false)
*/
private $country;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Department", inversedBy="employee", cascade={"remove","persist"})
* #ORM\JoinColumn(name="department_id", referencedColumnName="id", nullable=false)
*/
private $department;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Transfer", mappedBy="employee", cascade={"remove","persist"})
*/
private $transfer;
}
?>
Entity/Department.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping AS ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Selectable;
/**
* #ORM\Entity
* #ORM\Table(uniqueConstraints={#ORM\UniqueConstraint(
* name="UNIQ_DEPARTMENT_ID_NAME",
* columns={"id","name"}
* )})
*/
class Department
{
/**
* #ORM\Id
* #ORM\Column(type="integer", options={"unsigned":true})
* #ORM\GeneratedValue(strategy="NONE")
*/
private $id;
/**
* #ORM\Column(type="string", length=255, nullable=false)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Employee", mappedBy="department", cascade={"remove","persist"})
*/
private $employee;
/**
* #ORM\OneToMany(targetEntity="AppBundle\Entity\Transfer", mappedBy="department", cascade={"remove","persist"})
*/
private $transfer;
}
?>
Entity/Transfer.php
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping AS ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Selectable;
/**
* #ORM\Entity
* #ORM\Table(uniqueConstraints={#ORM\UniqueConstraint(
* name="UNIQ_TRANSFER_ID_DEPARTMENT_EMPLOYEE_START_END",
* columns={"id","name"}
* )})
*/
class Transfer
{
/**
* #ORM\Id
* #ORM\Column(type="integer", options={"unsigned":true})
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="date", length=255, nullable=false)
*/
private $start;
/**
* #ORM\Column(type="date", length=255, nullable=false)
*/
private $end;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Employee", inversedBy="attendance", cascade={"persist","remove"})
* #ORM\JoinColumn(name="employee_id", referencedColumnName="id", nullable=false)
*/
private $employee;
/**
* #ORM\ManyToOne(targetEntity="AppBundle\Entity\Department", inversedBy="attendance", cascade={"persist","remove"})
* #ORM\JoinColumn(name="department_id", referencedColumnName="id", nullable=false)
*/
private $department;
}
?>
UPDATE 1:
Now, I have another problem. Since my GeneratorValue strategy for Employee and Department are NONE, I have problem with duplicate record error. I am trying to use PreFlushEventArgs to remove the entity before persisting if the record exists in database. But I wonder if it should be that complex?
Thanks for your help in advance.
It seems you cascade persist operation from Employee to Department and Transfer. But they do cascade the persist operation to the Employee Entity too.
It means when you do
$em->persist($an_employee);
You are stuck in a persist loop.
In my opinion, the cascade persist should be only in one way, i.e. only on the Employee entity.
Also if you choose to do this, add your Department to the Employee, and not the opposite :
$an_employee->addDepartment($a_department);
$an_employee->addTransfer($a_transfer);
That way, when you persist an employee, its Departments and Transfer should be persisted too
Very very weird. I have used this method from doctrine hundreds of times. I have a simple controller that takes an id as parameter. The query that Doctrine generates is wrong and crash.
/**
* #Security("has_role('ROLE_ADMIN')")
* #return Response
*/
public function editSellerAction($id)
{
$em = $this->getDoctrine()->getManager();
$seller = $em->getRepository('SiteUserBundle:Seller')->find($id);
// ...
$form = $this->createForm(new SellerType(), $seller, array(
'method' => 'POST'
));
// ...
}
The query generated is the following
[2/2] DBALException: An exception occurred while executing 'SELECT t1.id AS id2, t1.username AS username3, t1.password AS password4, t1.firstname AS firstname5, t1.lastname AS lastname6 FROM seller t1 WHERE t0.id = ? LIMIT 1' with params ["2"]:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 't0.id' in 'where clause' +
The error thrown makes sense because it's looking at "WHERE t0.id" when it should be looking at "WHERE t1.id". I tried the query with t1 using phpmyadmin and it works.
Any idea what might cause this issue?
/**
* Seller have access to their customer and are able to RW access to the customers
*
* #ORM\Table("seller")
* #ORM\Entity
* #author Michael Villeneuve
*/
class Seller extends User
{
/**
* #var array
*
* #ORM\OneToMany(targetEntity="Customer", mappedBy="seller", cascade={"persist", "remove"})
* #ORM\JoinColumn(name="seller_id", referencedColumnName="id")
**/
protected $customers;
/**
* #var string
*
* #ORM\Column(name="firstname", type="string", length=255, nullable=false)
*/
protected $firstname;
/**
* #var string
*
* #ORM\Column(name="lastname", type="string", length=255, nullable=false)
*/
protected $lastname;
// Other attributes and only getters/setter
/**
*
* #ORM\Entity
*/
class User implements UserInterface
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(type="string", length=255, unique=true)
*/
private $username;
/**
* #ORM\Column(type="string", length=64)
*/
private $password;
I have 3 entities that extends the User (customer, admin and seller).
Updated link: https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/inheritance-mapping.html
Read up a bit on mapped super classes: http://docs.doctrine-project.org/en/latest/reference/inheritance-mapping.html. Basically, your abstract base user class cannot itself be an entity.
So take the #ORM\Entity line out of your User class. That is where the table 0 (t0) is coming from.
You have 2 options:
The first one is to create an abstract User entity and inherit all values from it. This is useful if you have many entities with the same behaviour. I e.g. like to create a BaseEntity with a ID field and some basic methods. All entities can extend this one and automatically have an ID. Cerad explained in his answer how this is done.
The second option are so called discriminator fields. Basically they allow you to have one User table and sub-tables for every extended entity. You can read about them in the official docs.
Which one you end up using is probably case dependent.
Try to add id field to the Seller entity instead of User
/**
* Seller have access to their customer and are able to RW access to the customers
*
* #ORM\Table("seller")
* #ORM\Entity
*/
class Seller extends User
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var array
*
* #ORM\OneToMany(targetEntity="Customer", mappedBy="seller", cascade={"persist", "remove"})
* #ORM\JoinColumn(name="seller_id", referencedColumnName="id")
**/
protected $customers;
/**
* #var string
*
* #ORM\Column(name="firstname", type="string", length=255, nullable=false)
*/
protected $firstname;
/**
* #var string
*
* #ORM\Column(name="lastname", type="string", length=255, nullable=false)
*/
protected $lastname;
// Other attributes and only getters/setter
/**
*
* #ORM\Entity
* #author Michael Villeneuve<michael#panierdachat.com>
*/
class User implements UserInterface
{
/**
* #ORM\Column(type="string", length=255, unique=true)
*/
private $username;
/**
* #ORM\Column(type="string", length=64)
*/
private $password;
i am tying to create one to one relation in symfony 2 using doctrine
here is my entity class
<?php
namespace Travel\HomeBundle\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(name="facebook_id", type="string", length=255, nullable=true) */
protected $facebook_id;
/** #ORM\Column(name="facebook_access_token", type="string", length=255, nullable=true) */
protected $facebook_access_token;
/**
#ORM\Column(name="travel_id", type="string", length=255, nullable=true)
#ORM\OneToOne(targetEntity="Travel\HomeBundle\Entity\Client")
#ORM\JoinColumn(name="travel_id", referencedColumnName="id")
*/
protected $travel_id;
/**
#ORM\Column(name="travel_access_token", type="string", length=255, nullable=true)
#ORM\OneToOne(targetEntity="Travel\HomeBundle\Entity\Client")
#ORM\JoinColumn(name="travel_access_token", referencedColumnName="secret")
*/
protected $travel_access_token;
public function __construct()
{
parent::__construct();
// your own logic
}
}
?>
after updating schema when i run following command to generate getter and setter methods
php app/console doctrine:generate:entities Travel/HomeBundle/Entity/User.php
i am getting following Runtime Error
Namespace "Travel\HomeBundle\Entity\User.php" does not contain any mapped
entities.
doctrine:generate:entities does not take file extensions. Just remove the .php.
The following are example of acceptable parameters for doctrine:generate:entities:
php app/console doctrine:generate:entities Travel/HomeBundle/Entity/User
Or
php app/console doctrine:generate:entities TravelHomeBundle:User
Here is the problem:
Class Routing with attributes objectId and objectType. objectId is an int, and objectType is a string. The reason for this was to allow the same table to hold data for different kind of routings. For instance for the routing of Products, Department and Brand. So, the combination of the objectType and the objectId is my JoinColumn.
How do I create such a bidirectional relationship with Doctrine2? I looked at inherited relationships, but none of the concepts seemed to be what I'm looking for.
I could create some views in the database and just have a couple of different Routing entities, but this does not seem the best route.
Here are my entities Department, Product and Brand.
../Entity/Department.php
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity()
* #ORM\Table(name="departments")
*/
class Department implements DescribableInterface
{
/**
* #ORM\Id
* #ORM\Column(name="id", type="integer", length=11)
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="status", type="string", length=1)
*/
private $status;
/**
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
...
../Entity/Product.php
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="products")
*/
class Product implements DescribableInterface
{
/**
* #ORM\Id
* #ORM\Column(name="id", type="integer", length=11)
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="status", type="string", length=1)
*/
private $status;
/**
* #ORM\Column(name="product_code", type="string", length=100, nullable=true)
*/
private $productCode = '';
/**
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
...
../Entity/Brand.php
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="brands")
*/
class Brand
{
/**
* #ORM\Id
* #ORM\Column(name="id", type="integer", length=11)
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="status", type="string", length=1)
*/
private $status = 'a';
/**
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
...
Each product, brand and department has its own URL that is held in the routing table set by the object_type and object_id where the object_type is simply department, product or brand and the object_id is the unique id of the corresponding product, brand or department.
../Entity/Routing.php
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity
* #ORM\Table(name="routing")
*/
class Routing
{
/**
* #ORM\Id
* #ORM\Column(name="id", type="integer", length=11)
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\Column(name="object_id", type="integer", length=11)
*/
private $objectId;
/**
* #ORM\Column(name="object_type", type="string", length=100)
*/
private $objectType;
/**
* #ORM\Column(name="url", type="text")
*/
private $url;
...
What I am really struggling with is how do I setup the relationship, so the departments, products and brands can access their URL from the single routing entity.
I have tried adding the relationships to the $objectId, but it doesn't seem to like that. Is it possible to set this up like this?
What I am basically trying to achieve is to get the data object and have the ability to get the URL of an object, for example:
$departments = $em->getRepository("AdamStaceySiteBundle:Department")->findAll();
foreach ($departments as $department)
{
echo ''.$department->getMenuTitle().';
}
Can anyone help?
After further researching I found a man (Dirk Olbertz) in the know who had the same problem.
Information can be found at: Google Groups: Multiple JoinColumns?
I have now implemented this and I will explain how I did it incase it might help anyone else.
The answer to my problem was the use of single table inheritance.
The first thing I needed to do was update the routing entity to use single table inheritance:
../Entity/Routing.php
/**
* #ORM\Entity
* #ORM\InheritanceType("SINGLE_TABLE")
* #ORM\DiscriminatorColumn(name="object_type", type="string")
* #ORM\DiscriminatorMap({"product" = "ProductRouting", "department" = "DepartmentRouting", "brand" = "BrandRouting"})
* #ORM\Table(name="routing")
*/
class Routing
{
/**
* #ORM\Id
* #ORM\Column(name="id", type="integer", length=11)
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
...
The DiscriminatorColumn allowed me to specify what column would be used to link, which in this case was the object_type field.
The DiscriminatorMap allowed me to specify what object_type will link with what entities.
These entities then had to be created that extended the Routing entity.
../Entity/ProductRouting.php
/**
* #ORM\Entity
*/
class ProductRouting extends Routing
{
/**
* #ORM\ManyToOne(targetEntity="Product")
* #ORM\JoinColumn(name="object_id", referencedColumnName="id")
*/
protected $product;
...
../Entity/DepartmentRouting.php
/**
* #ORM\Entity
*/
class DepartmentRouting extends Routing
{
/**
* #ORM\ManyToOne(targetEntity="Department")
* #ORM\JoinColumn(name="object_id", referencedColumnName="id")
*/
protected $department;
...
../Entity/BrandRouting.php
/**
* #ORM\Entity
*/
class BrandRouting extends Routing
{
/**
* #ORM\ManyToOne(targetEntity="Brand")
* #ORM\JoinColumn(name="object_id", referencedColumnName="id")
*/
protected $brand;
...
Then in each of the Product, Department and Brand entities I needed to add the new $routings.
../Entity/Product.php
...
class Product
{
...
/**
* #ORM\OneToMany(targetEntity="ProductRouting", mappedBy="product", cascade={"all"})
*/
private $routings;
...
../Entity/Department.php
...
class Department
{
...
/**
* #ORM\OneToMany(targetEntity="DepartmentRouting", mappedBy="department", cascade={"all"})
*/
private $routings;
...
../Entity/Brand.php
...
class Brand
{
...
/**
* #ORM\OneToMany(targetEntity="BrandRouting", mappedBy="brand", cascade={"all"})
*/
private $routings;
...
Hope that helps...