Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Embedded forms not working after last vendors update (Sonata origin/2.0) #727

Open
psyray opened this Issue · 8 comments

3 participants

@psyray

Since last update i can't get work any embedded forms.

The following error where returned in the ajax call :
"Expected argument of type "array or \Traversable", "CDL\EventBundle\Entity\SpeakerAction" given"

Where SpeakerAction is my entity.

Everything is working fine until update.
No modfication to my code.

Example of embedded field in my admin class

                    ->add('speakerActions', 'sonata_type_collection',
                            array('by_reference' => false), array(
                                    'edit' => 'inline',
                                    'inline' => 'table',
                            ))

Is there any other person in this case ?

@Romain-Geissler

Hi,

I am using similiar field configuration (without specifying 'by_reference' => false) and it works fine with the current 2.0 branch + a few patches of mine not affecting this. Can you provide a stacktrace, or at least the location where the original exeception is thrown ? Do you know when was the last commit where all worked fine for you ?

@psyray

I checked twice deps file and sonataadmin documentation, same problem.
Last commit was before switching to orig/2.0 in deps file
Maybe i forgot to add something elsewhere

Thanks for your help

Ajax call is :

app_dev.php/admin/core/append-form-field-element?code=cdl.speaker.admin&elementId=s4fa1567c3aed2_speakerActions&objectId=76&uniqid=s4fa1567c3aed2&_xml_http_request=true

Stack trace is:

Expected argument of type "array or \Traversable", "CDL\EventBundle\Entity\SpeakerAction" given
500 Internal Server Error - UnexpectedTypeException
Stack Trace

    in /opt/data/Eclipse/Coeur-de-livres-PRO-GIT-BUG/vendor/bundles/Sonata/AdminBundle/Form/EventListener/ResizeFormListener.php at line 74  -+
                }
                if (!is_array($data) && !$data instanceof \Traversable) {
                    throw new UnexpectedTypeException($data, 'array or \Traversable');
                }
                // First remove all rows except for the prototype row
2    at ResizeFormListener ->preSetData (object(DataEvent))
3    at call_user_func (array(object(ResizeFormListener), 'preSetData'), object(DataEvent))
    in kernel.root_dir/cache/dev/classes.php at line 3781  -+
4    at EventDispatcher ->doDispatch (array(array(object(ResizeFormListener), 'preSetData'), array(object(EnsureCsrfFieldListener), 'ensureCsrfField')), 'form.pre_set_data', object(DataEvent))
    in kernel.root_dir/cache/dev/classes.php at line 3707  -+
5    at EventDispatcher ->dispatch ('form.pre_set_data', object(DataEvent))
    in /opt/data/Eclipse/Coeur-de-livres-PRO-GIT-BUG/vendor/symfony/src/Symfony/Component/Form/Form.php at line 383  -+
6   at Form ->setData (object(SpeakerAction))
    in /opt/data/Eclipse/Coeur-de-livres-PRO-GIT-BUG/vendor/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php at line 63  -+
7    at PropertyPathMapper ->mapDataToForm (object(Speaker), object(Form))
    in /opt/data/Eclipse/Coeur-de-livres-PRO-GIT-BUG/vendor/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php at line 54  -+
 8   at PropertyPathMapper ->mapDataToForms (object(Speaker), array('_token' => object(Form), 'confirmed' => object(Form), 'event' => object(Form), 'author' => object(Form), 'library' => object(Form), 'speakerCategories' => object(Form), 'speakerActions' => object(Form), 'dedicate' => object(Form), 'comments' => object(Form)))
    in /opt/data/Eclipse/Coeur-de-livres-PRO-GIT-BUG/vendor/symfony/src/Symfony/Component/Form/Form.php at line 406  -+
 9  at Form ->setData (object(Speaker))
    in /opt/data/Eclipse/Coeur-de-livres-PRO-GIT-BUG/vendor/bundles/Sonata/AdminBundle/Admin/AdminHelper.php at line 136  -+
 10   at AdminHelper ->appendFormFieldElement (object(SpeakerAdmin), 's4fa1567c3aed2_speakerActions')
    in /opt/data/Eclipse/Coeur-de-livres-PRO-GIT-BUG/vendor/bundles/Sonata/AdminBundle/Controller/HelperController.php at line 81  -+
 11  at HelperController ->appendFormFieldElementAction (object(Request))
 12   at call_user_func_array (array(object(HelperController), 'appendFormFieldElementAction'), array(object(Request)))
    in kernel.root_dir/cache/dev/classes.php at line 3905  -+
 13    at HttpKernel ->handleRaw (object(Request), '1')
    in kernel.root_dir/cache/dev/classes.php at line 3875  -+
 14   at HttpKernel ->handle (object(Request), '1', true)
    in kernel.root_dir/cache/dev/classes.php at line 4879  -+
 15  at HttpKernel ->handle (object(Request), '1', true)
    in kernel.root_dir/bootstrap.php.cache at line 547  -+
 16   at Kernel ->handle (object(Request))
    in /opt/data/Eclipse/Coeur-de-livres-PRO-GIT-BUG/web/app_dev.php at line 28  -+

Logs  +-
1 error

    Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest".
    Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\RouterListener::onEarlyKernelRequest".
    Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest".
    Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest".
    Read SecurityContext from the session
    Reloading user from user provider.
    SET NAMES UTF8 ([])
    SELECT t0.username AS username1, t0.username_canonical AS username_canonical2, t0.email AS email3, t0.email_canonical AS email_canonical4, t0.enabled AS enabled5, t0.salt AS salt6, t0.password AS password7, t0.last_login AS last_login8, t0.locked AS locked9, t0.expired AS expired10, t0.expires_at AS expires_at11, t0.confirmation_token AS confirmation_token12, t0.password_requested_at AS password_requested_at13, t0.roles AS roles14, t0.credentials_expired AS credentials_expired15, t0.credentials_expire_at AS credentials_expire_at16, t0.created_at AS created_at17, t0.updated_at AS updated_at18, t0.two_step_code AS two_step_code19, t0.id AS id20 FROM fos_user_user t0 WHERE t0.username_canonical = ? (["admin"])
    Username "admin" was reloaded from user provider.
    Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\RouterListener::onKernelRequest".
    Matched route "sonata_admin_append_form_element" (parameters: "_controller": "sonata.admin.controller.admin:appendFormFieldElementAction", "_route": "sonata_admin_append_form_element")
    Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest".
    Notified event "kernel.request" to listener "Ps\PdfBundle\EventListener\PdfListener::onKernelRequest".
    Notified event "kernel.controller" to listener "Ps\PdfBundle\EventListener\PdfListener::onKernelController".
    Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RequestDataCollector::onKernelController".
    Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController".
    Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController".
    Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController".
    Notified event "kernel.controller" to listener "JMS\SecurityExtraBundle\Controller\ControllerListener::onCoreController".
    SELECT t0.id AS id1, t0.confirmed AS confirmed2, t0.dedicate AS dedicate3, t0.comments AS comments4, t0.event_id AS event_id5, t0.author_id AS author_id6, t0.library_id AS library_id7 FROM speaker t0 WHERE t0.id = ? (["76"])
    SELECT t0.id AS id1, t0.name AS name2 FROM category t0 WHERE t0.id = ? (["2"])
    SELECT t0.id AS id1, t0.name AS name2, t0.description AS description3, t0.date AS date4, t0.start AS start5, t0.end AS end6, t0.animator AS animator7, t0.location_id AS location_id8, t0.action_category_id AS action_category_id9, t0.event_id AS event_id10 FROM action t0 WHERE t0.id = ? (["105"])
    SELECT t0.id AS id1, t0.name AS name2, t0.description AS description3, t0.date AS date4, t0.start AS start5, t0.end AS end6, t0.animator AS animator7, t0.location_id AS location_id8, t0.action_category_id AS action_category_id9, t0.event_id AS event_id10 FROM action t0 WHERE t0.id = ? (["211"])
    SELECT t0.id AS id1, t0.name AS name2, t0.description AS description3, t0.date AS date4, t0.start AS start5, t0.end AS end6, t0.animator AS animator7, t0.location_id AS location_id8, t0.action_category_id AS action_category_id9, t0.event_id AS event_id10 FROM action t0 WHERE t0.id = ? (["46"])
    SELECT t0.id AS id1, t0.name AS name2, t0.start AS start3, t0.end AS end4, t0.budget_hotel AS budget_hotel5, t0.budget_restaurant AS budget_restaurant6, t0.budget_transport AS budget_transport7, t0.budget_remuneration AS budget_remuneration8, t0.print_header_id AS print_header_id9 FROM event t0 WHERE t0.id = ? (["1"])
    SELECT t0.id AS id1, t0.civility AS civility2, t0.lastname AS lastname3, t0.firstname AS firstname4, t0.nickname AS nickname5, t0.birthdate AS birthdate6, t0.comments AS comments7, t0.editor_id AS editor_id8, t0.country_id AS country_id9 FROM author t0 WHERE t0.id = ? (["167"])
    SELECT t0.id AS id1, t0.name AS name2, t0.address1 AS address13, t0.address2 AS address24, t0.postcode AS postcode5, t0.city AS city6, t0.tel AS tel7, t0.mobile AS mobile8, t0.email AS email9, t0.fax AS fax10, t0.im AS im11, t0.website AS website12, t0.country_id AS country_id13 FROM library t0 WHERE t0.id = ? (["2"])
    Notified event "kernel.exception" to listener "Symfony\Component\Security\Http\Firewall\ExceptionListener::onKernelException".
    Notified event "kernel.exception" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelException".
    Notified event "kernel.exception" to listener "Symfony\Component\HttpKernel\EventListener\ExceptionListener::onKernelException".
    Symfony\Component\Form\Exception\UnexpectedTypeException: Expected argument of type "array or \Traversable", "CDL\EventBundle\Entity\SpeakerAction" given (uncaught exception) at /opt/data/Eclipse/Coeur-de-livres-PRO-GIT-BUG/vendor/bundles/Sonata/AdminBundle/Form/EventListener/ResizeFormListener.php line 74
    Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest".
    Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\RouterListener::onEarlyKernelRequest".
    Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest".
    Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest".
    Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\RouterListener::onKernelRequest".
    Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest".
    Notified event "kernel.request" to listener "Ps\PdfBundle\EventListener\PdfListener::onKernelRequest".
    Notified event "kernel.controller" to listener "Ps\PdfBundle\EventListener\PdfListener::onKernelController".
    Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RequestDataCollector::onKernelController".
    Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController".
    Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController".
    Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController".
    Notified event "kernel.controller" to listener "JMS\SecurityExtraBundle\Controller\ControllerListener::onCoreController".
@psyray

Here more info

Commit working as expected on branch master


commit c6194bf
Merge: 89b8a91 b63023a
Author: Thomas thomas.rabaix@soleoweb.com
Date: Thu Mar 15 07:57:02 2012 -0700

Merge pull request #622 from ShiraNai7/master    
Czech translation

I do:
1. Updating deps file from Sonata master to Sonata 2.0 branch and Symfony 2.0.13
2. Vendors install
3. Test -> It seemes to be fine, some change in config.yml for intl...
4. But not....All the embedded form (1tomany) add button not working
Seems ajax call not working with previous stack error
Error 500
Seems also that certain embedded form not producing 500 error, status is 200, but nothing
inside the ajax return. Only current html, no form fields, no animation.

I tried to deeply debug, seems that in the file Sonata\AdminBundle\Form\EventListener\ResizeFormListener
the $event->getData() not returning correct Doctrine\ORM\PersistentCollection.
Entity is returned instead.

    public function preSetData(DataEvent $event)
    {
        $form = $event->getForm();
        $data = $event->getData();

        if (null === $data) {
            $data = array();
        }

        if (!is_array($data) && !$data instanceof \Traversable) {
            throw new UnexpectedTypeException($data, 'array or \Traversable');
        }

        // First remove all rows except for the prototype row
        foreach ($form as $name => $child) {
            $form->remove($name);
        }

        // Then add all rows again in the correct order
        foreach ($data as $name => $value) {
            $options = array_merge($this->typeOptions, array(
                'property_path' => '['.$name.']',
            ));

            $form->add($this->factory->createNamed($this->type, $name, $value, $options));
        }
    }

I tried a vendor full reinstall without success

So commit not working properly


commit a5e7dc2
Author: Thomas Rabaix thomas.rabaix@ekino.com
Date: Wed May 2 11:26:26 2012 +0200

Fix test

Thanks for help

@Romain-Geissler

I just upgraded to Symfony 2.0.13 as you did and still i can't reproduce your issue. I won't be able to help until i can reproduce.

@psyray

You have updated from master branch of sonata (from commit above) to sonata 2.0 branch first ? or you just update symfony with sonata 2.0 branch ?
I updated to symfony 2.0.13 from 2.0.11 AFTER sonata master branch to 2.0 branch.
In two times, so two vendors install. two deps update.
It's the update from master to 2.0 which breaks embedded forms.
The update to symfony 2.0.13 was just a test to see if it was the problem, but no.
Problem remains after symfony upgrade.

I checked my admin calls and my deps file.
It seems to be correct. I don't know what's wrong.
Why the entity is returned instead of the persistant collection ?
Mystery.

Thanks

@Romain-Geissler

I switched from sonata master to sonata 2.0 for one or two weeks now (a few days after it was created) for both AdminBundle and DoctrineORMAdminBundle.

I then (this morning) upgraded from Symfony 2.0.12 to 2.0.13.

If you can't debug it by yourself, i think your best bet is trying to provide a minimal test case exposing the issue : copy your current project and remove all entities but the two involved in the oneToMany relation. Just let one dummy string property in both remaining entities. Remove all validation or complex logic related to these entities.

I am not the project owner, but might help on my free time if i can reproduce the issue.

@psyray
@lmackiewicz

I've had same problem. In my case, I've just forgot to add "[]" in setter in entity :

public function __construct() {
    $this->roles = new \Doctrine\Common\Collections\ArrayCollection();
}

public function addRoles($roles)
{
    $this->roles[] = $roles; // problem cose : $this->roles = $roles;

    return $this;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.