-->

在extbase,如何比UID另一个值访问项目(In extbase, how to access

2019-11-03 16:16发布

在自定义extbase扩展,我需要调用一个show动作,传递值不是uid (这是一个延续使用比主键RealURL id_field等 )。

随着价值“比其他UID”没有解决,它导致异常http://wiki.typo3.org/Exception/CMS/1297759968

编辑:这是当前的工作(但丑陋的)控制器代码:

/**
 * 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]);
    }
}

PS这里有可能在库中使用,如果神奇的方法是行不通的5条线:

//public function findByDurchfuehrungId($dfid) {
//  $query = $this->createQuery();
//  $query->matching($query->equals('durchfuehrungId', $dfid));
//  return $query->execute();
//}

Answer 1:

是的,当你使用的是与在参数模型绑定的行为,它会永远找对象由定义为ID字段-在我们的情况下,它总是uid ,但要记住,你不需要自动绑定模式,你可以retrive它你自己。

最有可能你还记得,前一段时间我建议使用<f:link.page additionalParams="{originalUid:myObj.originalUid}"Click here</f:link.page>代替<f:link.action...>

在这种情况下,你的表演动作是这样的:

public function showAction() {
   $item = $this->itemRepository->findByOriginalUid(intval(GeneralUtility::_GET('originalUid')));
   $this->view->assign('item', $item);
}

findByOriginalUid神奇的工作不申报,但即使没有它只是无关紧要的5号线在回购;)

其他样本

根据你的代码粘贴我会使用它,而这样的,在这种情况下listAction得到调度员对整个插件的作用:

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()对象,如果可能的:

public function findByDurchfuehrungId($DfId) {
    $query = $this->createQuery();
    $query->matching($query->equals('durchfuehrungId', $DfId));
    return $query->execute()->getFirst();
}


文章来源: In extbase, how to access item by another value than uid