字段图片未链接到管理员

时间:2014-08-07 03:08:14

标签: php symfony sonata-admin symfony-sonata

我有两个实体:ProductImage,很多产品可以有很多 图像因此生成第三个表ProductHasImages。实体是对的 as doctrine:schema:validate命令输出:

Symfony > doctrine:schema:validate
[Mapping]  OK - The mapping files are correct.
[Database] FAIL - The database schema is not in sync with the current
mapping file.
The command terminated with an error status (2)

我已将此行添加到config.yml

services:
  tan.common.admin.image:
    class: Tan\CommonBundle\Admin\ImageAdmin
    tags:
      - { name: sonata.admin, manager_type: orm, label: "Imagenes",
show_in_dashboard: false }
    arguments: [null, Tan\CommonBundle\Entity\Image, null]

我已经创建了文件Tan\CommonBundle\Admin\ImageAdmin.php 以下内容:

<?php

namespace Tan\CommonBundle\Admin;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Form\FormMapper;

class ImageAdmin extends Admin
{

    protected function configureFormFields(FormMapper $formMapper)
    {
        $formMapper->add('file', 'file', array('required' => false));
    }

    public function prePersist($image)
    {
        $this->manageFileUpload($image);
    }

    public function preUpdate($image)
    {
        $this->manageFileUpload($image);
    }

    private function manageFileUpload($image)
    {
        if ($image->getFile()) {
            $image->refreshUpdated();
        }
    }

}

现在我尝试将image字段添加到ProductAdmin.php,如下所示:

    protected function configureFormFields(FormMapper $form)
    {

        $form
            ->add('product_name', null, array('label' => 'Nombre'))
            ->add('product_description', null, array('label' => 'Descripción'))
            ->add('image', 'sonata_type_admin', array('delete' => false));
    }

但是每当我尝试添加新产品时,我都会收到此错误:

  

当前字段image未链接到管理员。请创建一个   对于目标实体:``

为什么呢?我做错了什么?

1 个答案:

答案 0 :(得分:3)

对于文件最好的奏鸣曲管理员使用Sonata Media Bundle来处理上传,一旦你配置了你的映射就可以在不同的管理员中重复使用它们

实体

  • 产品
  • ProductHasImages

现在Products实体将拥有ProductHasImages的映射,可以指向许多图像(来自奏鸣曲媒体包的媒体文件),类似于

/**
 * @Assert\NotBlank()
 * @ORM\OneToMany(targetEntity="Namespace\YourBundle\Entity\ProductHasImages", mappedBy="productImages",cascade={"persist","remove"} )
 */
protected $images;

other fields ...

生成其getter和setter方法

removeImages()
getImages()
setImages()
addImages()

现在您的交汇点实体(ProductHasImages)将有2个映射指向Products,而另一个将指向Sonata Media

/**
 * @var \Application\Sonata\MediaBundle\Entity\Media
 * @Assert\NotBlank()
 * @ORM\ManyToOne(targetEntity="Application\Sonata\MediaBundle\Entity\Media", cascade={"persist"}, fetch="LAZY")
 * @ORM\JoinColumn(name="media_id", referencedColumnName="id")
 */
protected $media;

/**
 * @var \Namespace\YourBundle\Entity\Products
 * @Assert\NotBlank()
 * @ORM\ManyToOne(targetEntity="Namespace\YourBundle\Entity\Products", cascade={"persist","remove"} ,inversedBy="images", fetch="LAZY" )
 * @ORM\JoinColumn(name="product_id", referencedColumnName="id",nullable=true)
 */
protected $productImages;

生成他们的getter和setter

现在为ProductHasImages

创建Admin类

configureFormFields函数

中有一个字段
$formMapper->add('media', 'sonata_type_model_list', array('required' => false), array(
            'link_parameters' => $link_parameters
        )); //other fields too if you want to show in collection 

在您的Product Admin类中添加此字段

    ->add('images', 'sonata_type_collection', array(
            'cascade_validation' => false,
            'type_options' => array('delete' => false),
        ), array(

            'edit' => 'inline',
            'inline' => 'table',
            'sortable' => 'position',
            'link_parameters' => array('context' => 'default'),
            'admin_code' => 'sonata.admin.product_has_images' 
      /*here provide service name for junction admin 
        like service code for admin defined for ProductHasImages */
        )
    )

您可以在Git Hub

找到完整的代码演示