Symfony2 and its formbuilder
So, i know that this is only in beta right now, but we made the decision to continue with this instead of 1.
So, i've got this code
$this->em = $this->get('doctrine.orm.entity_manager');
$user = $this->em->find('BlahBlahBundle:User', $id);
$dql = "SELECT r FROM \Blah\BlahBundle\Entity\Role r";
if($user->getRole()->getRoleId() != '4')
$dql .= " WHERE r.roleId != '4'";
$rs = $this->em->createQuery($dql)->getResult();
$roles = array();
foreach($rs as $item)
$roles[$item->getRoleId()] = $item->getName();
$userRole = $user->getRole()->getRoleId();
$radio = array('0' => 'No', '1' => 'yes');
$form = $this->buildForm('User', $user)
->add('ScreenName')
->add('Email')
->add('Password','password', array('required' => false))
->add('Enabled', 'choice', array('choices' => $radio))
->add('AllowedOutside', 'choice', array('choices' => $radio))
->add('Role', 'choice', array( 'choices' => $roles, 'preferred_choices' => array($userRole)));
$form = $form->getForm();
And its always dying when i try to getForm()
When i remove 'Role' it works.
In our database structure Roles are a different Entity than Users, but they reference them over a foreign key
the error im given when i try and call getForm()
is this
Notice: Object of class Proxies\BlahBlahBundleEntityRoleProxy could not be converted to int in /mnt/www/reportings/vendor/symfony/src/Symfony/Component/Form/Util/FormUtil.php on line 18
Call Stack:
0.0010 638064 1. {main}() /mnt/www/reportings/web/app_dev.php:0
0.0374 1859640 2. Symfony\Component\HttpKernel\Kernel->handle() /mnt/www/reportings/web/app_dev.php:13
0.0476 2073904 3. Symfony\Bundle\FrameworkBundle\HttpKernel->handle() /mnt/www/reportings/app/bootstrap.php.cache:613
0.0477 2074912 4. Symfony\Component\HttpKernel\HttpKernel->handle() /mnt/www/reportings/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/HttpKernel.php:35
0.0478 2074912 5. Symfony\Component\HttpKernel\HttpKernel->handleRaw() /mnt/www/reportings/app/bootstrap.php.cache:420
0.1955 3938880 6. call_user_func_array() /mnt/www/reportings/app/bootstrap.php.cache:442
0.1955 3939368 7. Blah\BlahBundle\Controller\UserController->editAction() /mnt/www/reportings/app/bootstrap.php.cache:442
0.2696 5721072 8. Symfony\Component\Form\FormBuilder->getForm() /mnt/www/reportings/src/Blah/BlahBundle/Controller/UserController.php:51
0.3463 6520408 9. Symfony\Component\Form\Form->setData() /mnt/www/reportings/vendor/symfony/src/Symfony/Component/Form/FormBuilder.php:454
0.3465 6521136 10. Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper->mapDataToForms() /mnt/www/reportings/vendor/symfony/src/Symfony/Component/Form/Form.php:378
0.3523 6543648 11. Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper->mapDataToForm() /mnt/www/reportings/vendor/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php:47
0.3529 6543648 12. Symfony\Component\Form\Form->setData() /mnt/www/reportings/vendor/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php:56
0.3531 6544512 13. Symfony\Component\Form\Form->normToClient() /mnt/www/reportings/vendor/symfony/src/Symfony/Component/Form/Form.php:369
0.3531 6544512 14. Symfony\Component\Form\Extension\Core\DataTransformer\ScalarToChoiceTransformer->transform() /mnt/www/reportings/vendor/symfony/src/Symfony/Component/Form/Form.php:898
0.3531 6544512 15. Symfony\Component\Form\Util\FormUtil::toArrayKey() /mnt/www/reportings/vendor/symfony/src/Symfony/Component/Form/Extension/Core/DataTransformer/ScalarToChoiceTransformer.php:21
Catchable fatal error: Object of class Proxies\BlahBlahBundleEntityRoleProxy could not be converted to string in /mnt/www/reportings/vendor/symfony/src/Symfony/Component/Form/Util/FormUtil.php on line 18
Call Stack:
0.0010 638064 1. {main}() /mnt/www/reportings/web/app_dev.php:0
0.0374 1859640 2. Symfony\Component\HttpKernel\Kernel->handle() /mnt/www/reportings/web/app_dev.php:13
0.0476 2073904 3. Symfony\Bundle\FrameworkBundle\HttpKernel->handle() /mnt/www/reportings/app/bootstrap.php.cache:613
0.0477 2074912 4. Symfony\Component\HttpKernel\HttpKernel->handle() /mnt/www/reportings/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/HttpKernel.php:35
0.0478 2074912 5. Symfony\Component\HttpKernel\HttpKernel->handleRaw() /mnt/www/reportings/app/bootstrap.php.cache:420
0.1955 3938880 6. call_user_func_array() /mnt/www/reportings/app/bootstrap.php.cache:442
0.1955 3939368 7. Blah\BlahBundle\Controller\UserController->editAction() /mnt/www/reportings/app/bootstrap.php.cache:442
0.2696 5721072 8. Symfony\Component\Form\FormBuilder->getForm开发者_如何学Go() /mnt/www/reportings/src/Blah/BlahBundle/Controller/UserController.php:51
0.3463 6520408 9. Symfony\Component\Form\Form->setData() /mnt/www/reportings/vendor/symfony/src/Symfony/Component/Form/FormBuilder.php:454
0.3465 6521136 10. Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper->mapDataToForms() /mnt/www/reportings/vendor/symfony/src/Symfony/Component/Form/Form.php:378
0.3523 6543648 11. Symfony\Component\Form\Extension\Core\DataMapper\PropertyPathMapper->mapDataToForm() /mnt/www/reportings/vendor/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php:47
0.3529 6543648 12. Symfony\Component\Form\Form->setData() /mnt/www/reportings/vendor/symfony/src/Symfony/Component/Form/Extension/Core/DataMapper/PropertyPathMapper.php:56
0.3531 6544512 13. Symfony\Component\Form\Form->normToClient() /mnt/www/reportings/vendor/symfony/src/Symfony/Component/Form/Form.php:369
0.3531 6544512 14. Symfony\Component\Form\Extension\Core\DataTransformer\ScalarToChoiceTransformer->transform() /mnt/www/reportings/vendor/symfony/src/Symfony/Component/Form/Form.php:898
0.3531 6544512 15. Symfony\Component\Form\Util\FormUtil::toArrayKey() /mnt/www/reportings/vendor/symfony/src/Symfony/Component/Form/Extension/Core/DataTransformer/ScalarToChoiceTransformer.php:21
does anybody have any idea how i can fix that?
The problem is when symfony form component tries to get the current value of the role (by doing $user->getRole()
) it gets the doctrine's proxy object instead of an integer. To tell it understand the Role object you need to create a new form type.
Here's the example in the manual how to use them together with user form using embedded forms. (Product == User, Category == Role).
精彩评论