簡體   English   中英

在Sonata管理員列表視圖中顯示不同的實體

[英]Showing different entities in Sonata Admin list view

我有這個實體,其中包含entityName屬性和entityId屬性:

    /**
     * @var string
     *
     * @ORM\Column(name="entityName", type="string", length=255)
     */
    private $entityName;

    /**
     * @var integer
     * @ORM\Column(name="entityId", type="integer")
     */
    private $entityId;

我沒有使用__toString()函數顯示該實體,而是想實際返回帶有名稱和ID的實體。 並在索納塔管理員列表視圖中顯示。

現在,這里是__toString

public function __toString()
{
    return $this->entityName . ":" . $this->entityId;
}

應該返回如下內容:

public function __toString()
{
    return $em->getRepository($this->entityName)->find($this->entityId);
}

我希望我已經很好地描述了我的問題。 n

一種解決方法是對奏鳴曲使用自定義列表塊。

  1. 創建一個名為entityFilter 的新樹枝過濾器 ,該過濾器會將奏鳴曲管理對象的FQCN轉換為奏鳴曲生成的可讀路線名稱。 admin_blablabla_show

     public function entityFilter($entityName) { $str = str_replace('\\\\', '_', $entityName); $str = str_replace('Bundle', '', $str); $str = str_replace('_Entity', '', $str); $str = 'Admin' . $str . '_Show'; return strtolower($str); } public function getName() { return 'my_extension'; } 
  2. 在您的管理類中,將所需字段的模板設置為新的樹枝模板:

      ->add('orderItems', null, array( 'template' => 'AcmeBundle::order_items_list.html.twig' )) 
  3. 在新的樹枝模板(order_items_list.html.twig)中:

     {% extends 'SonataAdminBundle:CRUD:base_list_field.html.twig' %} {% block field %} <div> {% for item in object.orderItems %} {% if entity(item.entityName) == 'admin_first_entity_show' %} {% set foo = 'Apple ID' %} {% elseif entity(item.entityName) == 'admin_second_entity_show' %} {% set foo = 'Device Accessory' %} {% else %} {% set foo = 'Not defiend' %} {% endif %} <a target="_blank" class="btn btn-default btn-xs" href="{{ path(entity(item.entityName), {'id': item.entityId}) }}"><i class="fa fa-external-link"></i> {{ foo }}</a> {% endfor %} </div> {% endblock %} 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM