N
N
Nikita Gusakov2013-04-03 16:06:47
symfony
Nikita Gusakov, 2013-04-03 16:06:47

Deal with Doctrine?

There are two entities. The relationship between them is one to many.

#obj1
manyToOne:
        post:
            targetEntity: Site\DeskBundle\Entity\Post
            cascade:
                - persist
            inversedBy: dogs
            joinColumns:
                post_id:
                    referencedColumnName: id
#obj2
oneToMany:
        dogs:
            targetEntity: Site\DeskBundle\Entity\Dog
            mappedBy: post

Removed the remaining fields - they do not play a role.
Question 1:
Is it possible to simplify this construction:
$post = $form->getData()->setAddDate();<br>
$dogs = $post->getDogs();<br>
if (!empty($dogs)) {<br>
    $em = $this->getDoctrine()->getManager();<br>
    $em->persist($post);<br>
    foreach ($dogs as $dog) {<br>
        $dog->setPost($post);<br>
        $em->persist($dog);<br>
    }<br>
    $em->flush();<br>
}<br>

In fact, the option should do this cascade - persist, but errors fly out, I can tell you which ones.
question 2:
how to get post+dogs without queryBuilder? That is, it is necessary to join corny by id, but how to do it through ORM?
well, the 3rd question, not very important:
is it possible to make dynamic addition somehow better, here is my code:
var template = {<br>
    code: '{{ form_widget(form.dogs.vars.prototype) }}',<br>
    count: 0,<br>
    max: 6<br>
};<br>
function addDog(obj) {<br>
    var dogs = document.getElementById("add_dog_place");<br>
    template.count++;<br>
    if (template.count < template.max) {<br>
        var dog = document.createElement("li");<br>
        dog.innerHTML = template.code.split('__name__').join(template.count.toString());<br>
        dogs.appendChild(dog);<br>
    } else {<br>
        obj.setAttribute("disable", "true");<br>
    }<br>
}<br>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Romanenko, 2013-04-03
@hell0w0rd

1. How do you create a form? If you slip a clean model on it, everything should be saved automatically.
2. The question is not entirely clear. You select a post and do ->getDogs() for it. How do you plan to use this data
3. We use the method described in the documentation: symfony.com/doc/master/cookbook/form/form_collections.html
PS: add errors under the spoiler

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question