Form on entity linked to a relation - symfony

I have these entities :
Now, I need to create form to store data.
My problem is, I don't know how to figure out to create those forms.
What I want is a form where I can create a User, and here, I can select a Pack.
When I selected it, a list of Services of the selected pack appears, which I can checked if it is consumed by the User.
More complicated, I need to allow User to have several pack, and same pack is allowed.
It's look like a collection of User_Pack_Service form, but how to keep the Pack of each collection?
Other questions, since Pack_Service is a relation table of ManyToMany relation ship, it complicated the way of add Services in Pack. Is there a way to avoid is creation as entity? (I know I need it to link Pack and Service to User but that result in to many entities for nothing...)
Thanks for your help.

Related

Symfony2, 2 similiar types of users, best approach

i have this project where i have an issue, there are 3 relevant enteties.
User:
has_many: Leads
Bot:
has_many: Leads
Lead:
has_one: User/Bot
Now, a user and a bot share a lot of the same things, but they use different firewalls, they have many different fields etc, but i want a user and a bot to be interchangeable in regards of who a lead belongs to, it can either belong to a bot or a user, never both at the same time.
And in many of my other enteties where i run stats etc, i refere to a single field, i dont check if there is a user or a bot.
Is it possible to make these 2 enteties share the same Primary key and then just somehow refer to a single entity in the Lead field ?
Or what would be the best design approach in Symfony?
If you have all fields the same in 2 entities I would recommend you to drop Bot entity at all. All you need is just one field type with available values bot and user. To optimize SQL queries I would recommend you to declare this field as ENUM type.
Also if you really need different entities you can use Single table inheritance with discriminator field type described above.

Symfony 2 When to use collections and entities?

I feel pretty lost on collections, and entities as it stands.
My purpose:
A user will have one or more abilities. There are a set number of abilities, and numerous users. (A user entity, usertoabilities, abilities)
I want to display a form of the set abilities (lets say running, swimming, climbing), with properties such as (skill level, length of time).
The user would check each ability they have, and select their skill level and time.
My current understanding, is that my form will contain:
a collection of abilities (collection of entities), a collection of skill levels, and a collection times.
The form will print out each row of abilities with the corresponding properties. Where the user selects these abilities and saves them.
Is my understanding correct?
My current approach seems to have me going in circles.
you can create only two Entities (User and ability) and you need to create a ManyToMany Relation where you need to set a field (property) that is the relations field with some annotations where you wil set the "JoinTable" that in this case is "usertoabilities" for the two entities and more fields that will be the "abilities" you can find more information in http://doctrine-orm.readthedocs.org/en/latest/reference/association-mapping.html

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

Symfony working with different entity managers

I want to create an application with some subsections like a blog and a forum for example. Now I want users to be able to create an account on my site, and with that account they can use the forum and the blog. That's easy so far. But I want to keep the tables in seperate databases to keep a nice and clean structure. Let's say I use 3 databases, one for the UserBundle, one for the BlogBundle and one for the ForumBundle. This requires 3 entity managers. But that means that I can't use relationships from entities from ForumBundle or BlogBundle to the user entity in UserBundle. Simply adding UserBundle under the mappings for the other managers will create new tables in the other databases and that's the thing I'm trying to avoid.
So, is there a way to make bundles 'aware' of entities in other bundles?
I know it technically isn't a good thing to make bundles dependent on other bundles, but how else would I acheive my idea?
One approach that has worked reasonably well for me is to query using the event system.
Assume you are in the Forum Bundle, you have retrieved a list of posts for a given thread and now you need the user information for each post. You make an array of user id's then:
$userIds = array(...list of users you need information for...);
$findUsersEvent = new FindUsersEvent($userIds);
$dispatcher->dispatch('FindUsers',$findUsersEvent);
$users = $findUsersEvent->getUsers();
So now I have a list of user information all nicely indexed by user id from which I can then pull additional information.
The only coupling between the Forum and User bundles is the FindUsersEvent class which could be in a common bundle of some sort. The forum bundles does not care how the users are loaded. The user bundle just needs a listener.
==================================================================
A second approach is to basically use a REST like api for grabbing user information.

ASP.NET Dynamic Data: Access rights only to specific rows

I want to use ASP.NET Dynamic Data for my next project, but there is a problem a can't manage to solve. In the database we manage authorization on a per-row basis. For example no user is permitted to see all rows of the Contracts table. So there is a Many to Many Relationship between Contracts and Users. So everytime Dynamic Data performs a Select to show all Contracts it has to look into the ContractUsers junction table to see what contracts the current user is permitted to see (filtered by UserID which will be stored in a session variable). Of course these junction tables should be invisible to the users.
By default Dynamic Data returns all rows of a table, so is it possible to customize this behaviour for every query the user performs?
I want to use Dynamic Data together with LINQ to SQL but if this task would much easier to accomplish using Entity Framework I would look into that too.
Thanks for your help and time.
Implementing such a solution in Dynamic Data it will probably require the creation of a custom Entity Template; not really easy but once done it will not require the creation of custom pages just the editing of the page templates.
I think it will be really usefull to check the excellent work on DD done by S.J.Naughton and presented on his blog.
Greetings, F.
You should not use dynamic data because you need full control over querying and manually write all linq queries to add your data level security. If you still insist on dynamic data be aware that you will still write most of pages yourselves and you will only use dynamic templates. You will have to manually define ever data source and correctly pass where condition to filter results based on logged user.
In addition linq-to-sql is not able to hide junction table and entity framework is able to do that only if junction table contains just two FKs for many-to-many relation. If this table contains any other column you want to use in the application you will have to map it as any other entity and dynamic data will show it as an entity.
Dynamic data are technology for quick creation of simple application where you need to provide access to database through web interface but what you describe is not a simple scenario. You need per record authorization which can differ among entity types.

Resources