Can't get any entity associated to a tree entity - symfony

I'm working on an e-learning application and I have to categorize courses. So my Section entity is one of Tree extension. In order to retrieve all courses of a section, I made the following assoication:
/**
* #ORM\OneToMany(targetEntity="Svi\FormationBundle\Entity\Course", mappedBy="section")
*/
private $courses;
The problem is that when I try get courses in twig doing this {% if section.courses|length > 0 %}, I receive this error message
Key "courses" for array with keys "id, titre, sommaire, slug, deactivated, lft, lvl, rgt, root, __children" does not exist in SviFormationBundle:Formation:see_courses.html.twig at line 22.
I made a dump on the section object and it displayed all attributes except ones of OneToMany and ManyToOne associations. Any help please? Isn't it possible to associate other entities to a Tree entity? If so how to make a nested classification in Symfony? Thanks.

In your query, you have to explicitely add the 'join' or 'leftJoin' to the 'Course' entity.

Related

Easy admin : collection of forms

I am trying to build a simple backend application based on EasyAdmin. The ORM is Doctrine.
I have set up 3 entities :
* Entity A has a unidirectional many-to-many relation to entity B
* Entity B has a one-to-one relation to entity C
* Entity C has some basic properties
In EasyAdmin I have set up the Entity A and Entity C. The purpose is to add/edit Entity B while adding/editing entity A. II found the following which is exactly what I want. symfony easyadmin one to many form.
But when I want to add an Entity B while adding Entity A, it creates a form with the appropriate fields but the form has its own save button. Which seems to conflict with the main save button. Results
Is there a working example of what I want to achieve or some documentation on how to do it ?
Regards
Christophe Absil
I think that a good idea would be to override the 'new' and 'edit' templates. You have here the official documentation to help you.
While rendering the form, you will be able to hide/add labels & buttons; in order to hide a form element, just add:
{{ form_end(form, {'render_rest': false}) }}
A final step would be to override EasyAdmin's AdminController in order to persist the second entity; this is the official page.
Good luck!

symfony create a table with attribute from multiple entities

I have different entities like:
Element, Status, Action, Contributor ...
And I need to create an entity, let say Synonym, which can be related to one of these previous entities
My entity Synonym, can be a synonym of the entity Element or Status or Action ...
I try to use a MappedSuperclass but I'm really lost.
I think this kind of relation is common and must be easy to create with doctrine but I can't find the good way.

Doctrine2 Many To Many Polymorphic Relations

I try to create a Many To Many Polymorphic Relations with Doctrine2 in Symfony2.
I would like a single entity that is associate dynamically with multiples entities.
I want to get this following schema:
posts
id: integer,
name: string
======
videos
id - integer
name - string
======
tags
id - integer
name - string
======
taggables
tag_id - integer
taggable_id - integer
taggable_type - string
In taggables entity :
tag_id is the of the associate tag
taggable_id is the id of associate post
taggable_type is the type of associate entity ie 'Posts"
And I would like it to be the same with the "video" where:
tag_id represents the id of associate tag
taggable_id is the id of associate videos
taggable_type is the name of associate entity ie 'Videos'
and all this without duplicating table.
i'ved test multiple solutions but i never got this result :/
Thank you in advance for your help.
You could solve this with OOP, using inheritance.
Define an abstract class Taggable, and make Post and Video extend that class. Then create a OneToMany from Tag to Taggable.
Doctrine will take care of everithing, supposed that you choose between Single Table Inheritance or Class Table Inheritance.
I would choose Class Table, though.
More on this subject here.

FOSUserBundle One-To-One mapped Entity not saved

Hi Folks i have a question regarding implementing One-To-One in Entity FosUserBundle.
User Entity Has One To One Mapping With Profile Entity. I have override the basic RegistrationFormType as per shown in the FOSUserBundle's documentation. record is also saved in both table. but mapping entities show me blank data. Please find respected gist file for same.
UserEntity Gist- https://gist.github.com/ajaypatelbardoli/2f0c81cbdf3b0d136785
ProfileEntity Gist - https://gist.github.com/ajaypatelbardoli/fd02025fd338ed90545e
ProfileFormType gist - https://gist.github.com/ajaypatelbardoli/18ef99a3d0bd1198debc
RegistratonFormType Gist - https://gist.github.com/ajaypatelbardoli/09c047425032391c2445
The problem with your implementation is that you do not update the owning side of the bidirectional association. The Doctrine documentation explicitly states:
See how the foreign key is defined on the owning side of the relation, the table Cart.
In your case the owning side is Profile which you can update automatically in setUserId() as folows:
public function setUserId(\XXXX\Bundle\UserBundle\Entity\User $userId = null)
{
$this->userId = $userId;
$userId->setProfile($this);
return $this;
}
You can access the data from both sides of the relation without problems, Doctrine will look up the corresponding entries.

Embedded form persistence : get or create

I work with Symfony2 and doctrine. I currently have an entity called Person. This entity is related to some other entities as a One-To-Many relation (as a Many-To-One unidirectional relation). I want each Person entity to be unique (what I have done in my database by using the UniqueConstraint annotation).
To be clear, I will assume that I have two entities called Home and Car which both have a Many-to-One relation to the target entity Person.
Then I am using forms to create or edit my entities Car and Home. In these forms, I display a embedded form to create a Person entity or select one existing. I explain : the first field of my embedded form is the name of the person. As the user type the name of the person, a list of the existing persons is displayed (using JQuery Autocomplete UI) and if the user select one of them the other fields are autocompleted.
The issue is when the user submit the form with an existing person, I caught an Integrity Error (and I know why, because of my Unique Constraint).
One of the first workaround is to add the id field as an hidden input in the embedded form.
But then the user can edit the other fields and corrupt the current entity.
So no.
Another one could be to prevent the persist in the controller if the Person already exist, but as I am using this in many other entities. I will have to duplicate my code, and I do not want to, as the unique constraint is related to the Person entity and not to the Car or Home entity.
So no again.
The workaround that I am working about is to use a PrePersist Listener waiting for a Person entity, but I do not know how to cancel persist (and maybe it is not possible).
I have the following code :
public function prePersist(LifecycleEventArgs $args) {
$entity = $args->getEntity();
if($entity instanceof Personne) {
$em = $args->getEntityManager();
$persons = $em->getRepository('MyBundle:Person')->findBy(array(
// unique fields
));
if(count($persons) > 0) {
// ... ???
}
}
I have tried a $em->detach but it is useless as I am already persisting the entity.
What I want it is just a kind of a "Get Or Create". I explain, there are only two cases :
the Person entity (not persisted) has all the same fields that one existing in the database (excepted the id field), so the Person entity is the one in the database. I have to "replace" it by the one in the database ;
the Person entity (not persisted) is unique in the database, so we create a new one (persist).
Create your own getOrCreate() method and call it inside your listener.
See this post Symfony2 doctrine FindOneOrCreate
Another possibility would be the data transformers. http://symfony.com/doc/current/cookbook/form/data_transformers.html

Resources