我收到了这段代码:
/**
* Transforms field value to readable text for listing.
*
* @param string $fieldName
* @param Entity_Topic $entity
* @return string
*/
public function presenter($fieldName, *** $entity) {
$years = range(2014, 2020);
if ('dateFrom' == $fieldName) {
foreach($years as $year) {
$list = array(
$year.'-01-01' => 'Leden '.$year,
$year.'-02-01' => 'Únor '.$year,
$year.'-03-01' => 'Březen '.$year,
$year.'-04-01' => 'Duben '.$year,
$year.'-05-01' => 'Květen '.$year,
$year.'-06-01' => 'Červen '.$year,
$year.'-07-01' => 'Červenec '.$year,
$year.'-08-01' => 'Srpen '.$year,
$year.'-09-01' => 'Září '.$year,
$year.'-10-01' => 'Říjen '.$year,
$year.'-11-01' => 'Listopad '.$year,
$year.'-12-01' => 'Prosinec '.$year,
);
return $list[$entity->getDateFrom()];
}
}
return '';
}
此代码仅显示2014年的数据,其他年份仅为空。任何想法如何让它正常工作,以便它可以显示其他年份?
if ('dateFrom' == $fieldName) {
foreach($years as $year) {
$list = array(
***
);
}
return $list[$entity->getDateFrom()];
}
这不起作用..
答案 0 :(得分:0)
您已解决了一个问题但仍有一个问题。您在$list
循环的每次迭代中覆盖foreach
。即使你做$list[] = array(...
,问题也不会消失,因为你的最终目的是从这个数组中获取适当的数据。因此,请按以下方式更改presenter()
方法,
public function presenter($fieldName, $entity) {
$years = range(2014, 2020);
if ('dateFrom' == $fieldName) {
$years = range(2014, 2020);
$months = array('Leden', 'Únor', 'Březen', 'Duben', 'Květen', 'Červen', 'Červenec', 'Srpen', 'Září', 'Říjen', 'Listopad', 'Prosinec');
$list = array();
foreach($years as $year){
for($i = 1; $i <= 12; ++$i){
$list[$year . '-' . $i . '-01'] = $months[$i - 1] . ' ' . $year;
}
}
return $list[$entity->getDateFrom()];
}
return '';
}