Skip to content

Instantly share code, notes, and snippets.

@docteurklein
Created December 19, 2011 16:55
Show Gist options
  • Save docteurklein/1497965 to your computer and use it in GitHub Desktop.
Save docteurklein/1497965 to your computer and use it in GitHub Desktop.
avoid errors when binding empty propel collection to a symfony2 form
<?php
$callback = function($data = null) {
if (empty($data)) {
return new \PropelObjectCollection();
}
return $data;
};
$builder
->appendClientTransformer(new CallbackTransformer($callback, $callback));
;
@willdurand
Copy link

hm can you explain me a bit more ?

@docteurklein
Copy link
Author

yep sorry for the loosy feedback :)

I have a FiscalPeriod object that has many municipalities. (one to many)
I have symfony form that maps the fiscal_period.municipalities as a 'collection' type. ( by_reference = false, delete = true, add = true).

When I empty the municipalities collection( eg: en empty array in the request), the form dataMapper tries to call FiscalPeriod::setMunicipalities(array()); ( with an array),
which results in a fatal error, because setMunicipalities waits for a PropelObjectColelction.

By adding a dataTransformer that transforms empty array to PropelCollection, I avoid the error.

@willdurand
Copy link

ok, so there is no problem here, right ?

@docteurklein
Copy link
Author

no problem anymore. I think it could be useful to either provide this dataTransformer in a PropelCollection type or describe the problem here: http://www.propelorm.org/cookbook/symfony2/mastering-symfony2-forms-with-propel.html

@willdurand
Copy link

willdurand commented Dec 20, 2011 via email

@docteurklein
Copy link
Author

interesting, I've maybe done something wrong. I'l try to dig into that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment