2011-05-09 60 views
2

所以,我知道,這只是處於測試階段的權利,但我們決定繼續這一代替1的Symfony2及其formbuilder

所以,我有了這個代碼

$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(); 

而它總是在我試圖去死的時候getForm()當我刪除'Role'的時候它就起作用了。

在我們的數據庫結構,角色是不同的實體,用戶,但是他們引用它們在給定當我嘗試並調用getForm()外鍵

錯誤IM是這個

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() /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 

沒有任何人有任何想法如何我可以解決這個問題?

回答

3

問題是,當symfony表單組件嘗試獲取角色的當前值(通過執行$user->getRole())它獲得了原則的代理對象而不是整數。要告訴它理解角色對象,您需要創建一個新的表單類型。

下面是手冊中的示例,如何將它們與使用embedded forms的用戶表單一起使用。 (產品==用戶,類別==角色)。

+0

使用實體類型結束,因爲添加值稍微容易一些,但這也適用 – Ascherer 2011-05-10 15:36:48

相關問題