gpt4 book ai didi

php - Symfony2 及其表单构建器

转载 作者:行者123 更新时间:2023-12-04 06:26:54 27 4
gpt4 key购买 nike

所以,我知道现在这只是测试版,但我们决定继续使用它而不是 1。

所以,我有这个代码

 $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() 时它总是会死当我删除“角色”时,它可以工作。

在我们的数据库结构中,角色是不同于用户的实体,但它们通过外键引用它们

我尝试调用 getForm() 时出现的错误这是
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

有人知道我该如何解决吗?

最佳答案

问题是当 symfony 表单组件尝试获取角色的当前值时(通过执行 $user->getRole() ),它获取的是学说的代理对象而不是整数。要告诉它了解 Role 对象,您需要创建一个新的表单类型。

这是手册中的示例,如何使用 embedded forms 将它们与用户表单一起使用. (产品 == 用户,类别 == 角色)。

关于php - Symfony2 及其表单构建器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5943436/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com