Entity propetytype as object in Symfony2 - symfony

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().

Related

Is it possible to generate entities with automatic annotation

I would like to generate entities from DB with #ApiResource applied on each entity, is that possible ?
If you use the Maker Bundle, you can add an --api-resource flag for the make:entity command (See this merge request)

symfony create a table with attribute from multiple entities

I have different entities like:
Element, Status, Action, Contributor ...
And I need to create an entity, let say Synonym, which can be related to one of these previous entities
My entity Synonym, can be a synonym of the entity Element or Status or Action ...
I try to use a MappedSuperclass but I'm really lost.
I think this kind of relation is common and must be easy to create with doctrine but I can't find the good way.

How to log an entity that has collections?

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.

From Doctrine Query to QueryBuilder in a Simfony2 entity field type

I'm using the entity Field Type in a Symfony2.1 form. Here, I'm going to use the query_builder param to return only entities that matches a long-complex query (see the example in the official docs).
Obviously the query_builder param for the entity field type accepts a Doctrine QueryBuilder object. On the other side, I have large entity repositories with complex DQL queries obtained by the EntityManager's createQuery() function which returns a Doctrine Query object.
So, I cannot directly use all these queries in the entity field type. Moreover, rewriting all the queries for the use with a QueryBuilder would be a non-sense.
Is there such a way to automatically translate from the Query object to the QueryBuilder object?
From Symfony2 docs:
query_builder - type: Doctrine\ORM\QueryBuilder or a Closure <---
If specified, this is used to query the subset of options (and their
order) that should be used for the field. The value of this option can
either be a QueryBuilder object or a Closure. If using a Closure, it
should take a single argument, which is the EntityRepository of the
entity.
Now, I haven't got time to try an example but it seems to me that if you use Closure you could return ArrayCollection (or at least array) of target entity objects. Your Closure gets object of EntityRepository as an argument so there's no need to rewrite all that stuff.
Mind giving it a shot? :)
UPDATE
... sorry for kept you waiting...
It seems that it's not possible this way. Instead you would have to use choice form type and feed entity objects (or objects repository as I did) manually.
I've made some simplified example here: http://ideone.com/LHdi2E
Hope this helps...

symfony2 entityMetadata->getFieldNames() not including relational fields

I try to use $this->entityMetadata->getFieldNames() in Symfony2 to get all the fieldNames from a entity. The array I get in return does not include oneToMany relational fields or any other relational fields for that matter.
Is there a way to get all the fields by using that function? I could of course modify the original code but I wonder why the function does not include all the fields.
The bundle I am having problems with is Ddeboers otherwise excellent Data Import Bundle
The metadata class also includes a function called "getAssociationNames()" which returns the list of relational fields. Please take a look at
http://www.doctrine-project.org/api/orm/2.2/source-class-Doctrine.ORM.Mapping.ClassMetadataInfo.html
to get an idea of the structure of the information returned by this call

Resources