Doctrine Mapping 2 levels of inheritance - php

I defined a mapping for
/**
* Identite entity
* #ORM\Entity
* #ORM\Table(name="identite")
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="discr", type="string")
* #ORM\DiscriminatorMap({"identite" = "Identite","candidat" = "Candidat","consultant"="Consultant","staff"="Staff" })
*/
class Identite extends ObjetEtat{
/**
* #var integer
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* #var string
*
* #ORM\Column(name="nomidentite", type="string", length=200, nullable=true)
*/
private $nomidentite;}
and I have the mapping of the super Class:
/**
* Identite entity
* #ORM\Entity
* #ORM\Table(name="objetetat")
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="discrimin", type="string")
* #ORM\DiscriminatorMap({"objetetat" = "ObjetEtat","identite" = "Sigmatis\SigBundle\Entity\Identites\Identite" })
*/
class ObjetEtat {
//put your code here
/**
* #var integer
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id; }
And this is the third level of my hierarchy :
/**
* Candidat entity
* #ORM\Entity
* #ORM\Table(name="candidat")
*/
class Candidat extends Identite {
}
After updating my schema : I don't find the field "discr " of my table Identite . Do I miss something ??

You should put the #ORM\DiscriminatorColumn and #ORM\DiscriminatorMap annotations on the superclass, not the child one

Related

Annotation for simple inheritance on Doctrine2

can u help me with simple inheritance on Doctrine2?
I have basic class for all users:
/**
* Class User
*
* #ORM\Entity
* #ORM\Table(name="users")
* #InheritanceType("SINGLE_TABLE")
*/
class User
{
/**
* #ORM\Id()
* #ORM\Column(name="id", type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
* #var int
*/
private $_id;
/**
* #ORM\Column(name="email", type="string")
* #var int
*/
private $_email;
/**
* #ORM\Column(name="name", type="string")
* #var int
*/
private $_name;
/**
* #ORM\Column(name="password", type="string")
* #var int
*/
private $_password;
...
}
And I have child class:
/**
* Class Parent
*
* #ORM\Entity
* #ORM\Table(name="parents")
*/
class Parent extends User
{
/**
* #ORM\Id()
* #ORM\Column(name="id", type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
* #var int
*/
private $_id;
/**
* #ORM\Column(name="user_id", type="integer")
* #var int
*/
private $_user_id;
/**
* #ORM\Column(name="address", type="string")
* #var int
*/
private $_address;
...
}
How insert Parent.user_id after save User?
And are all the annotations correct?
I use laravel-doctrine and catch next exception:
File mapping drivers must have a valid directory path, however the given path [/var/www/app/Entities] seems to be incorrect!

Doctrine ORM does not persist relations

I have a problem persisting relations with doctrine.
This is the process I'm doing:
Create Material object & fill with data
Create PossibleMaterialConfiguration Object & fill with data
Assign PossibleMaterialConfiguration Object to Material Object with
$material->addPossibleMaterialConfiguration($possibleMaterialConfiguration);
Result:
PossibleMaterialConfiguration.material_id is empty!
But when I do it the other way round and assign Material Object to PossibleMaterialConfiguration it does work!
I'm driving crazy! What the hell is going on here? Thanks in advance.
My two entities:
Material.php
/**
* Material
*
* #ORM\Table()
* #ORM\Entity
*/
class Material
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="PossibleMaterialConfiguration", mappedBy="material", cascade={"persist"})
*/
private $possibleMaterialConfigurations;
PossibleMaterialConfiguration.php
/**
* PossibleMaterialConfiguration
*
* #ORM\Table()
* #ORM\Entity
*/
class PossibleMaterialConfiguration
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="Material", inversedBy="possibleMaterialConfigurations")
* #ORM\JoinColumn(name="material_id", referencedColumnName="id")
*/
private $material;
/**
* #var float
*
* #ORM\Column(name="thickness", type="decimal")
*/
private $thickness;
/**
* #var float
*
* #ORM\Column(name="lengthMin", type="decimal")
*/
private $lengthMin;
/**
* #var float
*
* #ORM\Column(name="lengthMax", type="decimal")
*/
private $lengthMax;
/**
* #var float
*
* #ORM\Column(name="widthMin", type="decimal")
*/
private $widthMin;
/**
* #var float
*
* #ORM\Column(name="widthMax", type="decimal")
*/
private $widthMax;
Take a look at the article about owing side in doctrine :
http://docs.doctrine-project.org/en/latest/reference/unitofwork-associations.html
Basically, doctrine won't check the side where is the "mappedBy" annotation.
If you want to do :
$material->addPossibleMaterialConfiguration($possibleMaterialConfiguration);
Then your addPossibleMaterialConfiguration function should be :
function addPossibleMaterialConfiguration($assoc) {
$this->possibleMaterialConfigurations[] = $assoc;
$assoc->setMaterial($this);
}

doctrine oneToOne mapping of one entity with many other entities do not relate those in database schema

I have region entity and regionEmploymentDetails, regionEducationDetails, regionPowerDetails entities which hold regions employment, education and power details. Entities are kept separate because they hold different data. Below are classes,
class Region
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
*#ORM\OneToOne(targetEntity="RegionEducationDetails")
#ORM\JoinColumn(name="region_id", referencedColumnName="id")
**/
private $education_details;
/**
*#ORM\OneToOne(targetEntity="RegionEmploymentDetails")
#ORM\JoinColumn(name="region_id", referencedColumnName="id")
**/
private $employment_details;
/**
*#ORM\OneToOne(targetEntity="RegionPowerDetails")
#ORM\JoinColumn(name="region_id", referencedColumnName="id")
**/
private $power_details;
}
class RegionEducationDetails
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var integer
*
* #ORM\Column(name="region_id", type="integer", length=20)
*/
private $region_id;
}
class RegionEmploymentDetails
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var integer
*
* #ORM\Column(name="region_id", type="integer")
*/
private $region_id;
}
class RegionPowerDetails
{
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var integer
*
* #ORM\Column(name="region_id", type="integer")
*/
private $region_id;
}
The problem is when I generate schema using command
php app/console doctrine:schema:update --force
It generate database tables but relates Region Entity with Only RegionPowserDetails Entity and does not relate other entities.
It seems it considers only the last mapping and so relates only with the last entity. If I remove Power entity relation it relates with Entity which is before power i.e Employment.
Am I doing anything wrong ? Is there any solution to this problem ?
The region class should be,
class Region {
/**
* #var integer
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
private $education_details_id;
/**
*#ORM\OneToOne(targetEntity="RegionEducationDetails")
#ORM\JoinColumn(name="education_details_id", referencedColumnName="id")
**/
private $education_details;
private $employment_details_id;
/**
*#ORM\OneToOne(targetEntity="RegionEmploymentDetails")
#ORM\JoinColumn(name="employment_details_id", referencedColumnName="id")
**/
private $employment_details;
private $power_details_id;
/**
*#ORM\OneToOne(targetEntity="RegionPowerDetails")
#ORM\JoinColumn(name="power_details_id", referencedColumnName="id")
**/
private $power_details;
}

Doctrine joined inheritance, repository findall returns both parent and child objects

I have a problem with doctrine's InheritanceType JOINED, I have a parent entity 'template' and child entity 'implementation',
/**
* Netvlies\Bundle\MisBundle\Entity\ItemTemplate
*
* #ORM\Entity
* #ORM\InheritanceType("JOINED")
* #ORM\DiscriminatorColumn(name="discriminator", type="string")
* #ORM\DiscriminatorMap({"template" = "ItemTemplate", "implementation" = "Item"})
*/
class ItemTemplate
{
/**
* #var integer $id
*
* #ORM\Column(name="id", type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string $name
*
* #ORM\Column(name="name", type="string", length=255)
*/
private $name;
...
/**
* Netvlies\Bundle\MisBundle\Entity\Item
*
* #ORM\Entity
* #ORM\HasLifecycleCallbacks
*/
class Item extends ItemTemplate
{
/**
* #var \DateTime $created
*
* #ORM\Column(name="created", type="datetime")
*/
private $created;
/**
* #var \DateTime $updated
*
* #ORM\Column(name="updated", type="datetime")
*/
private $updated;
...
I'm able to create and save both entities without problems. I can also retrieve Item entities using it's repositories findall() function just fine. But when i do the same with the parent entity I receive a resultset containing both parent and child classes.

Symfony 2.0 Model relationships

I am trying to model the following table structure in Symfony 2.0 using annotations.
State
PK Code
Name
County
PK State_Code -> FK State.Code
PK Code
Name
Muni
PK State_Code -> FK.State.Code
PK County_Code -> FK County.Code
PK Code
Name
Modeling the fields and the state - county relationship is simple enough, but I cannot determine how to define the relationship for the Muni table.
States have one or more counties.
Counties have one or more Munis.
Munis belong to one or more Counties.
The table structure is legacy and cannot be modified.
Here you go. Tested with Symfony 2.0.5 (Doctrine 2.1):
State.php
namespace Acme\WhateverBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* State
*
* #ORM\Entity
*/
class State
{
/**
* #ORM\Id()
* #ORM\GeneratedValue(strategy="NONE")
* #ORM\Column(name="Code", type="integer")
*/
private $code;
/**
* #ORM\Column(name="Name", type="string")
*/
private $name;
/**
* #ORM\OneToMany(targetEntity="County", mappedBy="state_code")
*/
private $counties;
/**
* #ORM\OneToMany(targetEntity="Muni", mappedBy="state_code")
*/
private $munis;
}
County.php
namespace Acme\WhateverBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* County
*
* #ORM\Entity()
*/
class County
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
* #ORM\Column(name="Code", type="integer")
*/
private $code;
/**
* #ORM\Column(name="Name", type="string")
*/
private $name;
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
* #ORM\ManyToOne(targetEntity="State", inversedBy="counties")
* #ORM\JoinColumn(name="State_Code", referencedColumnName="Code")
*/
private $state_code;
/**
* #ORM\OneToMany(targetEntity="Muni", mappedBy="county_code")
*/
private $munis;
}
Muni.php
namespace Acme\WhateverBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Muni
*
* #ORM\Entity
*/
class Muni
{
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
* #ORM\Column(name="Code", type="integer")
*/
private $code;
/**
* #ORM\Column(name="Name", type="string")
*/
private $name;
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
* #ORM\ManyToOne(targetEntity="County", inversedBy="munis")
* #ORM\JoinColumn(name="County_Code", referencedColumnName="Code")
*/
private $county_code;
/**
* #ORM\Id
* #ORM\GeneratedValue(strategy="NONE")
* #ORM\ManyToOne(targetEntity="State", inversedBy="munis")
* #ORM\JoinColumn(name="State_Code", referencedColumnName="Code")
*/
private $state_code;
}
Don't forget to generate getters/setters. All relationships are bi-directional.

Categories