在extbase中,如何通过uid以外的其他值访问item [英] In extbase, how to access item by another value than uid
问题描述
在自定义extbase扩展中,我需要调用show action,并向其传递除 uid
之外的另一个值(这是
In a custom extbase extension, I need to call a show action, passing it another value than uid
(this is a continuation of Use other than primary key as RealURL id_field).
由于未解析"uid以外的值",因此会导致异常 http://wiki.typo3.org/Exception/CMS/1297759968
As the value "other than uid" is not resolved, it results in the exception http://wiki.typo3.org/Exception/CMS/1297759968
这是当前有效的(但很丑陋)的控制器代码:
here's the current working (but ugly) Controller code:
/**
* ItemController
*/
class ItemController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
/**
* itemRepository
*
* @var \STUBR\Weiterbildung\Domain\Repository\ItemRepository
* @inject
*/
protected $itemRepository = NULL;
/**
* action list
*
* @return void
*/
public function listAction(\STUBR\Weiterbildung\Domain\Model\Item $item=null) {
if (!empty(\TYPO3\CMS\Core\Utility\GeneralUtility::_GET('tx_weiterbildung_pi1'))){
$this->forward('show');
}
$items = $this->itemRepository->findAll();
$this->view->assign('items', $items);
}
/**
* action show
*
* @param \STUBR\Weiterbildung\Domain\Model\Item $item
* @return void
*/
public function showAction(\STUBR\Weiterbildung\Domain\Model\Item $item=null) {
$tx_weiterbildung_pi1_get = \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('tx_weiterbildung_pi1');
if (!empty($tx_weiterbildung_pi1_get)){
$dfid = intval($tx_weiterbildung_pi1_get['durchfuehrungId']);
}
$items = $this->itemRepository->findByDurchfuehrungId($dfid);
// helpful:
//\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump($item);
$this->view->assign('item', $items[0]);
}
}
如果不可思议的方法行不通,以下是PS5中可以在存储库中使用的5行:
PS here are the 5 lines that could be used in the Repository if the magical method wouldn't work:
//public function findByDurchfuehrungId($dfid) {
// $query = $this->createQuery();
// $query->matching($query->equals('durchfuehrungId', $dfid));
// return $query->execute();
//}
推荐答案
是的,当您在参数中使用带有模型绑定的操作时,它将始终按定义为ID的字段查找对象-在我们的情况下,它始终为uid
,但请记住,您无需自动绑定模型,因为您可以自己检索模型.
Yes, when you're using actions with model binding in param it will always look for object by field defined as ID - in our case it's always uid
, but remember, that you do not need to bind model automatically, as you can retrive it yourself.
大多数人可能还记得,前一段时间我建议使用< f:link.page AdditionalParams ="{originalUid:myObj.originalUid}"点击此处</f:link.page>
而不是< f:link.action ...>
Most probably you remember, that some time ago I advised to use <f:link.page additionalParams="{originalUid:myObj.originalUid}"Click here</f:link.page>
instead of <f:link.action...>
在这种情况下,您的表演动作将如下所示:
In that case your show action would look like this:
public function showAction() {
$item = $this->itemRepository->findByOriginalUid(intval(GeneralUtility::_GET('originalUid')));
$this->view->assign('item', $item);
}
findByOriginalUid
应该在没有声明的情况下神奇地工作,但是即使不是这样,回购中也只有5行;)
Where findByOriginalUid
should work magically without declaration, but even if it doesn't it's just matter of 5 lines in the repo ;)
其他示例
根据您粘贴的代码,我会像这样使用它,在这种情况下, listAction
充当整个插件的调度员:
According to the code you pasted I'd use it rather like this, in this case listAction
gets a role of dispatcher for whole plugin:
public function listAction() {
// access get param in array
$tx_weiterbildung_pi1_get = \TYPO3\CMS\Core\Utility\GeneralUtility::_GET('tx_weiterbildung_pi1');
$dfId = intval($tx_weiterbildung_pi1_get['durchfuehrungId']);
if ($dfId > 0) { // tx_weiterbildung_pi1 exists and is positive, that means you want to go to showAction
$item = $this->itemRepository->findByDurchfuehrungId($dfId);
if (!is_null($item)) { // Forward to showAction with found $item
$this->forward('show', null, null, array('item' => $item));
}else { // Or redirect to the view URL after excluding single item ID from GET params
$this->redirectToUri($this->uriBuilder->setArgumentsToBeExcludedFromQueryString(array('tx_weiterbildung_pi1'))->build());
}
}
// No `tx_weiterbildung_pi1` param, so it should be displayed as a listAction
$items = $this->itemRepository->findAll();
$this->view->assign('items', $items);
}
/**
* @param \STUBR\Weiterbildung\Domain\Model\Item $item
*/
public function showAction(\STUBR\Weiterbildung\Domain\Model\Item $item = null) {
$this->view->assign('item', $item);
}
如果可能的话,您的发现者还应该 getFirst()
对象:
Your finder should also getFirst()
object if possible:
public function findByDurchfuehrungId($DfId) {
$query = $this->createQuery();
$query->matching($query->equals('durchfuehrungId', $DfId));
return $query->execute()->getFirst();
}
这篇关于在extbase中,如何通过uid以外的其他值访问item的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!