【问题标题】:Sonata Admin Bundle configureRoutes getPersistentParametersSonata 管理包 configureRoutes getPersistentParameters
【发布时间】:2015-05-05 15:19:10
【问题描述】:

我是索纳塔的新手。我有一个涉及客户和贷款的项目。在 ClientsAdmin.php 我配置了 configureRoutes 和 getPersistentParameters 函数

protected function configureRoutes(RouteCollection $collection)
{
    $collection->add('transacciones','transacciones/{id}');
}
public function getPersistentParameters()
{
    if (!$this->getRequest()) {
        return array();
    }

    return array(
        'id'  => $this->getRequest()->get('id'),
    );
} 

另外,我已经覆盖了 CRUDController(和 service.yml)

//service.yml

financiera.admin.clientes:
    class: BitsMkt\FinancieraBundle\Admin\ClientesAdmin
    arguments: [ ~,BitsMkt\FinancieraBundle\Entity\Clientes,FinancieraBundle:ClientesCRUD]
    tags:
        - {name: sonata.admin, manager_type: orm, group: Sistema, label: Clientes}



//ClientesCRUDController.php
namespace Bitsmkt\FinancieraBundle\Controller;

use Sonata\AdminBundle\Controller\CRUDController;

class ClientesCRUDController extends CRUDController
{
    public function transaccionesAction($id = null)
    {
        //throw new \RuntimeException('The Request object has not been set ' . $id);

        if (false === $this->admin->isGranted('LIST')) {
            throw new AccessDeniedException();
        }
        $id = $this->get('request')->get($this->admin->getIdParameter());

        if ($id == '*') {
            # TODOS - Viene de Dashboard

        }else
        {

            $object = $this->admin->getObject($id);

            if (!$object) {
                throw new NotFoundHttpException(sprintf('unable to find the object with id : %s', $id));
            }

            $this->admin->setSubject($object);            
        }


        $datagrid = $this->admin->getDatagrid();
        $formView = $datagrid->getForm()->createView();

        // set the theme for the current Admin Form
        $this->get('twig')->getExtension('form')->renderer->setTheme($formView, $this->admin->getFilterTheme());

        return $this->render('FinancieraBundle:Frontend:prestamos_clientes.html.twig', array(
            'action'     => 'list',
            'form'       => $formView,
            'datagrid'   => $datagrid,
            'csrf_token' => $this->getCsrfToken('sonata.batch'),
        ));


    }
}

prestamos_clientes.html.twig 视图显示客户和贷款信息。

问题: 我想用 $id 参数过滤我创建的列表视图 (transaccionesAction) 并查看特定客户的贷款。

谢谢。

【问题讨论】:

  • 为什么不为贷款实体创建一个管理员并在configuredatagridfilter() 添加客户列表

标签: parameters routes sonata-admin


【解决方案1】:

您可以将管理员设置为另一个管理员的子级。这样做的好处是,例如,您可以从一个特定客户点击该特定客户的贷款列表。

为此,请遵循有关将管理员设置为子管理员主题的简约文档:https://sonata-project.org/bundles/admin/master/doc/reference/architecture.html#create-child-admins

完成后,您可以添加从客户到贷款的链接:

向您的客户端管理员添加功能“configureSideMenu”:

/**
 * {@inheritdoc}
 */
protected function configureSideMenu(MenuItemInterface $menu, $action, AdminInterface $childAdmin = null)
{
    // show link only on edit and show
    if (!$childAdmin && !in_array($action, array('edit', 'show'))) {
        return;
    }
    $admin = $this->isChild() ? $this->getParent() : $this;
    $id = $admin->getRequest()->get('id');


    $menu->addChild(
        'Loans',
        array('uri' => $this->getChild('your.loan.service.id')->generateUrl('list', array('id' => $id)))
    );
}

你可以在奏鸣曲的演示中看到这个演示: http://demo.sonata-project.org/

点击“电子商务”->“订单”->“特定订单”->“元素”

您可以在此处找到上述示例的代码: https://github.com/sonata-project/ecommerce/tree/master/src/OrderBundle/Admin

有关子父管理设置的更多信息: Sonata/symfony - parent/child structure setup

【讨论】:

  • 谢谢@11mb!非常有帮助。在客户的编辑视图中,我有一个指向贷款列表的链接,甚至我可以从那里编辑它们。效果很好!甚至我在 _action 元素中添加了一个 Loans Button 并且像快捷方式一样工作。为了继续使用这种方法,我将 Loans 连接到 Collections。工作正常。但是当我去编辑一个特定的客户时,点击 Loans 并编辑其中一个,我不能 addMenu Collections 来继续不受约束的关系?另一种方法是直接链接到编辑贷款(不嵌入客户编辑视图中)
  • 很好,它对你有用。奏鸣曲很好地隐藏了所有有趣的东西;)..我不认为我真的理解你在评论中的问题..也许你可以改写一下?我对我的回答有额外的评论:我注意到我的搜索功能不再起作用了。我更改了 $admin->getRequest() 代码。有关更多信息,请参见此处:stackoverflow.com/questions/19551715/…
  • 对不起我的英语。我将尝试解释我的问题。例如,在贷款类中,我有一个指向客户类的属性。而且,我添加了一个指向卖家类的属性。当我像这样配置 LoansAdmin 配置函数时: $this->parentAssociationMapping = 'seller'; $this->parentAssociationMapping = 'client';因此,当我在客户列表中时,贷款按钮会将我带到按客户端属性过滤的贷款列表(在 configureDatagridFilters 中)但是,当我进入卖方列表时,贷款按钮不起作用并且卖方 ID 不起作用! 我可以这样做吗?
猜你喜欢
  • 2016-10-02
  • 2014-04-28
  • 2012-09-24
  • 2016-07-03
  • 2012-01-18
  • 2015-10-23
  • 1970-01-01
  • 2017-01-13
  • 2016-06-30
相关资源
最近更新 更多