RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 648171
Accepted
Вадим Бондаренко
Вадим Бондаренко
Asked:2020-04-04 17:15:13 +0000 UTC2020-04-04 17:15:13 +0000 UTC 2020-04-04 17:15:13 +0000 UTC

交响乐形式。既不是属性“TM”,也不是其中一种方法

  • 772

构建表单时发生错误。

Neither the property "TM" nor one of the methods "getTM()", "isTM()", "hasTM()", "__get()" exist and have public access in class "Nitra\SchemaCDBBundle\Entity\MonthPlan.

错误在于它FormBuilder没有在 MonthPlan 实体类中找到任何方法或 TM 属性。但是我不明白他为什么要在 MonthPlan 实体中寻找这个方法。

我有一个班级MonthPlan,与员工的关系表示班级Employee

  /**
 * Сотрудник
 *
 * @ORM\ManyToOne(targetEntity="Employee", inversedBy="monthPlans")
 * @Assert\Type(type="Nitra\SchemaCDBBundle\Entity\Employee")
 */
protected $employee;

我需要为班级MonthPlan中的某些员工创建一个表格Employee。为此,我创建了一个类Form并声明了buildForm. 正如您在上次调用中看到的那样$builder->add(),我指定了选择员工所依据的类和查询以及显示结果所依据的属性'property' =>'name',但最后我得到了上面指出的错误。

 public function buildForm(FormBuilderInterface $builder, array $options)
{
    parent::buildForm($builder, $options);

    $builder->add('chosenType', 'choice', array(
        'required' => true,
        'mapped' => false,
        'choices' => array(
            4 => 'fields.month.eta',
            2 => 'fields.month.TM'
        ),
        'label' => 'fields.month.typeEmployee',
        'translation_domain' => 'Admin'));

    $builder->add('TM', 'entity', array(
        'multiple' => false,
        'em' => 'default',
        'class' => 'NitraSchemaCDBBundle:Employee',
        'required' => false,
        'label' => 'fields.monthPlan.tm',
        'translation_domain' => 'Admin',
        'query_builder' => function(EntityRepository $er){
            return $er->createQueryBuilder('u')
                ->andWhere('u.lvl = :lvl')
                ->setParameter('lvl',2)
                ->addOrderBy('u.sureName','ASC');
        },
        'property' =>'name',
    ));

创建表单的完整代码。有一个父类EditType,里面是根据yml文件生成的。我继承并覆盖它buildForm

namespace Admingenerated\NitraEmployeeBundle\Form\BaseMonthPlanType;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use JMS\SecurityExtraBundle\Security\Authorization\Expression\Expression;

class EditType extends AbstractType
{
    protected $securityContext;

    public function buildForm(FormBuilderInterface $builder, array $options)
    {

        $formOptions = $this->getFormOption('year', array(  'required' => true,  'mapped' => true,  'label' => 'fields.target.choseYear',  'translation_domain' => 'Admin',));
        $builder->add('year', new \Nitra\CommonBundle\Form\YearType(), $formOptions);


        $formOptions = $this->getFormOption('month', array(  'required' => true,  'mapped' => true,  'label' => 'fields.target.choseMonth',  'translation_domain' => 'Admin',));
        $builder->add('month', new \Nitra\CommonBundle\Form\MonthType(), $formOptions);


        $formOptions = $this->getFormOption('target', array(  'multiple' => false,  'em' => 'default',  'class' => 'Nitra\\SchemaCDBBundle\\Entity\\Target',  'required' => false,  'label' => 'fields.monthPlan.target',  'translation_domain' => 'Admin',));
        $builder->add('target', 'entity', $formOptions);


        $formOptions = $this->getFormOption('employee', array(  'multiple' => false,  'em' => 'default',  'class' => 'Nitra\\SchemaCDBBundle\\Entity\\Employee',  'required' => true,  'label' => 'fields.monthPlan.employee',  'translation_domain' => 'Admin',));
        $builder->add('employee', 'entity', $formOptions);


        $formOptions = $this->getFormOption('summ', array(  'precision' => 12,  'required' => true,  'label' => 'fields.monthPlan.summ',  'translation_domain' => 'Admin',));
        $builder->add('summ', 'number', $formOptions);


    }

    protected function getFormOption($name, array $formOptions)
    {
        return $formOptions;
    }

    public function getName()
    {
        return 'edit_monthplan';
    }

    public function setSecurityContext($securityContext)
    {
        $this->securityContext = $securityContext;
    }

}

我的自定义表单构建类。

namespace Nitra\EmployeeBundle\Form\Type\MonthPlan;

use Admingenerated\NitraEmployeeBundle\Form\BaseMonthPlanType\EditType as BaseEditType;
use Nitra\SchemaCDBBundle\Repository\EmployeeRepository;
use Symfony\Component\Form\FormBuilderInterface;
use Doctrine\ORM\EntityRepository;
use Nitra\SchemaCDBBundle\Entity\Employee;
use Nitra\CommonBundle\Form\MonthType;
use Nitra\CommonBundle\Form\YearType;

class EditType extends BaseEditType {

    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        parent::buildForm($builder, $options);

        $builder->add('chosenType', 'choice', array(
            'required' => true,
            'mapped' => false,
            'choices' => array(
                4 => 'fields.month.eta',
                2 => 'fields.month.TM'
            ),
            'label' => 'fields.month.typeEmployee',
            'translation_domain' => 'Admin'));

        $builder->add('TM', 'entity', array(
            'multiple' => false,
            'em' => 'default',
            'class' => 'NitraSchemaCDBBundle:Employee',
            'required' => false,
            'label' => 'fields.monthPlan.tm',
            'translation_domain' => 'Admin',
            'query_builder' => function(EntityRepository $er){
                return $er->createQueryBuilder('u')
                    ->andWhere('u.lvl = :lvl')
                    ->setParameter('lvl',2)
                    ->addOrderBy('u.sureName','ASC');
            },
            'property' =>'name',
        ));
    }

    protected function getFormOption($name, array $formOptions) {
        switch ($name) {
            case 'employee':
                $formOptions['query_builder'] = function (EmployeeRepository $er) {
                            return $er->getEmployeeAtPositionEta($this->securityContext->getToken()->getUser());
                        };
                break;
        }
        return $formOptions;
    }

}
symfony2
  • 1 1 个回答
  • 10 Views

1 个回答

  • Voted
  1. Best Answer
    Jurij Jazdanov
    2020-04-04T17:44:00Z2020-04-04T17:44:00Z

    我怀疑您在创建表单时在控制器中提交了一个实体

    $monthPlan = new MonthPlan();
    $this->createForm(MonthPlanType::class, $monthPlan)
    

    在这种情况下,Symfony 将自动使用$monthPlan表单字段填充实体。Symfony 认为在这种形式中,所有表单字段都与实体字段相关联,在您的情况下,当它在表单中看到一个字段但它不存在于实体中时,它会抱怨。也就是说,在发送表单后的后台,会发生一些事情$monthPlan->setTM($value);,会报错。

    要避免此行为,请将该字段设置为'mapped' => false。这有点像表单字段的标志,表示它与提交的实体没有关联,不会处理$monthPlan->setTM($value);

    您可以创建没有实体的表单,$this->createForm(MonthPlanType::class, array())并在提交后以您喜欢的任何方式处理数据。

    如果需要此行为,则将表单中的字段命名为 emplyee 或在实体中创建一个 TM 字段。

    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    Python 3.6 - 安装 MySQL (Windows)

    • 1 个回答
  • Marko Smith

    C++ 编写程序“计算单个岛屿”。填充一个二维数组 12x12 0 和 1

    • 2 个回答
  • Marko Smith

    返回指针的函数

    • 1 个回答
  • Marko Smith

    我使用 django 管理面板添加图像,但它没有显示

    • 1 个回答
  • Marko Smith

    这些条目是什么意思,它们的完整等效项是什么样的

    • 2 个回答
  • Marko Smith

    浏览器仍然缓存文件数据

    • 1 个回答
  • Marko Smith

    在 Excel VBA 中激活工作表的问题

    • 3 个回答
  • Marko Smith

    为什么内置类型中包含复数而小数不包含?

    • 2 个回答
  • Marko Smith

    获得唯一途径

    • 3 个回答
  • Marko Smith

    告诉我一个像幻灯片一样创建滚动的库

    • 1 个回答
  • Martin Hope
    Air 究竟是什么标识了网站访问者? 2020-11-03 15:49:20 +0000 UTC
  • Martin Hope
    Алексей Шиманский 如何以及通过什么方式来查找 Javascript 代码中的错误? 2020-08-03 00:21:37 +0000 UTC
  • Martin Hope
    Qwertiy 号码显示 9223372036854775807 2020-07-11 18:16:49 +0000 UTC
  • Martin Hope
    user216109 如何为黑客设下陷阱,或充分击退攻击? 2020-05-10 02:22:52 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5