gpt4 book ai didi

Symfony2 选择字段不起作用

转载 作者:行者123 更新时间:2023-12-04 22:47:54 25 4
gpt4 key购买 nike

我在这里问了一个问题 How to use Repository custom functions in a FormType但是没有人回答,所以我做了一些挖掘并进行了一些改进,但我仍然收到此错误:

Notice: Object of class Proxies\__CG__\Kpr\CentarZdravljaBundle\Entity\Category 
could not be converted to int in /home/kprhr/public_html/CZ_Symfony/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php line 457

现在这是我的 CategoryType 的样子:
<?php

namespace Kpr\CentarZdravljaBundle\Form\Type;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Bridge\Doctrine\RegistryInterface;

class CategoryType extends AbstractType
{
private $doctrine;

public function __construct(RegistryInterface $doctrine)
{
$this->doctrine = $doctrine;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Kpr\CentarZdravljaBundle\Entity\Category',
'catID' => null,
));
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$someId = $builder->getData()->getId();
$param = ($someId) ? $someId : 0;
$catID = $options['catID'];
$builder->add('name', 'text', array('attr' => array('class' => 'span6')));
$builder->add('file', 'file', array('image_path' => 'webPath', 'required' => false));
$builder->add('parent', 'choice', array(
'choices' => $this->getAllChildren($catID),
'required' => false,
'attr' => array('data-placeholder' => '--Izaberite Opciju--'),
));
$builder->add('tags', 'tag_selector', array(
'required' => false,
));
$builder->add('status', 'choice', array(
'choices' => array('1' => 'Aktivna', '0' => 'Neaktivna'),
'required' => true,
));
$builder->add('queue', 'text', array('attr' => array('class' => 'span3')));
}
private function getAllChildren($catID)
{
$choices = array();
$children = $this->doctrine->getRepository('KprCentarZdravljaBundle:Category')->findByParenting($catID);

foreach ($children as $child) {
$choices[$child->getId()] = $child->getName();
}

return $choices;
}

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

}

我正在从 CategoryType 访问 CategoryRepository 函数 findByParenting($parent) 并且我正在从函数 getAllChildren($catID) 获取填充有准确数据的数组,但是错误在那里,我认为 Symfony 框架期望的是实体字段选择字段,但不知道如何修复它。
我还更改了 Controller 中的 formCreate 调用,将 $this->getDoctrine() 作为 CategoryType() 的参数:
$form = $this->createForm(new CategoryType($this->getDoctrine()), $cat, array('catID' => $id));

最佳答案

好的,我设法解决了这个难题。答案很简单,我所要做的就是改变

$builder->add('parent', 'choice', array(
'choices' => $this->getAllChildren($catID),
'required' => false,
'attr' => array('data-placeholder' => '--Izaberite Opciju--'),
));

对此:
 $builder->add('parent', 'entity', array(
'class' => 'KprCentarZdravljaBundle:Category',
'choices' => $this->getAllChildren($catID),
'property' => 'name',
'required' => false,
'attr' => array('data-placeholder' => '--Izaberite Opciju--'),
));

并更改 getAllChildren(..) 函数,使其返回对象
private function getAllChildren($catID)
{
$choices = array();
$children = $this->doctrine->getRepository('KprCentarZdravljaBundle:Category')->findByParenting($catID);

foreach ($children as $child) {
$choices[$child->getId()] = $child->getName();
}

return $choices;
}

我把它改成:
private function getAllChildren($catID)
{
$children = $this->doctrine->getRepository('KprCentarZdravljaBundle:Category')->findByParenting($catID)

return $children;
}

非常感谢用户 redbirdo用于指出实体字段上的选择选项。

关于Symfony2 选择字段不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16463926/

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