0

i trying to make admin page for Product which has relationship 1:1 with image.

Product

/**
 * @ORM\Entity
 * @ORM\Table(name="products")
    class Product
    {

        /**
         * @ORM\Column(type="integer")
         * @ORM\GeneratedValue
         * @ORM\Id
         * @var int
         */
         private $id = 0;

        /**
         * @ORM\OneToOne(targetEntity="Image", mappedBy="product")
         */


               private $image;
  /**
     * @return Image
     */
    public function getImage(): ?Image
    {
        return $this->image;
    }

    /**
     * @param Image $image
     */
    public function setImage(Image $image)
    {
        $this->image = $image;
        return $this;
    }
    }

Image

/**
 * @ORM\Entity
 * @ORM\Table(name="images")
 */
class Image
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue
     * @ORM\Id
     * @var int
     */
    private $id = 0;

    /**
     * @ORM\OneToOne(targetEntity="Product", inversedBy="image")
     * @ORM\JoinColumn(name="product_id", referencedColumnName="id")
     */
    private $product;

  /**
     * @return mixed
     */
    public function getProduct()
    {
        return $this->product;
    }

    public function setProduct(Product $product)
    {
        $this->product = $product;
    }
}

ProductAdmin

class ProductAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper->add('image', 'sonata_type_admin', array('label' => 'Okładka', 'by_reference' => false,));
}

ImageAdmin

class ImageAdmin extends AbstractAdmin
{
    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper
            ->add('file', 'file', array('label' => 'Okładka', 'required' => false))
            ->add('path', 'text', array('label' => 'Scieżka do pliku', 'required' => false));

    }

I setuped services correctly, but i can't edit product and after saving new one i geting error

unable to find the object with id : 0

2 Answers2

0

Try to not initialize your $id

private $id = 0; // =====> this is a private $id;
Jason Roman
  • 8,146
  • 10
  • 35
  • 40
KHALDOUN
  • 627
  • 5
  • 2
0

You have several mistakes. Let's try to correct your code.

  1. Just follow the tutorials and put right annotation for $id:

    /**
     * @var integer $id
     *
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
     protected $id;
    
  2. Hope, that this is just a typo with "?Image":

    /**
     * @return Image
     */
     public function getImage() : Image
     {
          return $this->image;
     }
    
  3. And finally.

     /**
      * Class ProductAdmin
      */
    class ProductAdmin extends AbstractAdmin
    {
         protected function configureFormFields(FormMapper $formMapper)
         {
             $formMapper
                 ->add('image', 'sonata_type_model_list', [
                          'btn_add'       => true,      //Or you can specify a custom label
                          'btn_list'      => 'list button!',     //which will be translated
                          'btn_delete'    => false,             //or hide the button.
                          'btn_catalogue' => 'messages', //Custom translation domain for buttons
                          'label'         => 'My image',
                      ], [
                          'placeholder'   => $this->trans('messages.no_images_message'),
                          'edit'          => 'standard',
                          'inline'        => 'standard',
                          'sortable'      => 'id',
                      ])
              ;
         }
    }
    
Manoj Kumar
  • 440
  • 1
  • 4
  • 21
staskrak
  • 873
  • 2
  • 10
  • 22