Bidirectional One-To-One Relationships in Flow - doctrine-orm

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);
}

Related

how to add/remove an entity's mapping programmatically

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

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.

How do I order by a property that isn't a DB column using Doctrine?

When defining a relationship, there is a property on the related model (not a DB column), but I would like to sort by it (in the #OrderBy annotation).
I have a base model that is extended using single table inheritance. The property in question is basically an order property that is specified in each child class, but is not saved to the DB.
(I don't want to add an order column to the DB table, since the ordering depends purely on which child class the discriminator is mapped to. There is already a unique constraint so that each child class can be used no more than once in the relationship.)
Here's a really simplified version of my code...
Base entity:
/**
* #ORM\Entity
* #ORM\Table(name="base")
* #ORM\InheritanceType("SINGLE_TABLE")
* #ORM\DiscriminatorColumn(name="class_name", type="string")
*
* #ORM\DiscriminatorMap({
* "Base" = "Models\Base",
* "ChildA" = "Models\ChildB",
* "ChildB" = "Models\ChildA"
* })
**/
class Base
{
/** #ORM\Column(type="string") **/
protected $class_name;
/**
* #ORM\ManyToOne(targetEntity="Related", inversedBy="collection")
**/
protected $related;
// this is just a plain ol' property (not in the DB)
protected $order;
public function getClassName()
{
return $this->class_name;
}
}
Children:
/**
* #ORM\Entity
* #ORM\Table(name="child_a")
**/
class ChildA extends Base
{
$order = 1;
}
/**
* #ORM\Entity
* #ORM\Table(name="child_b")
**/
class ChildB extends Base
{
$order = 2;
}
Related entity:
/**
* #ORM\Entity
* #ORM\Table(name="related")
**/
class Related
{
/**
* #ORM\OneToMany(targetEntity="Base", mappedBy="related")
* #ORM\OrderBy({"order"="ASC"})
**/
protected $collection;
public function getCollection()
{
$em = App::make('Doctrine\ORM\EntityManagerInterface');
// map each Base instance to the appropriate child class
return $this->collection->map(function ($base) use ($em) {
$class_name = $base->getClassName();
return $em->find($class_name, $base->getId());
});
}
}
Is it possible to use the order property for ordering the collection relationship? (Ordering based on class_name using a switch-like construct would also be valid, but I haven't found any way to do that either, and it would be harder to maintain.)
Thanks in advance!
The directive beginning with ORM is very much telling Doctrine you're doing referencing a property that has a relationship with a table field. You can't use ORM directives on fields that don't exist. Doctrine annotations: OrderBy
You would have to implement this in a function, best in the model itself (within your getCollection() function), or if you're using a framework like Symfony place it in a function of the repository class for this entity. You'd have to use PHP sorting functions to do this. SQL/DQL won't work either because the property isn't related to a field in the table.

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.

How to cascade a user defined operation on an entity?

I'm trying to figure out a way to cascade an operation on all entities associated with an entity. For example, if I have a User, and a user has entities, and those entities have entities, I want to perform an operation on every entity. Specifically I want to perform validation on every entity in that tree.
How would I implement something like 'getAssociatedEntities()':
class User {
/**
* #ManyToOne(targetEntity="Comment")
*/
private $comment;
/**
* #ManyToOne(targetEntity="Something")
*/
private $something;
}
$user->setComment($comment);
$user->setSomething($something);
$associated_entities = $user->getAssociatedEntities(); // NOT A REAL METHOD
foreach ($associated_entities AS $entity) {
validate($entity);
}
I realize I could use lifecycle callbacks to perform validation. However, setting validation annotations makes things sooooo much easier. I can validate every entity I persist, BUT I can't validate its associated entities.
I can validate every entity I persist, but I can't validate its
associated entities.
Of course you can perform any action to entitied associated with main entity if:
You add cascade={"all"} to #ManyToOne definition (required !).
#HasLifecycleCallbacks for each sub-entity you want action performed and mark methods #PrePersist or #PostPersist or more in documentation.
For example I use this method to cascade deletion of image file in the Image entity, while the any entity that contains Image entity is beeing deleted:
Here I have an livecycle callback:
/**
* An image.
*
* #ORM\Entity
* #ORM\Table(name="images")
* #ORM\HasLifecycleCallbacks
*/
class ImageEntity extends AbstractEntity
{
/**
* #ORM\PreRemove
*/
public function preRemoveDeleteFile()
{
// remoce file
}
}
Here I have an entity that contains Image (where there is a cascade operation defined):
/**
* A competency group name.
*
* #ORM\Entity
* #ORM\Table(name="product_meta_image_cover")
*/
class ProductMetaImageCoverEntity extends AbstractEntity
{
/**
* #var \ModuleModel\Entity\ImageEntity
* #ORM\OneToOne(targetEntity="ModuleModel\Entity\ImageEntity", cascade={"all"}, orphanRemoval=true)
* #ORM\JoinColumn(onDelete="CASCADE")
*/
protected $image;
}