如何在视图中使用doctrine查询结果?

时间:2015-04-10 16:02:54

标签: php doctrine-orm zend-framework2

我如何获得独立元素?例如$language->LanguageName

这是我的控制器代码:

class IndexController extends AbstractActionController {

protected $entityManager;

public function getEntityManager()
{
    if (null === $this->entityManager) {
        $this->entityManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
    }
    return $this->entityManager;
}

public function indexAction() {
    $language = $this->getEntityManager()
                     ->getRepository('\LanguagesModule\Entity\LanguageCommon')
                     ->findAll();

    $viewModel = new ViewModel([ 'languages' => $language ]);
    return $viewModel;
}}

这是我的查看文件代码......

<?php foreach ($this->languages as $language) : ?>
<tr>
    <pre><?php print_r($language) ?></pre>
</tr>

因此我有

LanguagesModule\Entity\LanguageCommon Object
(
    [LanguageId:protected] => 1
    [LanguageName:protected] => English
    [LanguageCode2:protected] => en
    [LanguageCode3:protected] => eng
    [LanguageImage:protected] => img_path
    [LanguageActive:protected] => 1
)

LanguagesModule\Entity\LanguageCommon Object
(
    [LanguageId:protected] => 2
    [LanguageName:protected] => Other
    [LanguageCode2:protected] => ot
    [LanguageCode3:protected] => oth
    [LanguageImage:protected] => image_path
    [LanguageActive:protected] => 1
)

1 个答案:

答案 0 :(得分: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;?>