Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add an ImageType inside a form: "cannot read index ... doesn't implement \ArrayAccess"?

Tags:

I have a small issue with a custom Type in a field of a form : I am trying to add an "ImageType" related to an "Image" entity, which has "url" and "alt" as variables.
I get this error :

Cannot read index "url" from object of type "Proxies__CG__\OC\PlatformBundle\Entity\Image" because it doesn't implement \ArrayAccess.

Here is the Type:

<?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';   } } 

The form:

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'))    ;   } 

And the entity :

<?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;     } } 

I don't have any Array in the Image class, so I am not sure what throws the issue.

like image 218
Paul Avatar asked Jan 04 '16 16:01

Paul


1 Answers

You are receiving this error because Symfony is looking for the data class of your form type but cannot find it. Your code would have worked for Symfony 2.x versions, where setDefaultOptions() was valid. Beginning with Symfony 2.7, that method was deprecated in favor of configureOptions() and removed entirely in 3.0.

So, your function exists in your form class but it will never be referenced by Symfony. Your debug toolbar may not even show this deprecation since it was removed entirely in 3.0. Simpler form types may not error if you make this same mistake, but setDefaultOptions() will never be called in either case, so I would go through and check your other forms to make sure they are valid as well.

As a solution, change this:

use Symfony\Component\OptionsResolver\OptionsResolverInterface;  class AdvertType extends AbstractType {     public function setDefaultOptions(OptionsResolverInterface $resolver)     {         $resolver->setDefaults(array(             'data_class' => 'OC\PlatformBundle\Entity\Image',         ));     } } 

to this:

use Symfony\Component\OptionsResolver\OptionsResolver;  class AdvertType extends AbstractType {     public function configureOptions(OptionsResolver $resolver)     {         $resolver->setDefaults(array(             'data_class' => 'OC\PlatformBundle\Entity\Image',         ));     } } 
like image 99
Jason Roman Avatar answered Sep 28 '22 07:09

Jason Roman