Code Organization - Relation or Repository - symfony

I'd like to have your opinion about code organization. I have two entities: City and Country. I have an unidirectional ManyToOne between them, Many side is of course City.
Now, I need to get all the cities corresponding to a country. I have two choices:
Changing the relation to a bidirectionnal ManyToOne
Creating custom method in City repository
What is the best way to do so ?

Depends. If the two entities are in the same Bundle (or in Bundles that require each other's presence), then make it bi-directional, especially if you think this'll be a common thing to search for.
On the other hand, if this is a special case, the entities are in different Bundles, and you don't want to couple them further, then it's better to make a custom method for it.

It depends on what kind of data and how oftend you need it:
if you have the Country object and you need City objects, make it a 2-way ManyToOne
if you have the Country id and you need City objects, add a query to repository
if you have the Country id and you need City ids, add a query to repository
#Erik's response is also a good view on the subject

Related

ERD - issue about entities

Im making an ERD now for medicine care.
I have an issue with 2 entities:
Patient and caregiver.
The caregiver is a person that helps for the patient and can do features on the application for the patient.
For now I choose that the patient is a separate user from the caregiver.
If its like that,I need to do a seperate entities for the caregiver and for the patient. When I do this , there is an overload on the ERD because the 2 entities can do the same thing on the system so they have the same connections to other entities.
In addition , they have just one attribute that is different between them and this is the diagnosis of the patient.
What to do?
I would suggest looking into adding the caregiver as a sub class if all the rows are the same apart from one. But having them both as separate entities wouldnt be an overload on the database as long as you add primary/foreign keys correctly. Have you got a current ERD i can look at?

Symfony use setter for Arraycollection in CreateController

I'd like to create an upload form for documents.
It's possible to select several so called 'agencies' and each of these agencies belongs to one specific market.
What I want to do is, set the markets automatically.
So in my controller I had something like this:
$document->setMarket($document->getAgencies()->getMarket());
But since the agencies are an ArrayCollection, I can't call a getter on them. So I was thinking about a for each loop in order to get the markets for every single agency. Would that work and if yes how would I do it best and most efficiently?
Would be happy about tips and tricks :D

vtiger: Relationship with extra information

I would like to have a one-to-many relationship with extra information attached: Each organisation has a customer-id for a specific vendor (because purchase orders are processed in the name of the organisation).
What is the best way to achieve that?
If one organization can be related to only one vendor (have uitype 10 field), best way to store vendor specific Id - make additional field for organization. If relation is many-to-many - you have to add special table (like OrgToVendorRel) to store relation data. And all necessary functions to process it.
Oleg
Oleg.a#pinstudio.ru

Entity associations mapping, ORM and Data Modelling approach for a complex task

I'm working on a studio project to try to learn different approaches using Symfony2, Doctrine 2.4.7 as ORM and MySql 5.5 as DB. I've deliberately minimized my question for a better understandability and readability, if you need more details you only have to ask and apologize me if my english is not so good.
To avoid a large discussion due to the title of my question let me synthesize the problem showing a simple and common case (but complex for me because I'm new with doctrine).
The Model:
The User entity (mapped) that stores the user's data
The Category entity (mapped) that stores some categories associated to the User with a ManyToMany BD.
Each User can select one or more Categories.
The Problem:
User categories are near 100.
Many Categories could have a specific associated form.
Each form is composed by common and/or only specific fields (from 1 to 10 fields per category).
The Goal:
Understand what's the most balanced approach for this use case (in terms of flexibility and performances), for create the entities and associations needed to store the data filled by the user (some of these data I wish they were to be searchable).
Some Related References:
Doctrine2 docs
Serialized LOB
Extensible Data Modelling
and many other threads not much relevant...
A Possible Solution:
Create manually a form type for each category with inside the block of related fields (I use this forms as services in DIC and use blocks for the fields I need to reuse on more then one category).
Create a CategoryForm entity with the properties needed to retrieve the name of the form related to the category (useful to the form factory when I build the form) with an association ManyToMany UD with Category and to store the serialized LOB (the data coming from the form and related to the User).
There is a better approach to avoid the serialization of the object in a LOB? (maybe I'm wrong but serialized data are not searchable/indexable in mysql)
Any other solution or reference to a readable resource is welcome!
Well, I will try to answer the question with a simple guess: the category is something shared beetween several users (since you got the many to many).
So, if you want the User's form to be able to set (add/delete or update) Categories assiated to the user, then you should just have acollection of entity widgets related to the Category.
Why do I say that ?
Since your categories are linked to several users, the way you want to treat the relation beetween Categories and Users will cause any update on existing Category from a User's form to be propagated to other Users.
This means that Categories should be created/updated by a single form (modulo your needs). You can then link the Category to the User from User's form.
As far as the number of form of Categories is concerned, there are several parameters to handle:
Are all the Categories validated the same way (to know if you simply need to hide widgets to make the validation work) ?
Do you have a large amount of different types of Categories ?
If yes, are always composed the same way for a given type ?
Give further details if I'm wrong in my initial guess ;)

Getting information of linked topics on a single request

So, say I search for a City using Freebase API. Say, San Francisco:
https://www.googleapis.com/freebase/v1/topic/m/0d6lp?limit=20&filter=/common/topic/description&filter=/common/topic/article&filter=/location/location/geolocation&filter=/location/location/containedby&filter=/travel/travel_destination/tourist_attractions
I get a bunch of data, including the '/location/location/containedby', which refers by which other entities this one is contained by. This is how I can find out to which State and Country the city belongs to.
The problem is that I only get those entities name and mid, but not '/common/topic/notable_for', therefore, I have to make separate queries per each entity, asking just the notable_for property, to find out which one of those is a Country, a State, or other stuff I don't need.
In example, this is one of the queries, which determines United States of America is a country:
https://www.googleapis.com/freebase/v1/topic/m/09c7w0?filter=/common/topic/notable_for
This is executed between 3 to 6 times each city.
Is there a way to tell the API to include more information about these linked entities on a certain Topic? Like on this case, to include '/common/topic/notable_for' on linked entities. It would save tons of queries, and time to the end user in my case.
Thank you for your time!
You can actually get these results using the new output parameter on the Freebase Search API. Like this:
query=/m/0d6lp
output=(description /location/location/geolocation (/location/location/containedby notable))
Try it out
I'd suggest using the MQL Read API if you want better control on the information returned. Then you can specify nested queries that ask for the contained_by locations to be returned with their types (or you can explicitly filter to only those which are a country or a state).

Resources