dongli8979 2016-03-15 17:32
浏览 36

Extbase无法识别自定义JsonView

I am using typo3 7.6. I can't make extbase take my custom JsonView, it seems like it does not recognize it.

I overwrite the default JsonView like this:

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

EDIT: According to this docu

Now I don't understand why I still get this output as Json:

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

One the pid is still there even though it is in the exclude field and the place does not get outputed. It seems like extbase is ignoring my override, but I don't know why and no errors are ever thrown. I put my custom JsonView into Classes/View/JsonView.php

My models are:

Job

/**
* 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

/**
* 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

}

Controller

/**
* 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'));
}
}    
  • 写回答

1条回答 默认 最新

  • dongshen9686 2016-03-17 13:19
    关注

    I found an answer, there were two mistakes one was my fault another seems to me like poor documentation. I still used the default Jsonview in my controller so I needed

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

    instead of

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

    The second was my faulty Jsonview config. In the docu it reads

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

    I thought that the variable3 could be an array but this is not true. The notation for arrays is like this:

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

    so in the end I had this config

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

报告相同问题?

悬赏问题

  • ¥15 一道python难题2
  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备