如何获取独立元素?例如$language->LanguageName
.
这是我的控制器代码:
class IndexController extends AbstractActionController {
protected $entityManager;
public function getEntityManager()
{
if (null === $this->entityManager) {
$this->entityManager = $this->getServiceLocator()->get('DoctrineORMEntityManager');
}
return $this->entityManager;
}
public function indexAction() {
$language = $this->getEntityManager()
->getRepository('LanguagesModuleEntityLanguageCommon')
->findAll();
$viewModel = new ViewModel([ 'languages' => $language ]);
return $viewModel;
}}
这是我的视图文件代码...
<?php foreach ($this->languages as $language) : ?>
<tr>
<pre><?php print_r($language) ?></pre>
</tr>
结果我有
LanguagesModuleEntityLanguageCommon Object
(
[LanguageId:protected] => 1
[LanguageName:protected] => English
[LanguageCode2:protected] => en
[LanguageCode3:protected] => eng
[LanguageImage:protected] => img_path
[LanguageActive:protected] => 1
)
LanguagesModuleEntityLanguageCommon Object
(
[LanguageId:protected] => 2
[LanguageName:protected] => Other
[LanguageCode2:protected] => ot
[LanguageCode3:protected] => oth
[LanguageImage:protected] => image_path
[LanguageActive:protected] => 1
)
您返回的是一个实体数组。因此,循环访问对象并使用实体的getters
来获取每个字段的值。这应该有效:
<?php foreach ($languages as $language) : ?>
<tr>
<td><?php echo $language->getLanguageId(); ?></td>
<td><?php echo $language->getLanguageName(); ?></td>
<td>....</td>
</tr>
<?php endforeach;?>