how to add/remove an entity's mapping programmatically - doctrine-orm

I have a handler which uses the same Entity for two different kind of queries:
/**
* #ORM\Entity
* #ORM\Table(name="app_iso50k1.meter", schema="app_iso50k1")
* #ORM\InheritanceType("SINGLE_TABLE")
* #ORM\DiscriminatorColumn(name="mettype", type="string")
* #ORM\DiscriminatorMap({"phy" = "PhyMeter"})
*/
abstract class Meter
{
const TYPE_PHYSICAL = 'PHY';
const TYPE_VIRTUAL = 'VIR';
const TYPE_LOGICAL = 'LOG';
/**
* #ORM\Column(type="string")
* #ORM\Id
*/
protected $id;
<methods>
}
/**
* #ORM\Entity
* #ORM\Entity(repositoryClass="PhyMeterRepository")
* #HasLifecycleCallbacks
*/
class PhyMeter extends Meter
{
/**
* #ORM\Column(type="integer", nullable=false)
*/
protected $idInstrum;
/**
* #ORM\Column(type="integer", nullable=false)
*/
protected $idDevice;
/**
* #ORM\Column(type="integer")
*/
protected $instrumType;
...<methods>
}
The first handler's method is performed on a legacy DB table and it would need to map all the fields annotated with #ORM\Column (id, idInstrum, idDevice, instrumType). To do that, I use a primitive query and I map the data by means of a ResultSetMapping
$rsm = new ResultSetMapping;
$rsm->addEntityResult('Belka\Iso50k1Bundle\Entity\PhyMeter', 'mt');
$rsm->addFieldResult('mt', 'id', 'id');
...
and it works like a charm. The problem is on the the other handler's methods which need to persist data on app.meter table: what I really would like to persist are a small part of the properties (i.e. just the id, idInstrum but not instrumType so as not to have that column in my new table's schema.
I was thinking about using StaticPHPDriver but I'm not sure if it is the right way: what I really would like is manually adding/removing some ORM mapping according to my needs (i.e. different handler's functions)
Is that possible? I could remove the mappings (#ORM\column annotation) I don't need to persist, but that way I cannot map the extra properties by using ResultSetMapping, unless I can add it programmatically.
Any hint is more than welcome

Related

Doctrine 3 Way Mapping

I need some help getting my head around a 3 way mapping.
I have an entity Student and an entity Parent, obviously one parent can have many students and vice versa, but i need additional information between each parent and student that will be different for each one.
Perhaps we have the following data:
Student A - Parent A (no responsibility) , Parent B (has responsibility) - even though one parent holds legal responsibility and the other doesn't, they are both still parents of the same student.
Student B - Parent A (has responsibility), Parent B (has responsibility) - in this case another student has the same parents but this time they both have legal responsibility.
To start basic entities i would have:
class Student
{
// normally would have a ManyToMany here to link parents, but i need the 3rd entity
// to hold whether this student's parent has legal responsibility or not
}
class Parent
{
// normally again would have ManyToMany here to link students to the parent
}
class ParentStudent
{
/**
* #var boolean
* #ORM\Column(type="boolean", options={"default":true})
*/
private $responsibility = true;
// it's this part where i link student to parent and vice versa that's becoming confusing
}
This is typical Many to Many relationship with edge data / relationship data use case.
You Actually have relationship data class with extra meta data about their relationship, thus it becomes an Entity and you access it just like any other Entity class. Think this relation in terms of graph, nodes and edges. Your Student and Parent Entities are nodes while relationship between them is an edge with weight true/false (i.e 0/1).
For RDBMS, you solve it by introducing 3 entities:
Student
Parent
ParentStudent or StudentParent whichever suits and rhymes better with application
You can have as much as data to this relationship and also map it's ID to other relevant data to this association as it is an entity now.
Further You have Following relationships with each other which goes from OneToMany with intermediate entity and ManyToOne mapping inverse side, ultimately connecting both entities as ManyToMany via ParentStudent Entity:
<?php
/*
* #ORM\Entity()
*/
class Student {
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\OneToMany(targetEntity="App\Entity\ParentStudent", mappedBy="student", orphanRemoval=true)
*/
private $parentStudent;
}
/*
* #ORM\Entity()
*/
class Parent {
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\OneToMany(targetEntity="App\Entity\ParentStudent", mappedBy="parent", orphanRemoval=true)
*/
private $parentStudent;
}
/*
* #ORM\Entity()
*/
class ParentStudent {
/**
* #ORM\Id()
* #ORM\GeneratedValue()
* #ORM\Column(type="integer")
*/
private $id;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Parent", inversedBy="parentStudent")
* #ORM\JoinColumn(nullable=false)
*/
private $parent;
/**
* #ORM\ManyToOne(targetEntity="App\Entity\Student", inversedBy="parentStudent")
* #ORM\JoinColumn(nullable=false)
*/
private $student;
/**
* #var boolean
* #ORM\Column(type="boolean", options={"default":true})
*/
private $responsibility = true;
}

What is the reason why OneToOne relationship in Doctrine cannot be lazy loaded?

I have a Person and Admin entity. One Person can only have one Admin, but I do not need admin all the time. When I list all people with admin associated, doctrine will load all admins from DB. Why is this happening?
In a case of one-to-one association (and in similar cases, e.g. while lazy loading one-to-many association) Doctrine generates so called proxy objects for associated entities. These proxy objects mimics interface of target entity but only triggers actual data loading from database upon access to non-id field.
Because of this in your case when you're fetching list of Person entities - you doesn't get list of Admin entities fetched from database, but receiving list of Admin proxies instead. Unless you will (occasionally or intentionally) try to access some of properties of Admin entity (with exception of its id which can be safely accessed) Doctrine will not try to fetch any Admin information from database.
Consider following simplified setup of A and B entities with one-to-one association:
// A.php
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity()
*/
class A
{
/**
* #var integer
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var B
* #ORM\OneToOne(targetEntity="App\Entity\B")
*/
private $b;
/**
* #return int
*/
public function getId(): int
{
return $this->id;
}
/**
* #return B
*/
public function getB(): B
{
return $this->b;
}
}
// B.php
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* #ORM\Entity()
*/
class B
{
/**
* #var integer
* #ORM\Column(type="integer")
* #ORM\Id
* #ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* #var string|null
* #ORM\Column(type="string", nullable=true)
*/
private $name;
}
You can proof that A::$b contain proxy object by either looking into debugger or by using reflection:
$entities = $this->getEntityManager()->getRepository(\App\Entity\A::class)->findAll();
/** #var \App\Entity\A $a */
$a = array_shift($entities);
$class = (new \ReflectionObject($a->getB()))->getName();
In this case value of $class variable will be Proxies\__CG__\App\Entity\B (Doctrine proxy object for App\Entity\B) and not App\Entity\B as it would be in a case of normal entity object.

doctrine2 merge object with relation

I have an entity with a OneToMany relation to itself (note that it uses single table inheritance).
/**
* #ORM\Table()
* #ORM\Entity()
* #ORM\InheritanceType("SINGLE_TABLE")
* #ORM\DiscriminatorColumn(name="discr", type="string", length=30)
**/
abstract class PlatformPost
{
// [...]
/**
* #var PlatformPost
*
* #ORM\ManyToOne(targetEntity="PlatformPost", inversedBy="comments")
*/
private $parent;
/**
* #var PlatformPost[]|Collection
*
* #ORM\OneToMany(targetEntity="PlatformPost", mappedBy="parent", orphanRemoval=true, cascade={"ALL"})
* #Assert\Valid()
*/
private $comments;
// [...]
}
I create this entity from an api and then persist it. If it already exists i use $em->merge($post) to update it.
This only kind of works. The entity I get from that method is exactly like I want it but doctrine does not update the foreign key of the comments. If I query the post again the comment array still contains the old entities which should have been deleted.
A quick solution would be to remove all comments before merging but i'd like a better solution.

Bidirectional One-To-One Relationships in Flow

Is it possible to have One-To-One Relationships in Flow without having to set the attributes twice?
I have two tables that are connected in a One-To-One Relationship, but only one of them should contain an extra column for this Relation.
Doctrine clearly supports this behavior:
http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html#one-to-one-bidirectional
The class that should come with a componenttape column:
/**
* #Flow\Entity
*/
class Component{
/**
* #var \Some\Package\Domain\Model\Component\Tape
* #ORM\OneToOne(cascade={"all"}, inversedBy="component")
*/
protected $componentTape;
…
}
The class that should just be able to find the connection without an extra column:
/**
* #Flow\Entity
*/
class Tape{
/**
* #var \ Some\Package\Domain\Model\Component
* #ORM\OneToOne(mappedBy="componentTape")
*/
protected $component;
}
A doctrine update will create extra columns for both models.
This is what my workarround at the moment looks like:
class Component{
..
/**
* #param \Some\Package\Domain\Model\Component\Tape $componentTape
* #return void
*/
public function setComponentTape($componentTape) {
$this->componentTape = $componentTape;
$this->componentTape->setComponent($this);
}
The workaround will be necessary anyway to keep the relation correct at all times during a request.
But the second DB column shouldn't be necessary. Did you check if doctrine actually fills it? Maybe/Probably just the created migration is wrong and the component column in Tape can be omitted.
Does your workaround stil work for you?
In my case, I have to update the ComponentTape model on the repository by self:
class Component {
/**
* #param \Some\Package\Domain\Model\Component\Tape $componentTape
* #return void
*/
public function setComponentTape($componentTape) {
$this->componentTape = $componentTape;
$this->componentTape->setComponent($this);
$this->componentTapeRepository->update($this->componentTape);
}

Table already exist

I am using doctrine 2 in zend framework 2. Below is my entity file. The problem is, when I tried to validate schema using,
./vendor/bin/doctrine-module orm:validate-schema
command.
I am getting error,
[Doctrine\DBAL\Schema\SchemaException]
The table with name 'database.opportunitycriteria' already exists.
What should I do?
namespace Administration\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* OpportunityCriteria
*
* #ORM\Table(name="OpportunityCriteria")
* #ORM\Entity
*/
class Criteria
{
/**
* #var integer
* #ORM\Id
* #ORM\Column(name="criteria_id", type="integer", nullable=false)
*/
private $criteria_id;
/**
* #var string
*
* #ORM\Column(name="description", type="string", nullable=false)
*/
private $description;
}
and appropriate getter and setter methods..
I finally figured it out. OP's use case may be different, but in my case, this was because of a misconfigured bidirectional many-to-many relationship.
I had the following entities:
class Cuisine {
/**
* #ManyToMany(targetEntity="Dish")
* #ORM\JoinTable(name="CuisineDish", ...)
*/
protected $dishes;
}
class Dish {
/**
* #ORM\ManyToMany(targetEntity="Cuisine")
* #ORM\JoinTable(name="CuisineDish", ...)
*/
protected $cuisines;
}
What was missing was the inversedBy and mappedBy properties of the #ManyToMany annotations. These are only required when the association is bi-directional.
So now the correctly mapped entities look like:
class Cuisine {
/**
* #ManyToMany(targetEntity="Dish", inversedBy="cuisines")
* #ORM\JoinTable(name="CuisineDish", )
*/
protected $dishes;
}
class Dish {
/**
* #ORM\ManyToMany(targetEntity="Cuisine", mappedBy="dishes")
* #ORM\JoinTable(name="CuisineDish", ...)
*/
protected $cuisines;
}
And orm:validate-schema does not exit with an exception any more.
The exception message is just misleading, as the database is not altered by this operation. Furthermore, this issue is only spotted when validating the sync with the database, not when validating the mapping only (--skip-sync), where it should.
I just reported this bug.
it can cause this error message if you want to use a table name which is already used by one of the installed bundles.