Symfony2“类的对象无法转换为int”

时间:2022-10-22 18:32:02

So i have a script that works well for my creation. It populates a select box with geckos that match the current user's id. Works great, however, when i go to edit an entry in the database i get the following error:

所以我有一个适合我创作的脚本。它使用与当前用户的id匹配的geckos填充选择框。工作得很好,但是,当我去编辑数据库中的条目时,我收到以下错误:

Notice: Object of class Proxies__CG__\Breedr\GeckoBundle\Entity\Gecko could not be converted to int

注意:类Proxies__CG __ \ Breedr \ GeckoBundle \ Entity \ Gecko的对象无法转换为int

The code for my createCreateForm is this (this is the bit that works fine):

我的createCreateForm的代码是这个(这是可行的一点):

private function createCreateForm(Weight $entity)
    {   
        $currentUserId = $this->getUser()->getId();
        $sql = 'select id, name from gecko where user_id = ?';        
        $stmt = $this->getDoctrine()->getManager()->getConnection()->prepare($sql); 
        $stmt->execute(array($currentUserId)); 
        $results = $stmt->fetchAll(\PDO::FETCH_ASSOC);

        $form = $this->createForm(new WeightType($results), $entity, array(
            'action' => $this->generateUrl('weight_create'),
            'method' => 'POST',
        ));

        $form->add('submit', 'submit', array('label' => 'Create'));

        // dump($results);
        // exit();
        return $form;
    }

And after i got other errors, i thought that copying to my createEditForm would fix my issue:

在我遇到其他错误之后,我认为复制到我的createEditForm会解决我的问题:

private function createEditForm(Weight $entity)
{
    $currentUserId = $this->getUser()->getId();
    $sql = 'select id, name from gecko where user_id = ?';        
    $stmt = $this->getDoctrine()->getManager()->getConnection()->prepare($sql); 
    $stmt->execute(array($currentUserId)); 
    $results = $stmt->fetchAll(\PDO::FETCH_ASSOC);

    $form = $this->createForm(new WeightType($results), $entity, array(
        'action' => $this->generateUrl('weight_update', array('id' => $entity->getId())),
        'method' => 'PUT',
    ));

    $form->add('submit', 'submit', array('label' => 'Update'));

    return $form;
}

But it didn't and now i get the error i quoted above. Below is my WeightType form so you can see how it's working:

但它没有,现在我得到上面引用的错误。下面是我的WeightType表单,以便您可以看到它是如何工作的:

<?php

namespace Breedr\GeckoBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class WeightType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */

    /**
     * @var array
     */
    protected $geckosList;

    /**
     * param array $geckosList It can be passed from controller 
     * when creating form instance
     */
    public function __construct($geckosList)
    {
        $this->geckosList = $geckosList;
    }

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

        foreach ($this->geckosList as $entry) {
            $geckosChoice[ $entry['id'] ] = $entry['name'];
        }

        $builder
            ->add('weighedDate')
            ->add('weight')
            ->add('geckoId', 'choice', array(
            'choices' => $geckosChoice  
        ));
    }

    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Breedr\GeckoBundle\Entity\Weight'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'breedr_geckobundle_weight';
    }
}

This is for my editAction:

这是我的editAction:

public function editAction($id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('BreedrGeckoBundle:Weight')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Weight entity.');
    }

    $editForm = $this->createEditForm($entity);
    $deleteForm = $this->createDeleteForm($id);

    return array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    );
}

So my question is, how can i overcome this error and show the current gecko's value in the drop down (the point is that if they chose the wrong gecko they could easily change the name, but my logic behind this hasn't worked at all!)

所以我的问题是,我怎样才能克服这个错误并在下拉中显示当前壁虎的价值(重点是,如果他们选择了错误的壁虎,他们可以轻松更改名称,但我背后的逻辑根本没有用到!)

Any help is welcomed, and i thank you in advance to any that can help me

欢迎任何帮助,我提前感谢任何可以帮助我的人

1 个解决方案

#1


I would like to suggest to use Doctrine query builder inside the form, and also the entity form field. You code can be something like this:

我想建议在表单中使用Doctrine查询构建器,以及实体表单字段。您的代码可以是这样的:

Controller

private function createEditForm(Weight $entity)
{
    $currentUser = $this->getUser();

    $form = $this->createForm(new WeightType($currentUser), $entity, array(
        'action' => $this->generateUrl('weight_update', array('id' => $entity->getId())),
        'method' => 'PUT',
    ));

    $form->add('submit', 'submit', array('label' => 'Update'));

    return $form;
}

Form

    /**
     * @var User
     */
    protected $currentUser;

    /**
     * param User $currentUser
     */
    public function __construct(User $currentUser)
    {
        $this->currentUser = $currentUser;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $currentUser = $this->currentUser;

        $builder
            ->add('weighedDate')
            ->add('weight')
        ;
        $builder->add('gecko', 'entity', array(
            'class' => 'Breedr\GeckoBundle\Entity\Gecko',
            'property' => 'name',
            'query_builder' => function(EntityRepository $er) use ($currentUser) {
                    return $er->createQueryBuilder('g')
                        ->where('g.user = :currentUser')
                        ->orderBy('g.name ASC')
                        ->setParameter('currentUser', $currentUser);
                },
        ));
    }

The doc's reference.

该文件的参考。

#1


I would like to suggest to use Doctrine query builder inside the form, and also the entity form field. You code can be something like this:

我想建议在表单中使用Doctrine查询构建器,以及实体表单字段。您的代码可以是这样的:

Controller

private function createEditForm(Weight $entity)
{
    $currentUser = $this->getUser();

    $form = $this->createForm(new WeightType($currentUser), $entity, array(
        'action' => $this->generateUrl('weight_update', array('id' => $entity->getId())),
        'method' => 'PUT',
    ));

    $form->add('submit', 'submit', array('label' => 'Update'));

    return $form;
}

Form

    /**
     * @var User
     */
    protected $currentUser;

    /**
     * param User $currentUser
     */
    public function __construct(User $currentUser)
    {
        $this->currentUser = $currentUser;
    }

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $currentUser = $this->currentUser;

        $builder
            ->add('weighedDate')
            ->add('weight')
        ;
        $builder->add('gecko', 'entity', array(
            'class' => 'Breedr\GeckoBundle\Entity\Gecko',
            'property' => 'name',
            'query_builder' => function(EntityRepository $er) use ($currentUser) {
                    return $er->createQueryBuilder('g')
                        ->where('g.user = :currentUser')
                        ->orderBy('g.name ASC')
                        ->setParameter('currentUser', $currentUser);
                },
        ));
    }

The doc's reference.

该文件的参考。