【问题标题】:Extbase not recognizing custom JsonViewExtbase 无法识别自定义 JsonView
【发布时间】:2016-03-15 17:32:13
【问题描述】:

我正在使用typo3 7.6。 我无法让 extbase 获取我的自定义 JsonView,它似乎无法识别它。

我这样覆盖默认的 JsonView:

use TYPO3\CMS\Extbase\Mvc\View\JsonView as ExtbaseJsonView;
class JsonView extends ExtbaseJsonView
{
/**
 * @var array
 */
protected $configuration = [
    'jobs' => [
        '_exclude' => ['pid'],
        '_descend' => [
            'place' => [
                '_only' => ['name']
            ]
        ]
    ],
];
}

编辑:据此docu

现在我不明白为什么我仍然得到这个输出为 Json:

[{"description":"Owns products","pensum":100,"pid":55,"test":"Product","title":"Product Owner","uid":1}]

一个 pid 仍然存在,即使它在排除字段中并且该位置没有被输出。似乎 extbase 忽略了我的覆盖,但我不知道为什么,也没有抛出任何错误。我将自定义 JsonView 放入 Classes/View/JsonView.php

我的模型是:

工作

/**
* Job
*/
class Job extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{
/**
 * title
 *
 * @var string
 * @validate NotEmpty
 */
protected $title = '';

/**
 * description
 *
 * @var string
 */
protected $description = '';

/**
 * pensum
 *
 * @var int
 */
protected $pensum = 0;

/**
 * test
 *
 * @var string
 */
protected $test = '';

/**
 * place
 *
 * @var \Vendor\SfpJobs\Domain\Model\Place
 */
protected $place = null;

// below getter and setter

}

地点

/**
* Places
*/
class Place extends \TYPO3\CMS\Extbase\DomainObject\AbstractEntity
{

/**
 * name
 *
 * @var string
 * @validate NotEmpty
 */
protected $name = '';

/**
 * numberOfEmployees
 *
 * @var int
 * @validate NotEmpty
 */
protected $numberOfEmployees = 0;

/**
 * acquired
 *
 * @var \DateTime
 * @validate NotEmpty
 */
protected $acquired = null;

// below getter and setter

}

控制器

/**
* JobAjaxController
*/
class JobAjaxController extends ActionController
{

/**
 * @var string
 */
protected $defaultViewObjectName = \TYPO3\CMS\Extbase\Mvc\View\JsonView::class;

/**
 * jobRepository
 *
 * @var \Vendor\SfpJobs\Domain\Repository\JobRepository
 * @inject
 */
protected $jobRepository = NULL;

/**
 * placeRepository
 *
 * @var \Vendor\SfpJobs\Domain\Repository\PlaceRepository
 * @inject
 */
protected $placeRepository = NULL;

/**
 * action list
 * This function
 *
 * @param \Vendor\SfpJobs\Domain\Model\Job $job
 * @return void
 */
public function listAction()
{
    $jobs = $this->jobRepository->findAll();
    $this->view->assign('jobs', $jobs);
    $this->view->setVariablesToRender(array('jobs'));
}
}    

【问题讨论】:

    标签: php json extbase typo3-7.6.x


    【解决方案1】:

    我找到了答案,有两个错误,一个是我的错,另一个在我看来像是糟糕的文档。 我仍然在我的控制器中使用默认的 Jsonview 所以我需要

    protected $defaultViewObjectName = \Vendor\Jobs\View\JsonView::class;
    

    而不是

    protected $defaultViewObjectName = \TYPO3\CMS\Extbase\Mvc\View\JsonView::class;
    

    第二个是我错误的 Jsonview 配置。 在docu 中显示

    'variable3' => array(
     *          '_exclude' => array('secretTitle'),
     *          '_descend' => array(
     *              'customer' => array(
     *                  '_only' => array('firstName', 'lastName')
     *              )
     *          )
     *      ),
    

    我认为 variable3 可能是一个数组,但事实并非如此。数组的符号是这样的:

    'somearrayvalue' => array(
     *          '_descendAll' => array(
     *              '_only' => array('property1')
     *          )
     *      )
    

    所以最后我有了这个配置

    protected $configuration = [
        'jobs' => [
            '_descendAll' => [
                '_exclude' => ['pid'],
                '_descend' => [
                    'place' => [
                        '_only' => ['name']
                    ]
                ]
            ]
        ],
    ];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-08-27
      • 2016-01-30
      • 2012-08-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-23
      • 1970-01-01
      相关资源
      最近更新 更多