Str*_*rko 4 symfony-forms symfony
我在这里问了一个问题如何在FormType中使用Repository自定义函数但是没有人使用,所以我做了一点挖掘并提前了一点但我仍然得到这个错误:
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
Run Code Online (Sandbox Code Playgroud)
现在这是我的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';
}
}
Run Code Online (Sandbox Code Playgroud)
我从CategoryType访问CategoryRepository函数findByParenting($ parent),我从getAllChildren函数($ catID)中获取了准确数据的数组,但错误就在那里,我认为Symfony框架正在期待一个实体字段选择领域,但不知道如何解决它.我还改变了控制器中的formCreate调用,将$ this-> getDoctrine()作为CategoryType()的参数:
$form = $this->createForm(new CategoryType($this->getDoctrine()), $cat, array('catID' => $id));
Run Code Online (Sandbox Code Playgroud)
好吧,我设法解决了这个难题.答案很简单我所要做的就是改变
$builder->add('parent', 'choice', array(
'choices' => $this->getAllChildren($catID),
'required' => false,
'attr' => array('data-placeholder' => '--Izaberite Opciju--'),
));
Run Code Online (Sandbox Code Playgroud)
对此:
$builder->add('parent', 'entity', array(
'class' => 'KprCentarZdravljaBundle:Category',
'choices' => $this->getAllChildren($catID),
'property' => 'name',
'required' => false,
'attr' => array('data-placeholder' => '--Izaberite Opciju--'),
));
Run Code Online (Sandbox Code Playgroud)
并更改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;
}
Run Code Online (Sandbox Code Playgroud)
我改成了:
private function getAllChildren($catID)
{
$children = $this->doctrine->getRepository('KprCentarZdravljaBundle:Category')->findByParenting($catID)
return $children;
}
Run Code Online (Sandbox Code Playgroud)
非常感谢用户redbirdo指出实体字段上的选项选项.
| 归档时间: |
|
| 查看次数: |
9060 次 |
| 最近记录: |