2016-03-15 130 views
2

我正在使用typo3 7.6。 我不能讓我的自定義JSONView extbase,它似乎不認識它。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}] 

即使在排除字段中,該位置仍然存在,並且該位置不會被輸出。看起來extbase忽略了我的重寫,但我不知道爲什麼,也沒有錯誤拋出。我把我的自定義JsonView成類/查看/ 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')); 
} 
}  

回答

0

我找到了答案,那裏w出現了兩個錯誤,一個是我的錯,另一個對我來說就像是糟糕的文檔。 我還是使用默認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'] 
       ] 
      ] 
     ] 
    ], 
];