所以我有一个DataObject,我通过标准网格域(使用Silverstripe 4)通过Model Admin管理
我希望能够生成直接编辑此对象的链接。如果我手动输入ModelAdmin网址,我可以这样做,但这似乎不是很动态(如果我们将来更改管理员网址,那么该方法将会中断)。
我试图实现以下内容:
class MyObject extends DataObject
{
public function EditLink()
{
$classname = str_replace('\\', '-', $this->owner->ClassName);
$admin = OrderAdmin::singleton();
$fields = $admin->getEditForm()->Fields();
$grid = $fields
->fieldByName($classsname);
return Controller::join_links(
$grid->Link("item"),
$this->owner->ID,
"edit"
);
}
}
但这会输出此错误&堆栈跟踪:
[Emergency] Uncaught InvalidArgumentException: SilverStripe\ORM\DataObject is not a subclass of DataObject
POST /as-plumbing-app-v2/admin/jobs/Job/EditForm/field/Job/item/1/ItemEditForm/
Line 147 in /home/morven/Projects/as-plumbing-app-v2/vendor/silverstripe/framework/src/ORM/DataObjectSchema.php
Source
138 {
139 $class = ClassInfo::class_name($class);
140 $current = $class;
141 while ($next = get_parent_class($current)) {
142 if ($next === DataObject::class) {
143 return $current;
144 }
145 $current = $next;
146 }
147 throw new InvalidArgumentException("$class is not a subclass of DataObject");
148 }
149
150 /**
151 * Get the base table
152 *
153 * @param string|object $class
Trace
SilverStripe\ORM\DataObjectSchema->baseDataClass(SilverStripe\ORM\DataObject)
DataQuery.php:165
SilverStripe\ORM\DataQuery->initialiseQuery()
DataQuery.php:76
SilverStripe\ORM\DataQuery->__construct(SilverStripe\ORM\DataObject)
DataList.php:61
SilverStripe\ORM\DataList->__construct(SilverStripe\ORM\DataObject)
ReflectionClass->newInstanceArgs(Array)
InjectionCreator.php:23
SilverStripe\Core\Injector\InjectionCreator->create(SilverStripe\ORM\DataList, Array)
Injector.php:585
SilverStripe\Core\Injector\Injector->instantiate(Array, SilverStripe\ORM\DataList, prototype)
Injector.php:988
SilverStripe\Core\Injector\Injector->getNamedService(SilverStripe\ORM\DataList, , Array)
Injector.php:941
SilverStripe\Core\Injector\Injector->get(SilverStripe\ORM\DataList, , Array)
Injector.php:1111
SilverStripe\Core\Injector\Injector->createWithArgs(SilverStripe\ORM\DataList, Array)
Injectable.php:30
SilverStripe\View\ViewableData::create(SilverStripe\ORM\DataObject)
SearchContext.php:160
SilverStripe\ORM\Search\SearchContext->getQuery(Array, , )
SearchContext.php:210
SilverStripe\ORM\Search\SearchContext->getResults(Array)
ModelAdmin.php:347
SilverStripe\Admin\ModelAdmin->getList()
OrderAdmin.php:166
SilverCommerce\OrdersAdmin\Admin\OrderAdmin->getList()
ModelAdmin.php:184
SilverStripe\Admin\ModelAdmin->getEditForm(, )
OrderAdmin.php:88
SilverCommerce\OrdersAdmin\Admin\OrderAdmin->getEditForm()
Estimate.php:151
SilverCommerce\OrdersAdmin\Model\Estimate->EditLink()
JobDetailForm_ItemRequest.php:117
App\Forms\GridField\JobDetailForm_ItemRequest->doViewInvoice(Array, SilverStripe\Forms\Form, SilverStripe\Control\HTTPRequest, SilverStripe\Forms\FormRequestHandler)
FormRequestHandler.php:231
SilverStripe\Forms\FormRequestHandler->httpSubmission(SilverStripe\Control\HTTPRequest)
RequestHandler.php:320
SilverStripe\Control\RequestHandler->handleAction(SilverStripe\Control\HTTPRequest, httpSubmission)
RequestHandler.php:201
SilverStripe\Control\RequestHandler->handleRequest(SilverStripe\Control\HTTPRequest)
RequestHandler.php:225
SilverStripe\Control\RequestHandler->handleRequest(SilverStripe\Control\HTTPRequest)
GridFieldDetailForm.php:118
SilverStripe\Forms\GridField\GridFieldDetailForm->handleItem(SilverStripe\Forms\GridField\GridField, SilverStripe\Control\HTTPRequest)
GridField.php:1019
SilverStripe\Forms\GridField\GridField->handleRequest(SilverStripe\Control\HTTPRequest)
RequestHandler.php:225
SilverStripe\Control\RequestHandler->handleRequest(SilverStripe\Control\HTTPRequest)
RequestHandler.php:225
SilverStripe\Control\RequestHandler->handleRequest(SilverStripe\Control\HTTPRequest)
Controller.php:207
SilverStripe\Control\Controller->handleRequest(SilverStripe\Control\HTTPRequest)
LeftAndMain.php:750
SilverStripe\Admin\LeftAndMain->handleRequest(SilverStripe\Control\HTTPRequest)
AdminRootController.php:123
SilverStripe\Admin\AdminRootController->handleRequest(SilverStripe\Control\HTTPRequest)
Director.php:360
SilverStripe\Control\Director->SilverStripe\Control\{closure}(SilverStripe\Control\HTTPRequest)
VersionedHTTPMiddleware.php:40
SilverStripe\Versioned\VersionedHTTPMiddleware->process(SilverStripe\Control\HTTPRequest, Closure)
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest)
AuthenticationMiddleware.php:61
SilverStripe\Security\AuthenticationMiddleware->process(SilverStripe\Control\HTTPRequest, Closure)
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest)
CanonicalURLMiddleware.php:155
SilverStripe\Control\Middleware\CanonicalURLMiddleware->process(SilverStripe\Control\HTTPRequest, Closure)
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest)
FlushMiddleware.php:26
SilverStripe\Control\Middleware\FlushMiddleware->process(SilverStripe\Control\HTTPRequest, Closure)
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest)
RequestProcessor.php:66
SilverStripe\Control\RequestProcessor->process(SilverStripe\Control\HTTPRequest, Closure)
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest)
SessionMiddleware.php:20
SilverStripe\Control\Middleware\SessionMiddleware->process(SilverStripe\Control\HTTPRequest, Closure)
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest)
AllowedHostsMiddleware.php:60
SilverStripe\Control\Middleware\AllowedHostsMiddleware->process(SilverStripe\Control\HTTPRequest, Closure)
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest)
TrustedProxyMiddleware.php:176
SilverStripe\Control\Middleware\TrustedProxyMiddleware->process(SilverStripe\Control\HTTPRequest, Closure)
HTTPMiddlewareAware.php:62
SilverStripe\Control\Director->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest)
HTTPMiddlewareAware.php:65
SilverStripe\Control\Director->callMiddleware(SilverStripe\Control\HTTPRequest, Closure)
Director.php:369
SilverStripe\Control\Director->handleRequest(SilverStripe\Control\HTTPRequest)
HTTPApplication.php:48
SilverStripe\Control\HTTPApplication->SilverStripe\Control\{closure}(SilverStripe\Control\HTTPRequest)
call_user_func(Closure, SilverStripe\Control\HTTPRequest)
HTTPApplication.php:66
SilverStripe\Control\HTTPApplication->SilverStripe\Control\{closure}(SilverStripe\Control\HTTPRequest)
call_user_func(Closure, SilverStripe\Control\HTTPRequest)
ErrorControlChainMiddleware.php:56
SilverStripe\Core\Startup\ErrorControlChainMiddleware->SilverStripe\Core\Startup\{closure}(SilverStripe\Core\Startup\ErrorControlChain)
call_user_func(Closure, SilverStripe\Core\Startup\ErrorControlChain)
ErrorControlChain.php:236
SilverStripe\Core\Startup\ErrorControlChain->step()
ErrorControlChain.php:226
SilverStripe\Core\Startup\ErrorControlChain->execute()
ErrorControlChainMiddleware.php:69
SilverStripe\Core\Startup\ErrorControlChainMiddleware->process(SilverStripe\Control\HTTPRequest, Closure)
HTTPMiddlewareAware.php:62
SilverStripe\Control\HTTPApplication->SilverStripe\Control\Middleware\{closure}(SilverStripe\Control\HTTPRequest)
HTTPMiddlewareAware.php:65
SilverStripe\Control\HTTPApplication->callMiddleware(SilverStripe\Control\HTTPRequest, Closure)
HTTPApplication.php:67
SilverStripe\Control\HTTPApplication->execute(SilverStripe\Control\HTTPRequest, Closure, )
HTTPApplication.php:49
SilverStripe\Control\HTTPApplication->handle(SilverStripe\Control\HTTPRequest)
index.php:17
我假设这是因为我在创建它时没有将正确的响应对象传递给ModelAdmin,所以不确定要使用哪个ModelClass?
有没有人对实施此方法的首选方法有任何想法?
答案 0 :(得分:3)
这适用于SilverStripe 3和4(在3.6和4.0上测试)
class MyObject extends DataObject
{
public function EditLink()
{
$classname = str_replace('\\', '-', singleton($this->owner->ClassName)->i18n_plural_name());
$admin = OrderAdmin::singleton();
$fields = $admin->getEditForm()->Fields();
$grid = $fields->dataFieldByName($classname);
return Controller::join_links(
$grid->Link("item"),
$this->owner->ID,
"edit"
);
}
}
对原始内容进行的更改包括
$classname
的拼写错误->dataFieldByName()
而不是->fieldByName()
注意:SilverStripe 4.0使用正确的命名空间,因此您还要确保为DataObject使用正确的命名空间。
E.g。
namespace MySite;
use SilverStripe\ORM\DataObject;
class MyObject extends DataObject
{
...