Symfony2 - Doctrine2 Association Class - symfony

I am having trouble converting a ManyToMany relationship to utilize an Association Class. I have 2 classes Asset and User Currently I have a ManyToMany on Asset#users and User#Assets
I needed to add some extra data so I now have 3 classes Assets Users and Associations. Asset#users and User#assets are now a OneToMany with Associations and Associations#Asset and Association#User are a ManyToOne
In my previous implementation I had a FormType for Assets with the following:
->add('users', 'entity', array(
'class' => 'AppUserBundle:User',
'query_builder' => function($er) use($username){
return $er->createQueryBuilder('u')
->where('u.username != ?1')
->setParameter(1,$username);
},
'expanded' => "true",
"multiple" => "true"
))
This would display a list of users (except for the current user), and any user already associated would be checked.
With my new Schema I get the error Found entity of type App\UserBundle\Entity\User on association App\AssetBundle\Entity\Asset#users, but expecting App\AssetBundle\Entity\UserAssociation
This makes sense as it is now looking for the UserAssociation and not the User
I need the form to display All Users (minus the logged in user) and show which users are already Associated with the Asset. I am lost at how to do this.

Related

Symfony2 - related entity contains 'isDefault' property how to use in related entity form

I have 2 related entities, e.g. Book and Publisher (Book has one publisher, publisher has many books).
When editing\adding a Book I want to present a select of the Publishers.
Publishers has a property 'isDefault' on of the Publisher records will be marked as isDefault TRUE.
How do I make use of this in my add/edit form to pre-select the default Publisher?
I would recommend injecting publisherRepository as a service into your form.
And then declare a field something like this:
$builder->add('publishers', 'choice', array(
'choices' => $this->publisherRepository->findAll(),
'data' => $this->publisherRepository->findOneBy(['isDefault' => true]),
));

Symfony2 FOSUserBundle: accessing DB inside profile build form

I'm using FOSUserBundle on Symfony2.
I've extended the profile form type to include my fields.
I'd like to pre-populate one of those fields with one value found in DB (not in the user entity).
Basically I need to access DB from inside the buildForm.
I don't want (if possible) to override the original controller.
EDIT: I probably cannot use the "entity" field type as that (as far as I understand) creates the equivalent of a choice (with values loaded from DB). I need to have the field editable. I need to have access to the current user entity so that I have access to its ID. With that ID I can perform a query and get a text value from my DB (it's a license associated to the user) and use that value to populate one of the text fields of my form. Could I possibly override the getLicense() method of the user class to perform my queries there? How can I have access to DB inside an entity?
Hints?
Thank you!
You need the Entity field:
http://symfony.com/doc/current/reference/forms/types/entity.html
Here's an example:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('foobar', 'entity', array(
'class' => 'DummyBundle:Test',
'property' => 'name',
'multiple' => false,
'expanded' => false,
'query_builder' => function(\Doctrine\ORM\EntityRepository $er) {
return $er->createQueryBuilder('i')
->orderBy('i.name', 'ASC');
}
));
}

Using two entities to create a form

I use one general form type class to create all forms in my application. From controllers I only pass some parameters to it so that it knows which fields to render. For example:
$form = $this->createForm(new CommonType($repository, $queryResultSet), new UsersEntity(),
[
'action' => $this->generateUrl('user_edit'),
'attr' => ['class' => 'stdform'],
'repository_name' => 'AcmeBundle:Users'
]);
And then CommonType class generates fields based on arrays from repository class.
However I need to add two entities now - the form has to contain fields from user and shop repositories/entities.
I thought about creating two forms here and connecting them, but I wouldn't be able to persist it to the database later.
How can I solve this problem?
I can't use form embedding because I have CommonType class here in my case.

Symfony2: Two owning sides on many-to-many?

Let's say I have an Person entity and a ResearchArea entity. There is a ManyToMany Doctrine relationship between them with simple join table, a Person can have multiple ResearchAreas and a ResearchArea can have multiple Persons.
In my database, there are thousands of Persons, but only around 10 ResearchAreas.
On the Person edit form, I want to present a checkbox list for each ResearchArea. This is easy in a form builder:
->add('researchAreas', 'entity', array(
'label' => false,
'class' => 'AcmeDemoBundle:ResearchArea',
'property' => 'title',
'required' => false,
'multiple' => true,
'expanded' => true,
))
When submitting the form and binding the request data, this works well and the ManyToMany is handled nicely.
However, when editing a ResearchArea entity, I want to also provide a way to manage the Persons associated with that area. However, I can't use the same entity form type as I did above because there are so many Person entities.
Instead what I want is a collection form type, where the user can add/remove Person entities. I can do this by rendering text fields for each "row" and accept an ID of the person to add.
To support an approach like that, I need to change the relationship from a ManyToMany to a OneToMany -> ManyToOne and make the join table its own entity. But in doing that, then I can no longer use the nice checkboxes on the Person form which would only work with a direct ManyToMany.
Am I just making this all too complicated? Is there a solution to this?
For a similar use case I simply used the entity field type, rendered as a multiselect (multiple => true, expanded => false) and improved with a jquery plugins. This worked quite well, but I didn't have thousands of entities.
->add('persons', 'entity', array(
'label' => false,
'class' => 'AcmeDemoBundle:Person',
'required' => false,
'multiple' => true,
'expanded' => false,
))
I used a jquery plugin (like Chosen) to improve the multiselect and make it more user friendly. With chosen you can simply use:
$(".chosen-select").chosen();
With a bit of custom css you might be able to style it to fit your needs.
I too, would advocate the entity approach, but I used select2 (http://ivaynberg.github.io/select2/) instead.
In Many-to-Many Ajax Forms (Symfony2 Forms) I have some fairly detailed implementation examples.
Select2 can support ajax loading and searching, so the 3000 researchers is not a big deal.

symfony doctrine with two entity managers manually bind select tag

I googled a lot trying to find an answer for my problem, but no luck.
I have two tables in two different db db1.pages and db2.layouts I have defined the two entity managers and I can successfully manage the two as unrelated entities.
My problem is on table db1.pages, I have a layout_id column which contains a reference to the db2.layouts.id column, I didn't created an association between the two because, from my understanding, is not supported by doctrine. I successfully managed to display a selection list of possible layouts on the PageType using the following code:
$builder
->add('name', null, array('label' => 'Name'))
->add('label', null, array('label' => 'Title'))
->add('home', null, array('label' => 'Homepage'))
->add('layoutId', 'entity', array(
'class' => 'USGMCMSBundle:Layouts',
'property' => 'label',
'label' => 'Layout',
))
but it doesn't select the correct layout matched by the value stored into layoutId. Is there a way to select the current selected item and keep it in sync when the user choose a different one?
Maybe using an EventSubscriber? but on which stage? Or is there a better solution?
Hope I made myself clear (sorry for my english)

Resources