奏鸣曲管理 - 儿童管理班

发布于 2024-12-26 22:32:38 字数 585 浏览 0 评论 0原文

我有一个 Order 实体,它可以关联多个 LineItem 实体。

我为 Order 创建了一个 Admin 类,为 LineItem 创建了一个 Admin 类。但我需要 LineItem Admin 类成为 Order Admin 类的子类。

在 LineItemAdmin 类中,我设置了 protected $parentAssociationMapping = 'order';

另外,在 OrderAdmin 类的 configureFormFields 方法中,我添加了 ->add('lineItems', 'sonata_type_model')

然而,它仍然不起作用。订单表单中的行项目列表不可单击,因此我无法看到如何从订单管理表单中获取 LineItem 管理列表页面。

是否有需要配置的路由?我需要对 lineItems 表单字段进行更改吗?

很难找到有关 Sonata Admin 捆绑包的任何好的文档,因此我们将不胜感激。

附言。即使浏览 SonataAdminBundle 代码也无济于事,因为该代码由于其复杂性而很难理解。

I have an Order entity, which can have multiple LineItem entities associated.

I've created an Admin class for Order and an Admin class for LineItem. But I need the LineItem Admin class to be a child of the Order Admin class.

In the LineItemAdmin class, I've set protected $parentAssociationMapping = 'order';.

Also, in the OrderAdmin class configureFormFields method, I have added ->add('lineItems', 'sonata_type_model').

However, it still doesn't work. The list of line items in the order form are not clickable, so I cannot see how to get from the Order admin form to say the LineItem admin list page.

Are there routes that need to be configured? Are there changes to the lineItems form field that I need to make?

It's been very difficult to find any good documentation on the Sonata Admin bundle, so any help would be appreciated.

PS. Even going through the SonataAdminBundle code hasn't helped, as the code is very difficult to follow owing to its complexity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡墨 2025-01-02 22:32:38

刚刚经历了未记录功能的相同问题,您似乎错过的唯一步骤是在父 OrderAdmin 类上调用 addChildconfigureSideMenu

此解决方案将在侧菜单外创建一个单独的页面,其中将包含 lineItems,它们不会嵌入到 OrderAdmin 表单中(我不确定这是否可能)。

无需配置任何路由,SonataAdmin 会为您处理此操作。

下面是一个使用注释的父管理类示例:

namespace YourVendor\YourBundle\Admin;

use JMS\DiExtraBundle\Annotation\Service;
use JMS\DiExtraBundle\Annotation\Tag;
use JMS\DiExtraBundle\Annotation\Inject;
use JMS\DiExtraBundle\Annotation\InjectParams;

use Knp\Menu\ItemInterface as MenuItemInterface;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Admin\AdminInterface;

/**
 * @Service("sonata.admin.order")
 * @Tag("sonata.admin", attributes={"manager_type"="orm", "group"="Orders", "label"="Orders"})
 */
class OrderAdmin extends Admin
{
    /**
     * @InjectParams({
     *     "code" = @Inject("%your.parameters.code%"),
     *     "class" = @Inject("%your.parameters.class%"),
     *     "baseControllerName" = @Inject("%your.parameters.controller%"),
     *     "lineItems" = @Inject("sonata.admin.line_item")
     * })
     */
    public function __construct($code, $class, $baseControllerName, $lineItems)
    {
        parent::__construct($code, $class, $baseControllerName);

        $this->addChild($lineItems);
    }

    protected function configureSideMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
    {
        if (!$childAdmin && !in_array($action, array('edit', 'show'))) { return; }

        $admin = $this->isChild() ? $this->getParent() : $this;
        $id = $admin->getRequest()->get('id');

        $menu->addChild('Show Order', array('uri' => $admin->generateUrl('show', array('id' => $id))));
        $menu->addChild('Edit Order', array('uri' => $admin->generateUrl('edit', array('id' => $id))));
        $menu->addChild('Line items', array('uri' => $admin->generateUrl('sonata.admin.line_item.list', array('id' => $id))));
    }
}

如果您的服务使用 XML 或 YML,您可能不需要 __construct 方法,因为 addChild 调用可以进入服务定义。

在撰写本文时,JMS DiExtra Bundle 存在一个未解决的问题,其中包含拉取请求专用的 @Admin 注释也可以避免此要求。不过几周来一直很平静。

Having just gone through the same problems with undocumented functionality, the only steps you appear to have missed are calling addChild and configureSideMenu on the parent OrderAdmin class.

This solution will create a separate page off a sidemenu which will contain the lineItems, they will not be embedded in the OrderAdmin form (I'm not sure that this is possible).

There aren't any routes to be configured, as SonataAdmin handles this for you.

Here's an example parent admin class, using annotations:

namespace YourVendor\YourBundle\Admin;

use JMS\DiExtraBundle\Annotation\Service;
use JMS\DiExtraBundle\Annotation\Tag;
use JMS\DiExtraBundle\Annotation\Inject;
use JMS\DiExtraBundle\Annotation\InjectParams;

use Knp\Menu\ItemInterface as MenuItemInterface;

use Sonata\AdminBundle\Admin\Admin;
use Sonata\AdminBundle\Admin\AdminInterface;

/**
 * @Service("sonata.admin.order")
 * @Tag("sonata.admin", attributes={"manager_type"="orm", "group"="Orders", "label"="Orders"})
 */
class OrderAdmin extends Admin
{
    /**
     * @InjectParams({
     *     "code" = @Inject("%your.parameters.code%"),
     *     "class" = @Inject("%your.parameters.class%"),
     *     "baseControllerName" = @Inject("%your.parameters.controller%"),
     *     "lineItems" = @Inject("sonata.admin.line_item")
     * })
     */
    public function __construct($code, $class, $baseControllerName, $lineItems)
    {
        parent::__construct($code, $class, $baseControllerName);

        $this->addChild($lineItems);
    }

    protected function configureSideMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
    {
        if (!$childAdmin && !in_array($action, array('edit', 'show'))) { return; }

        $admin = $this->isChild() ? $this->getParent() : $this;
        $id = $admin->getRequest()->get('id');

        $menu->addChild('Show Order', array('uri' => $admin->generateUrl('show', array('id' => $id))));
        $menu->addChild('Edit Order', array('uri' => $admin->generateUrl('edit', array('id' => $id))));
        $menu->addChild('Line items', array('uri' => $admin->generateUrl('sonata.admin.line_item.list', array('id' => $id))));
    }
}

If you use XML or YML for your services you probably won't need the __construct method as the addChild calls can go in the service definition.

At the time of writing there's an open issue with the JMS DiExtra Bundle with pull request for a dedicated @Admin annotation which may also avoid this requirement. It's been quiet for a couple of weeks though.

情感失落者 2025-01-02 22:32:38

您可以执行以下操作:

// Order Entity
/**
 * @ORM\OneToMany(targetEntity="OrderItem", mappedBy="invoice", cascade={"persist", "remove"}, orphanRemoval=true)
 * 
 */
protected $items;
...



// OrderAdmin.php
protected function configureFormFields(FormMapper $formMapper)
{
        $formMapper
        ->with('Order Items')
            ->add('items', 'sonata_type_collection', array('required' => true,
                                                           'by_reference' => false,
                                                           ),
                                                     array('edit' => 'inline',
                                                           'inline' => 'table',))
 ....

确保您还有一个 OrderItem 管理员。这是必要的,因为 sonata_type_collection 需要 Admin 类才能工作。

该解决方案非常适合我的包含许多发票项目的发票捆绑包。

You can to the following:

// Order Entity
/**
 * @ORM\OneToMany(targetEntity="OrderItem", mappedBy="invoice", cascade={"persist", "remove"}, orphanRemoval=true)
 * 
 */
protected $items;
...



// OrderAdmin.php
protected function configureFormFields(FormMapper $formMapper)
{
        $formMapper
        ->with('Order Items')
            ->add('items', 'sonata_type_collection', array('required' => true,
                                                           'by_reference' => false,
                                                           ),
                                                     array('edit' => 'inline',
                                                           'inline' => 'table',))
 ....

Make sure that you have also an OrderItem Admin. Thats necessary because of the sonata_type_collection needs an Admin class to work.

That solution works fine for in my invoice bundle with many invoice items.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文