How to reattach multiselect in Laravel 5? - php

I have mulutiselect option enabled in select html code, my frontend look like this:
For example if user unselect some option and select other how can I update it in database, any idea?

If you have ManyToMany relationships between say Group and Project you can use sync() method to maintain association as below,
$group->projects()->sync([$projId1, $projId2]);
Above will remove all previous association between current group($group) and projects and associates newly supplied projects i.e. $projId1, $projId2.
If you want to maintain previous associations pass false as a second argument in sync() as below,
$group->projects()->sync([$projId1, $projId2], false);
Above code will maintain previous Group and Project association and will also associate passed projects.

Related

Doctrine2 update relations many to many (without Symfony)

I have 2 Doctrine Entities with many-to-many relations. When I edit the first entity I want to be able to select the checkboxes that have the data from the 2nd entity to establish the joins for particular entry.
It works fine on creating a new Entry (using Array Collection), but when I want to edit an Entry - it adds the ones that I have selected without removing the previous choice (unchecking).
Which way would be the correct way to do that and how?
Remove all the Join table data for the Entry that is being updated,
then set the new data. (How can I remove it from the join table that
is not an Entity?)
Pass all the data from the 2nd Entity and remove
those that aren't checked (seems super-clumsy?)
Some other way I am not aware of?
I am not using Symfony, just Doctrine.
Doctrine makes working with the many-to-many associations quite easy. Your associations are stored into an ArrayCollection class that has some methods that can help you. First of all, check all the available methods for the ArrayCollection here (Doctrine API - ArrayCollection)
In your case, I'd use this approach: use the clear method on your ArrayCollection that contains the relationship with the 2nd entity and populate it again with the checked elements. After this, call the flush method on the entitymanager.
Another approach consists in filtering your collection (with the filter method) for getting a brand new ArrayCollection that contains only the elements that are checked. Like the first approach, associate this new collection to the relationship's ArrayCollection and call the flush method on the entitymanager.

How to tell doctrine:mapping:import to create own entity instead of junction-table

I have three tables:
banners (id, url, img)
banner_on_position (banner, position, loading)
banner_positions (id, name)
When I run the doctrine:mapping:import, doctrine creates only two tables. Banners and BannerPositions. Auto-generation doesn't create an own entity for the banner_on_position table.
Is there a way to access the loading attribute with querybuilder?
OR
How to tell doctrine to create own entity?
Until now I had no problems with auto generation of entities and it was very comfortable. I hope I don't have to create to code manually.
You have to create code manually in that case. If your many_to_many table have some extra properties then you need to use that trick: Both side need to use oneToMany relation for banner_on_position and banner_on_position need to have manyToOne to both tables.
Check how we did it in similar case (ContainerWidget have link to container, widget and custom parameter - position). https://github.com/superdesk/web-publisher/blob/master/src/SWP/Bundle/TemplateEngineBundle/Resources/config/doctrine/ContainerWidget.orm.yml
Here is also really good question and answers on stackoverflow: Doctrine2: Best way to handle many-to-many with extra columns in reference table

Doctrine 2 Many to Many with join table, mapping?

I'm trying to figure out the correct way to map my three entities together. I have a "HouseType", a "Resource" and a HouseType can have multiple Resources (with an integer indicating how many of that resource they have). So I have three tables, the house_type, resource and house_type_resource (with house_type_id, resource_id, and num).
I can't figure out what type of association mapping I should do. I'm reading this page: http://docs.doctrine-project.org/en/latest/reference/association-mapping.html but I don't see one that's like mine, where I have a third entity to represent the join table, because it has a specific property that needs to be included (the num). It's sort of like the "Many-To-Many, Unidirectional" but I need that third Entity mapped in there. I see the note at the end about doing an Association class but I don't see any more info on that.
Once you start adding extra columns, Doctrine stops treating it as a pure many-to-many relationship because there is extra data, and this data should be managed with an entity. You should create a new entity called HouseTypeResource, then create one-to-many relationships with this inside your HouseType and Resource entities.

Difference between HABTM relationship and 2 $belongsTo relationship with a third model

I'm creating a project management system which projects are assigned to users
What's the difference between creating a Model ProjectsUser and defining 2 $belongsTo relationship and defining HABTM relationships in both Project and User models? What would be the most correct way, though? And how do I save the data in the projects_users table?
From my experience, if you want to be able to save or delete rows only from the join table (the one with 2 IDs), then it is much more simple using three models associated through both a hasMany and a belongsTo association.
You can also retrieve data from the join table directly and do the queries you want much more easily
This is what CakePHP documentation says refering to HABTM and saving data:
However, in most cases it’s easier to make a model for the join table and setup hasMany, belongsTo associations as shown in example above instead of using HABTM association.
Here you can find more the full text:
http://book.cakephp.org/2.0/en/models/saving-your-data.html#what-to-do-when-habtm-becomes-complicated
I have used this method for a "reads" table (with post_id and user_id) as well as for subscriptions and similar kind of relationships.
The first way is called "hasAndBelongsToMany" [details here].
The second is called "hasMany through" [details here].
The second link relating to "hasMany through" has details and a lengthy explanation about when and why you would want to use it.
Not sure about the specifics of cakephp, but in general defining the relation model explicitly gives you more control over it, for instance if you wanted to do some validation or add callbacks on creation of this relationship.

Zend Framework: Handling of multiple database records in one form as checkboxes

I'm new to zend framework but have made my first steps with it successfully.
Until now I have created some Zend_Forms which are mapping single records of my model
to the form fields. I have handled the forms with form classes for each case.
This works all very well until now.
Now I have the situation that I have to asign features to a product. Features and products are parts of my application. Features are stored in my database in three tables. For each feature there is one record in the third table.
First is the feature group where the name of the feature group is saved. Every feature should be asigned to a feature group.
Second table is the features table. This table has an foreign key to the feature group and the name of the feature.
Third table is some kind of many-to-many relation which connects features to products. This table has an aditional field which contains an optional value (beside the two foreign keys) for this unique feature of the product.
For example: if the product has a weight of 4,78 kg the value "4,78" is stored in the third table and the label "weight of %s kg" is stored in the second table. The feature group could be something like "physical attributes" had is saved in the first table.
To cut a long story short:
My problem is how to handle the case that I have to create and edit multiple database records in one form. The plan is to have a form with many checkboxes for each for a feature whereby features are thematicaly grouped. Every checkbox should have an aditional text field to input optional values.
you could make a custom form class that extends Zend_Form and use that for you classes.
It could take in the construct instances of your models and construct the form inputs based on that models.
After form validation in your controller you can do
$values = $form->getValues();
and use that array to populate your models again
You can try creating subforms (Zend_Form_SubForm) inside your form class. This can separate fields for different tables. For edition, in your controller, when you pull all the data from the tree tables, you can populate subforms that correspond to the tables.
You can try to extend Zend_Form to create your own elements.
You will be able to write a class that connects to DB to get attributes (features & products).
Assuming you wrote My_Form_Element_Features & My_Form_Element_Products classes, you can do $features = new My_Form_Features(); and then use the base class methods like getValues(), populate(), etc.
You can take a look there to start :
http://framework.zend.com/manual/en/zend.form.elements.html
http://smartycode.com/extending/database-aware-select-elements/
--
To answer to your comment, you can use :
Zend_Form::setElementsBelongTo($array):
More information can be found at Zend_Form Advanced manual page.

Categories