gpt4 book ai didi

admin - 自动生成 SilverStripe 管理链接

转载 作者:行者123 更新时间:2023-12-01 13:27:49 25 4
gpt4 key购买 nike

所以我有一个数据对象,我通过标准网格字段(使用 Silverstripe 4)通过模型管理管理

我希望能够生成一个链接以直接由管理员编辑此对象。如果我手动输入 ModelAdmin URL,我可以做到这一点,但这看起来不是很动态(如果我们将来更改管理 URL,那么该方法就会中断)。

我已尝试实现以下内容:

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?

有没有人对实现这个的首选方式有任何想法?

最佳答案

这应该适用于 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 使用正确的命名空间。

例如

namespace MySite;

use SilverStripe\ORM\DataObject;

class MyObject extends DataObject
{
...

关于admin - 自动生成 SilverStripe 管理链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47580026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com