Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2.8 file uploading "Cannot read index "..." from object of type because Entity doesn't implement ArrayAcces."

I've got a problem with my editAction, when I'm trying to edit some proudct i'm getting

Cannot read index "marka" from object of type "ShoeShopBundle\Entity\Buty" because it doesn't implement \ArrayAccess.

Earlier I was getting

The form's view data is expected to be an instance of class Symfony\Component\HttpFoundation\File\File, but is a(n) string. You can avoid this error by setting the "data_class" option to null or by adding a view transformer that transforms a(n) string to an instance of Symfony\Component\HttpFoundation\File\File.

error, so I changed data_class in configureOptions to null and added 'data_class' => 'path_to_entity' to every field. This is my form:

class ButyType extends AbstractType
{
/**
 * @param FormBuilderInterface $builder
 * @param array $options
 */
public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('marka', ChoiceType::class, array(
            'data_class' => 'ShoeShopBundle\Entity\Buty',
            'choices' => ['Adidas', 'Asics', 'Nike', 'Puma'
            ]
        ))
        ->add('model', TextareaType::class, array(
            'data_class' => 'ShoeShopBundle\Entity\Buty',
        ))
        ->add('kolor', TextareaType::class, array(
            'data_class' => 'ShoeShopBundle\Entity\Buty',
        ))
        ->add('cena', TextareaType::class, array(
            'data_class' => 'ShoeShopBundle\Entity\Buty',
        ))
        ->add('rozmiar', EntityType::class, array(
                'class' => 'ShoeShopBundle:Rozmiar',
                'expanded' => true,
                'multiple' => true,
                'query_builder' => function (EntityRepository $er) {
                    return $er->createQueryBuilder('r')
                        ->orderBy('r.rozmiar', 'ASC');
                }, 'choice_label' => 'rozmiar',
            )
        )
        ->add('zdjecie', FileType::class, array(
            'data_class' => 'Symfony\Component\Form\Extension\Core\Type\FileType',
            'property_path' => 'zdjecie',
            'label' => 'Zdjecie (img file)'))
        ->add('zdjecieMIN', FileType::class, array(
            'data_class'=> 'Symfony\Component\Form\Extension\Core\Type\FileType',
            'property_path' => 'zdjecieMIN',
            'label' => 'Zdjecie miniatura (img file)'));
}

/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => null
    ));
}

and this is my Entity

/**
 * Buty
 *
 * @ORM\Table(name="buty")
 * @ORM\Entity(repositoryClass="ShoeShopBundle\Repository\ButyRepository")
 */
class Buty
{
/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="marka", type="string", length=255)
 */
private $marka;

/**
 * @var string
 *
 * @ORM\Column(name="model", type="string", length=255)
 */
private $model;

/**
 * @var string
 *
 * @ORM\Column(name="kolor", type="string", length=255)
 */
private $kolor;

/**
 * @var int
 *
 * @ORM\Column(name="cena", type="float")
 */
private $cena;

/**
 * @var ArrayCollection
 * @ORM\ManyToMany(targetEntity="Rozmiar")
 * @ORM\JoinTable(
 *     name="buty__rozmiary",
 *     joinColumns={@ORM\JoinColumn(name="buty_id", referencedColumnName="id")},
 *     inverseJoinColumns={@ORM\JoinColumn(name="rozmiar_id", referencedColumnName="id")}
 * )
 */
private $rozmiar;

/**
 * @ORM\Column(type="string")
 *
 * @Assert\NotBlank(message="Dodaj zdjecie miniaturke")
 * @Assert\File(mimeTypes={"image/png", "image/jpeg", "image/jpg",})
 */
private $zdjecieMIN;

public function getZdjecieMIN()
{
    return $this->zdjecieMIN;
}

public function setZdjecieMIN($zdjecieMIN)
{
    $this->zdjecieMIN = $zdjecieMIN;

    return $this;
}

/**
 * @ORM\Column(type="string")
 *
 * @Assert\NotBlank(message="Dodaj zdjecie")
 * @Assert\File(mimeTypes={"image/png", "image/jpeg", "image/jpg",})
 */
private $zdjecie;

public function getZdjecie()
{
    return $this->zdjecie;
}

public function setZdjecie($zdjecie)
{
    $this->zdjecie = $zdjecie;

    return $this;
}

/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}


/**
 * Set marka
 *
 * @param string $marka
 * @return Buty
 */
public function setMarka($marka)
{
    $this->marka = $marka;

}

/**
 * Get marka
 *
 * @return string 
 */
public function getMarka()
{
    return $this->marka;
}

/**
 * Set model
 *
 * @param string $model
 * @return Buty
 */
public function setModel($model)
{
    $this->model = $model;

}

/**
 * Get model
 *
 * @return string 
 */
public function getModel()
{
    return $this->model;
}

/**
 * Set kolor
 *
 * @param string $kolor
 * @return Buty
 */
public function setKolor($kolor)
{
    $this->kolor = $kolor;

}

/**
 * Get kolor
 *
 * @return string 
 */
public function getKolor()
{
    return $this->kolor;
}

/**
 * Set cena
 *
 * @param integer $cena
 * @return Buty
 */
public function setCena($cena)
{
    $this->cena = $cena;

}

/**
 * Get cena
 *
 * @return integer 
 */
public function getCena()
{
    return $this->cena;
}

/**
 * Set rozmiar
 *
 * @param ArrayCollection $rozmiar
 * @return rozmiar
 */
public function setRozmiar($rozmiar)
{
    $this->rozmiar = $rozmiar;
    return $this;

}

/**
 * @return ArrayCollection
 */
public function getRozmiar()
{
    return $this->rozmiar;
}


public function __construct()
{
    $this->rozmiar = new ArrayCollection();
}
}

Can anyone please tell me what's wrong? Thanks in advance.

like image 779
PawełP Avatar asked Oct 20 '16 12:10

PawełP


1 Answers

You have to set the 'data_class' option of your form the the appropriate class otherwise the Symfony property accessor assumes it's an array.

/**
 * @param OptionsResolver $resolver
 */
public function configureOptions(OptionsResolver $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => Buty::class
    ));
}
like image 176
TFennis Avatar answered Sep 28 '22 19:09

TFennis