我有两个实体Crawler
和Customer
。Crawler
包含从远程URL解析的条目,Customer
包含从Crawler
数据手动创建的条目。
我分别创建了CrawlerAdmin
和CustomerAdmin
。现在,为了创建新的Customer
条目,我希望在CrawlerAdmin
列表视图中进行自定义操作,例如"创建客户"。通过单击按钮,应使用预先填充所选Crawler
条目的值的字段显示CustomerAdmin
创建视图。
如何产生这种结果?
编辑:作为可能的替代方法,我在自定义的CrudController中创建了一个"克隆"功能。它采用Crawler
条目并自动基于它创建新的Customer
条目。然后,有必要重定向编辑新创建的Customer
条目的视图。
我如何从自定义调音仪重定向到CustomerAdmin
编辑视图,并具有新创建的条目的ID?
这是控制器:
<?php
// src/CrawlerBundle/Controller/CRUDController.php
namespace CrawlerBundleController;
use FrontendBundleEntityCustomer;
use SonataAdminBundleControllerCRUDController as Controller;
use SymfonyComponentHttpFoundationRedirectResponse;
use SymfonyComponentHttpKernelExceptionNotFoundHttpException;
class CRUDController extends Controller
{
/**
* @param $id
* @return RedirectResponse
*/
public function cloneAction($id)
{
$object = $this->admin->getSubject();
if (!$object) {
throw new NotFoundHttpException'unable to find object');
}
$newObject = new Customer();
$newObject->setCompanyName($object->getCompanyName().' (Clone)');
$newObject->setRegisterNumber($object->getRegisterNumber().' (Clone)');
$newObject->setFields(array()); // ignore this
$this->admin->create($newObject);
$this->addFlash('sonata_flash_success', 'Cloned successfully');
// return new RedirectResponse($this->admin->generateUrl('list'));
// if you have a filtered list and want to keep your filters after the redirect
return new RedirectResponse($this->admin->generateUrl('list', array('filter' => $this->admin->getFilterParameters())));
// just redirects to the class list view
}
}
我的服务定义:
crawler.admin.crawler:
class: CrawlerBundleAdminCrawlerAdmin
arguments: [~, CrawlerBundleEntityCrawler, CrawlerBundle:CRUD]
tags:
- { name: sonata.admin, manager_type: orm, group: admin, label: Crawler, pager_type: "simple" }
如果您需要更多信息,请评论
预先感谢大家!
因此,在您的链接中创建一个新孩子时,只需通过父ID传递URL参数,然后在您的子admin中,只需获取此参数并使用新对象设置新对象。示例:configureformfields函数:
protected function configureFormFields(FormMapper $formMapper)
{
$object = $formMapper->getFormBuilder()->getData();
$parentId = $this->getRequest()->get('objectId');
if (null === $object || null === $object->getParentDocument()) {
$object = new ChildEntity();
$parentDoc = $this->getParentAdmin()->getObject($parentId);
if (null !== $parentDoc) {
$object->setParentDocument($parentDoc);
}
$formMapper->getFormBuilder()->setData($object);
}
....
}