Symfony 类型“object”的值不能转换为有效的数组键


Symfony The value of type "object" cannot be converted to a valid array key

我试图从实体创建一个表单,但遇到了一些麻烦。

这是我的表单类型

namespace AppBundle'Form'Type;
use Symfony'Component'Form'AbstractType;
use Symfony'Component'Form'FormBuilderInterface;
class EditPhotoFormType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add('title', 'text');
    $builder->add('description', 'textarea');
        $builder->add('category', 'choice', array(
            'choices' => array(
                '1' => 'categories.uncategorized',
                'categories.photography.photography'   => array(
                    '5' => 'categories.photography.abstract',
                    '6' => 'categories.photography.animals',
                    '7' => 'categories.photography.blackandwhite',
                    '8' => 'categories.photography.architecture',
                    '9' => 'categories.photography.fashion',
                    '10' => 'categories.photography.food',
                    '11' => 'categories.photography.landscapes',
                    '12' => 'categories.photography.macro', 
                    '13' => 'categories.photography.people',
                    '14' => 'categories.photography.sport'
                ),
                'categories.painting.painting' => array(
                    '15' => 'categories.painting.landscapes',
                    '16' => 'categories.painting.seascapes',
                    '17' => 'categories.painting.portrait',
                    '18' => 'categories.painting.stilllife',
                    '19' => 'categories.painting.architecture',
                    '20' => 'categories.painting.fantasy'
                 ),
                'categories.3d.3d'   => array(
                    '21' => 'categories.3d.interiors',
                    '22' => 'categories.3d.exteriors',
                    '23' => 'categories.3d.creative'
                ),
            ),
        ));
    }
    public function getName()
    {
        return 'app_photo_edit';
    }
}

这是我的控制器。

$em = $this->getDoctrine()->getManager();
    $photo = $em->getRepository('AppBundle:Photo')->find($photoId);
    $form = $this->createForm('app_photo_edit', $photo);
    return $this->render('AdminBundle::edit.html.twig', array('form' => $form->createView(), 'photo' => $photo));

照片实体

<?php
namespace AppBundle'Entity;
use Doctrine'ORM'Mapping as ORM;
use Symfony'Component'Validator'Constraints as Assert;
/**
 * @ORM'Entity
 * @ORM'Table(name="photos")
 */
class Photo
{
    const
            CATEGORY_PHOTOGRAPHY = 1,
            CATEGORY_PAINTING = 2,
            CATEGORY_3D = 3;
    /*
     * Flow photos limit
     */
    const FLOW_PHOTOS_LIMIT = 15;
    /**
     * @ORM'Id
     * @ORM'Column(type="integer")
     * @ORM'GeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @ORM'ManyToOne(targetEntity="User", inversedBy="photos")
     */
    protected $user;
    /**
     * @ORM'Column(type="text", nullable=true)
     */
    protected $title;
    /**
     * @ORM'Column(type="text", nullable=true)
     */
    protected $description;
    /**
     * @ORM'Column(type="text")
     */
    protected $name;
    /**
     * @ORM'ManyToOne(targetEntity="PhotoCategory")
     */
    protected $category;
    /**
     * @ORM'Column(name="creation_date", type="datetime")
     */
    protected $creationDate;
    /**
     * @ORM'Column(name="edit_date", type="datetime", nullable=true)
     */
    protected $editDate;
    /**
     * @ORM'Column(name="is_moderated", type="boolean")
     */
    protected $isModerated = false;
    /**
     * @ORM'Column(name="moderation_date", type="datetime", nullable=true)
     */
    protected $moderationDate;
    /**
     * @ORM'Column(name="is_active", type="boolean")
     */
    protected $isActive = true;
    /**
     * @ORM'OneToMany(targetEntity="Comment", mappedBy="photo")
     * @ORM'OrderBy({"id" = "DESC"})
     */
    protected $comments;
    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Set user
     *
     * @param User $user
     *
     * @return Photo
     */
    public function setUser($user)
    {
        $this->user = $user;
        return $this;
    }
    /**
     * Get user
     *
     * @return User $user
     */
    public function getUser()
    {
        return $this->user;
    }
    /**
     * Get category
     *
     * @return Category $category
     */
    public function getCategory()
    {
        return $this->category;
    }
    /**
     * Set title
     *
     * @param string $title
     *
     * @return Photo
     */
    public function setTitle($title)
    {
        $this->title = $title;
        return $this;
    }
    /**
     * Get title
     *
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }
    /**
     * Get creationDate
     *
     * @return 'DateTime
     */
    public function getCreationDate()
    {
        return $this->creationDate;
    }
    /**
     * Get editDate
     *
     * @return 'DateTime
     */
    public function getEditDate()
    {
        return $this->editDate;
    }
    /**
     * Get moderationDate
     *
     * @return 'DateTime
     */
    public function getModerationDate()
    {
        return $this->moderationDate;
    }
    /**
     * Get is active
     *
     * @return integer
     */
    public function isActive()
    {
        return $this->isActive;
    }
    /**
     * Get is moderated
     *
     * @return integer
     */
    public function isModerated()
    {
        return $this->isModerated;
    }
    /*
     * Get image
     * 
     * @return string
     */
    public function getImage()
    {
        return $this->getWebDirectory().$this->getName();
    }
    /*
     * Get image directory 
     * 
     * @return string
     */
    public function getDirectory()
    {
        return __DIR__.'/../../../web/uploads/photos/'.$this->getUser()->getId().'/'.$this->creationDate->format('Y-m-d').'/';
    }
    /*
     * Get image web directory
     * 
     * @return string
     */
    public function getWebDirectory()
    {
        return '/web/uploads/photos/'.$this->getUser()->getId().'/'.$this->creationDate->format('Y-m-d').'/';
    }
    /*
     * Get comments
     */
    public function getComments()
    {
        return $this->comments;
    }
    /**
     * Set description
     *
     * @param string $description
     *
     * @return Photo
     */
    public function setDescription($description)
    {
        $this->description = $description;
        return $this;
    }
    /**
     * Get description
     *
     * @return string
     */
    public function getDescription()
    {
        return $this->description;
    }
    /**
     * Set name
     *
     * @param string $name
     *
     * @return Photo
     */
    public function setName($name)
    {
        $this->name = $name;
        return $this;
    }
    /**
     * Get name
     *
     * @return string
     */
    public function getName()
    {
        return $this->name;
    }
    /**
     * Set creationDate
     *
     * @param 'DateTime $creationDate
     */
    public function setCreationDate('DateTime $creationDate)
    {
        $this->creationDate = $creationDate;
    }
    /**
     * Set editDate
     *
     * @param 'DateTime $editDate
     */
    public function setEditDate('DateTime $editDate)
    {
        $this->editDate = $editDate;
    }
    /**
     * Set moderationDate
     *
     * @param 'DateTime $moderationDate
     */
    public function setModerationDate('DateTime $moderationDate)
    {
        $this->moderationDate = $moderationDate;
    }
    /**
     * Set active
     */
    public function setActive($active)
    {
        $this->isActive = $active;
    }
    /**
     * Set category
     */
    public function setCategory($category)
    {
        $this->category = $category;
    }
    /**
     * Set moderated
     */
    public function setModerated($moderated)
    {
        $this->isModerated = $moderated;
    }
    /*
     * Delete image (Used when didnt pass moderation)
     */
    public function deleteImage() {
    return unlink($this->getDirectory().$this->getName());
    }
}

我收到错误:"对象"类型的值无法转换为有效的数组键。

问题出在 FormType 类别中,但我不知道如何正确操作。

谢谢

我相信这个问题是由嵌套数组引起的:

'categories.photography.photography'   => array(...)

如果你把简单的键=>值数组,它应该可以工作。例:

$builder->add('type', 'choice', 
array(
'label'=>'My own label', 
'choices'=>array('verb'=>'Displayed verb', 'noun'=>'Displayed noun', 'adjective'=>'Displayed adjective', 'adverb'=>'Displayed adverb')));

对于您的表单,我建议创建 3 个单独的选择字段。

  • 您不能在选择类别中定义 1 个键数组的 2 个值
  • 索引键数组错误
  • 不能使用此代码在数组中插入数组
  • 你的浸润对象太奇怪
  • 您的制表代码是随机的或随机的
  • 2 生成器->add() 在 1 窗体类型中没有必要

解决代码中的所有这些问题,代码运行得更好。