Search code examples
phpsymfonydoctrine-ormsonata-adminsonata

Symfony3 Failed to create object


I using Sonata Admin bundle. I get error: Failed to create object: PizzaBundle\Entity\Promotion. I did a Promotion entity:

/**
 * @ORM\Entity
 * @ORM\Table(name="promotion")
 */
class Promotion {

    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=100)
     */
    private $title;

    /**
     * @ORM\Column(type="string")
     */
    private $description;



    /**
     * @ORM\Column(type="blob")
     */
    private $image;

    /**
     * @ORM\Column(type="date")
     */
    private $dataStart;

    /**
     * @ORM\Column(type="date")
     */
    private $dataEnd;

And PromotionAdmin.php

public function configureFormFields(FormMapper $formMapper) {
        $formMapper
                ->add('title', 'text')
                ->add('description', 'text')
                ->end()
                ->with('Zdjęcie')
                    ->add('image', 'file', ['required' => false]) 
                    ->add('dataStart', 'date')
                    ->add('dataEnd', 'date')
                ->end();
    }

    protected function configureDatagridFilters(DatagridMapper $datagridMapper)
    {
        $datagridMapper
                ->add('title')
                ->add('description')
                ->add('image')
                ->add('dataStart')
                ->add('dataEnd')
                ;
    }

    protected function configureListFields(ListMapper $listMapper)
    {
        $listMapper
                ->addIdentifier('title')
                ->addIdentifier('description')
                ->addIdentifier('image')
                ->addIdentifier('dataStart')
                ->addIdentifier('dataEnd')
                ;
    }



}

My services.yml

admin.promotion:
        class: PizzaBundle\Admin\PromotionAdmin
        arguments: [~, PizzaBundle\Entity\Promotion, ~]
        tags:
            - { name: sonata.admin, manager_type: orm, label: Promotion }

I don't have a problem with my code. I think that this is problem with type variable Image in entity. If this is error in type blob, please help resolve problem.


Solution

  • You are trying to insert NULL into the "image" column, which is not nullable it seems, which results in an MySQL error.

    /**
     * @ORM\Entity
     * @ORM\Table(name="promotion")
     */
    class Promotion 
    {
         /**
          * @ORM\Column(type="blob", nullable=true)
          */
         private $image;
    }