dongyuanliao6204 2014-08-07 03:08
浏览 29
已采纳

字段图片未链接到管理员

I have two entities: Product and Image and many Products can have many Images so this generates a third table ProductHasImages. Entities are right as doctrine:schema:validate command outputs:

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)

I've added this lines to my 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]

I've have the file Tan\CommonBundle\Admin\ImageAdmin.php created with the following content:

<?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();
        }
    }

}

Now I'm trying to add the image field to ProductAdmin.php as follow:

    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));
    }

But any time I try to add a new product I get this error:

The current field image is not linked to an admin. Please create one for the target entity : ``

Why? What I'm doing wrong?

  • 写回答

1条回答 默认 最新

  • dongwei2030 2014-08-12 05:54
    关注

    For files the best in sonata admin use Sonata Media Bundle to handle uploads and you can reuse them in your different admin once you have configured then your mapping will be

    Entities

    • Products
    • ProductHasImages

    Now Products entity will have a mapping for ProductHasImages which can point many images (Media files from sonata media bundle) similar to

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

    Generate its getter and setter methods

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

    And now your junction entity i.e (ProductHasImages) will have a 2 mappings to point back Products and other one will point to 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;
    

    Generate their getters and setters

    Now create Admin class for ProductHasImages

    which has a field in configureFormFields function

    $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 
    

    And in your Product Admin class add this field

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

    You can find full code demo here Git Hub

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)