在表单中添加图像类型:“无法读取索引...不实现 \ArrayAccess“?
我在表单字段中的自定义类型中有一个小问题:我正在尝试添加与“图像”实体相关的“ImageType”,该实体具有“url”和“alt”作为变量。
我收到此错误:
无法从类型为“Proxies__CG__\OC\PlatformBundle\Entity\Image”的对象读取索引“url”,因为它没有实现 \ArrayAccess。
下面是类型:
<?php
// src/OC/PlatformBundle/Form/ImageType.php
namespace OC\PlatformBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\Extension\Core\Type\TextType;
class ImageType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('url', TextType::class)
->add('alt', TextType::class)
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'OC\PlatformBundle\Entity\Image'
));
}
public function getName()
{
return 'oc_platformbundle_image';
}
}
形式:
class AdvertType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class)
->add('date', DateType::class)
->add('categories', EntityType::class, array(
'class' => 'OCPlatformBundle:Category',
'choice_label' => 'name',
'multiple' => true
))
->add('image', ImageType::class)
->add('save', SubmitType::class, array('label' => 'Create Task')) ;
}
实体:
<?php
namespace OC\PlatformBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Image
*
* @ORM\Table(name="image")
* @ORM\Entity(repositoryClass="OC\PlatformBundle\Repository\ImageRepository")
*/
class Image
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="url", type="string", length=255)
*/
private $url;
/**
* @var string
*
* @ORM\Column(name="alt", type="string", length=255)
*/
private $alt;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set url
*
* @param string $url
*
* @return Image
*/
public function setUrl($url)
{
$this->url = $url;
return $this;
}
/**
* Get url
*
* @return string
*/
public function getUrl()
{
return $this->url;
}
/**
* Set alt
*
* @param string $alt
*
* @return Image
*/
public function setAlt($alt)
{
$this->alt = $alt;
return $this;
}
/**
* Get alt
*
* @return string
*/
public function getAlt()
{
return $this->alt;
}
}
我在 Image 类中没有任何 Array,所以我不确定是什么引发了这个问题。