symfony2 entityMetadata->getFieldNames() not including relational fields - symfony

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

Related

Get request data (GET) in a Symfony (5) formType without using data_class

I'm building a formType to filter products on a collection page. You can set multiple select boxes which makes other auto filled or unnecessary. I want to be able to manipulate the formType based on the data like when using a data_class object. I'm not using data_class because the search isn't a persisted object which is saved to the database. I'm using a GET form.
For example 2 select boxes:
category
productType
When setting a category makes some of the productTypes unnecessary. So i want to not show it.
To do so in the formType I need the data of the request (GET) but I can't find a way to do so.
To retrieve data from the form, you can use $form->getData().
As you're in a GET context, I suspect you can take advantage from FormEvents (take a closer look to POST_SET_DATA event) and get rid of values you don't need.
One other thing I would like to point out, is that you still can use some kind of object that's not persisted to DB, like DTO or whatever.
Forms and entities are not related anyhow, neither in the usage nor in the intentions.

Symfony form, EntytyType/ChoiceType from 2 different entities

I need to create a dropdown in a Symfony form that contains entities from 2 different tables, for example:
Where EntityA and EntityB are two different classes and MySQL tables, with a different structure.
Is it possible to achieve this?
You should use the choice_loader option of a ChoiceType field.
You can use a CallbackChoiceLoader to load your entities through a closure or implement your own choice loader as a service that you can inject to your form type.
This allows you to build a custom query (or execute two queries in this case) and return the choice list built from the results lazily.
Check the official documentation here (https://symfony.com/doc/current/reference/forms/types/choice.html#choice-loader).

Symfony Mapping in Doctrine without Annotations or XML Files

a customer has an existing database. The schema is often changed within the database itself (e.g. he adds a new column).
My task is to develop an admin area with symfony that automatically reacts on table schema changes without modifying the application code. E.g. the customer adds a new column to table "MyEntity", and the application automatically generates a new column in the accordingly list view.
My approach is to dynamically map the table columns to the Entity class so that ALL Attributes and ALL Getters/Setters are generated dynamically from the table schema.
So is it possible to map the table columns in a Doctrine Entity without the use of Annotations or XML Files.
Something like:
class MyEntity{
public function generateMappingFromSchema($sTableName){...}
}
Please don't do that. Doctrine was not designed for such use case.
There is a library though you should check https://github.com/laravel-doctrine/fluent which basically is a mapping driver that allows you to manage your mappings in an Object Oriented approach. And there are other tools:
http://crud-admin-generator.com/
http://crudkit.com/
http://www.grocerycrud.com/
which are maybe better for that, I don't know.
But again, please don't do that. Do not allow the customer to modify the database schema or give them e.g. a phpMyAdmin which was designed for that.

Get the entity context in a Data Transformer

I have a problem concerning the usage of a DataTransformer.
Basically, I am developing a translation tool for my application whose goal is to be as generic as possible.
For that, I chose to follow that model : Database modeling for international and multilingual purposes
So, in different entities in my application, I have translatable attributes that simply are references to i18n elements. Then, this i18n ID is referenced in Translation table entries, that handle translation strings.
I succeed handling my translation interface, but I now have a problem with my forms : Indeed, I want some of my entities to be created/updated via forms. The problem is that I don't want the user to set a i18n ID for the translatable fields, of course, but a text, so that it can be handled by my application to either update or create the related translation in database.
I thought then that creating a DataTransformer could be a good idea, so that I can get the related translation string from the i18nID that is in my Entity entry (for that way, no problem). But my problem here is for the opposite way :
How can I deal with creating/updating i18n entries in my reverseTransform() method without knowing the entity values context?
Is there any way to get the previous entity values so that I could get the i18 ID that is stored originally in my entity? I understand that a Data Transformer is theorically totally independent from my forms and my entities, but I'm totally blocked about how to handle this case.
Indeed, when I save my entity with my translated string, I have no way to know the entity context in my reverseTransform() method, that would have permitted me to get the i18nID of the entity and to update it.
I just have the string that typed the user, but I can't do anything with that, because I can't know if it is an update or not since I don't have access to my entities.
Do you have any clue to do that? Is trying to use a DataTransformer to perform this a bad idea?
Thank you !

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

Resources