In a Symfony2 application I have a MainBundle and distinct bundles which can be enabled or not. In the MainBundle I need to have the Model and a basic Entity. In an OtherBundle an Entity with the same table name than Entity in MainBundle.
Fixtures in MainBundle need to be loaded with or without the other bundles than MainBundle :
MainBundle
- Model
- Entity (Table name "test")
- Fixtures
OtherBundle
- Entity (Table name "test")
- Fixtures
OtherBundle2
- Entity (Table name="test")
- Fixtures
If i used the #ORM\MappedSuperclass for the Model, a #ORM\Entity for the Entity in MainBundle and #ORM\Entity in OtherBundle then Doctrine2 stop with the error "table already exists".
I cant use the Inheritance table as my model dont need to know about other entities in the other bundles. The #ORM\DiscriminatorMap cant point to OtherBundle.
Is there a way to do this ?
As mentioned by Jasper N. Brouwer it's esentially the same entity and the same table, so there is no point in doing what you're trying to do.
Create your entity in a bundle named for example "SharedEntityBundle" and use resolve_target_entity to relate to this entity from other bundles without them knowing about eachother.
http://symfony.com/doc/current/cookbook/doctrine/resolve_target_entity.html
That being said, there seems to be a solution with multiple entity managers:
Symfony 2 / Doctrine 2: Two Entities for the same table, use one in favour of the other
Related
I am working on a Symfony + Doctrine based project. Is it possible to query an entity class using a given table name?
I know that it is no problem to ask Doctrine which table name belongs to a given entity class but I need this the other way (table name -> entity class, NOT entity class -> table name).
Asking Doctrine for all table names of all configured entities does not work in my case, since I need this feature within a bundle which is used in different projects and thus does not know all the project entities...
I have three kinds of Users, every user has some FiscalData and is linked to a UserCredential entry.
Which translates in:
UserX (X=1,2,3) has two FKs referring to FiscalData and
UserCredential tables.
Using Doctrine2, reading the docs http://doctrine-orm.readthedocs.io/projects/doctrine-orm/en/latest/reference/association-mapping.html, I believe to need the MappedSuperClass pattern.
I have also read the following questions:
Doctrine 2 - One-To-Many with multiple Entities
Many-To-One with multiple target entities
Doctrine2, Symfony2 - oneToOne with multiple entities?
But in the docs is clearly stated that
A mapped superclass cannot be an entity, it is not query-able and persistent relationships defined by a mapped superclass must be unidirectional (with an owning side only). This means that One-To-Many associations are not possible on a mapped superclass at all. Furthermore Many-To-Many associations are only possible if the mapped superclass is only used in exactly one entity at the moment. For further support of inheritance, the single or joined table inheritance features have to be used.
So, how to achieve what I'm trying to achieve, which is a bidirectional relationship between UserX and FiscalData/UserCredential? (so that f.e. via Doctrine I can get a UserCredential and check what kind of profiles it has associated)
Any complete minimal code example showing how to enforce the relationship I'm looking for (and not just the MappedSuperClass inheritance shown in the docs will be highly appreciated.
Use an abstract entity instead of MappedSuperClass. Single-table is usually the way to go unless you're sure you want class/table.
<?php
/**
* #ORM\Entity
* #ORM\InheritanceType("SINGLE_TABLE")
* #ORM\DiscriminatorColumn(name="discr", type="string")
* #ORM\DiscriminatorMap({
* "mentor" = "Mentor",
* "protege" = "Protege"
* })
*/
abstract class User { ... }
I'm looking information, how use entity propety type as object.
I have entity Products and Categories. In Products Entity propety category is type object. How pass object Categories to that propety? Any ideas how that use? Any Example ?
I would say: don't use the object type. Instead use Doctrine's association mappping.
I would advice to use the one-to-many bidirectional or the many-to-many bidirectional if you want to be able to have one product in multiple categories too.
Follow these steps:
Place the examples in your own entity and change the annotations from #... to #ORM\...
generate the getters and setters with the commandline:
app/console doctrine:generate:entities AppBundle
update your database schema:
app/console doctrine:schema:update --force
Now check your created getter and setter functions. You might find something like getProducts() removeProduct() and addProduct().
I want to log all changes of an entity. I looked into Loggable doctrine extension as provided by the StofDoctrineExtensionsBundle.
I got it working for fields that store simple data, e.g. string and integers. But my entity also has ManyToMany relationship to another entity, e.g. Tags.
I am getting this error:
InvalidMappingException: Cannot versioned [tags] as it is collection in object - Hn\AssetDbBundle\Entity\Asset
Is there a way to log an entity with its relationships? I don't mind switching to another bundle.
Currently no bundles/extensions have this functionality out of the box. One option would be to implement it yourself. This can be done by making use of Doctrine Listeners. Particularly you need to listen to postUpdate and postPersist events - these happen when entity is updated and created and store your Tags there.
Another option is to get rid of ManyToMany relationship. For this create an intermediate entity AssetTag that would have OneToMany relationship to both Asset and Tag. After this is done, you can use EntityAudit Doctrine Extension, which supports this type of relationships.
Let' imagine we have several tables: table_item, table_category, table_items_status.
Which is updated by service in single mode (no relations) using their own entity.
Can i, and how, create one entity that will have only relatioship of this tables, for example something like that....
**
* #ORM\OneToOne(targetEntity="table_item")
* #ORM\JoinColumn(name="itemID", referencedColumnName="itemID")
**
private $tableItemIDByItemID
// ... getter\setter
**
* #ORM\Column(type="integer")
**
private $itemID;
// ... getter\setter
In php code i want simply call
$entity->setItemID(123);
$result = $entity->getTableItemIDByItemID();
And will get ArrayCollection() from table_item by itemID.
Main thing that I want create extra entity only with relationships for several tables and only unidirectional. I need this for creating entity without touching another for relationships.
You should look into entity repositories. These provide some basic functionality queries but you can extend them with your own custom queries (eg: getItemIDByItemID). It's called the repository pattern. An entity is just a object representation of your database.
Symfony2 manual:
"It's a good idea to create a custom repository class for your entity and add methods with your query logic there."
Some more information about repositories:
http://symfony.com/doc/current/book/doctrine.html#custom-repository-classes
http://www.zalas.eu/doctrine2-and-symfony2