7

複数の LineItem エンティティを関連付けることができる Order エンティティがあります。

Order の Admin クラスと LineItem の Admin クラスを作成しました。しかし、LineItem Admin クラスを Order Admin クラスの子にする必要があります。

LineItemAdmin クラスでは、 を設定しましprotected $parentAssociationMapping = 'order';た。

また、OrderAdmin クラスの configureFormFields メソッドに、 を追加しまし->add('lineItems', 'sonata_type_model')た。

しかし、それでもうまくいきません。注文フォームの項目のリストはクリックできないため、注文管理フォームから LineItem 管理リスト ページを取得する方法がわかりません。

構成する必要があるルートはありますか? lineItemsフォーム フィールドに必要な変更はありますか?

Sonata Admin バンドルに関する適切なドキュメントを見つけるのは非常に困難でした。

PS。SonataAdminBundle コードを調べても役に立ちませんでした。コードが複雑なため、理解するのが非常に難しいからです。

4

2 に答える 2

5

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.

于 2012-11-28T09:44:10.760 に答える
1

次のことができます。

// 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 クラスが必要なため必要です。

そのソリューションは、多くの請求書項目を含む私の請求書バンドルでうまく機能します。

于 2013-08-02T21:04:14.593 に答える