SonataAdmin中的一对一关系

时间:2017-07-21 12:37:27

标签: symfony sonata-admin sonata

我尝试为与图像有1:1关系的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;
    }
    }

图像

/**
 * @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));

    }

我正确设置了服务,但我无法编辑产品,并在保存新的错误之后

  

无法找到ID为0的对象

2 个答案:

答案 0 :(得分:0)

尝试不初始化您的$ id

private $id = 0; // =====> this is a private $id;

答案 1 :(得分:0)

你有几个错误。让我们尝试纠正您的代码。

  1. 只需按照教程并为$ id:

    添加写注释
    /**
     * @var integer $id
     *
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
     protected $id;
    
  2. 希望,这只是一个错误的"?Image":

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

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