如何使用ZF2和Doctrine列出oneToMany

时间:2015-05-18 22:35:15

标签: php zend-framework doctrine-orm zend-framework2 doctrine

我正在尝试创建listAction()以列出来自实体的所有数据。我使用了Update two tables from one form with ZF2 and DoctrineHydrator中列出的示例来创建我的研究案例。

我能够列出父数据但来自子实体的数据我只是不知道如何。

只是不知道如何创建list.phtml确实显示子数据实体。

有地方有例子吗?

ProfileController.php

public function updateAction()
{

    // Verificar se existe parâmetro passado na rota
    $id = (int)$this->params()->fromRoute('id', 0);

    // Se não existe parâmetro, redirecionar para lista
    if (!$id) {
        return $this->redirect()->toRoute(
            $this->route,
            array(
                'controller' => $this->controller,
                'action' => 'list')
        );
    }

    // Get your ObjectManager from the ServiceManager
    $objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');

    // Create the form and inject the ObjectManager
    $form = new PerfilUpdate($objectManager);

    // Buscar dados na entidade and bind it to the form
    $perfil = $this->getEm()->find($this->entity, $id);
    $form->bind($perfil);

    $request = $this->getRequest();

    if ($request->isPost()) {
        $form->setData($request->getPost());

        if ($form->isValid()) {
            // Save the changes
            $objectManager->flush();

            // Redirecionar para lista de perfis
            return $this->redirect()->toRoute(
                $this->route,
                array(
                    'controller' => $this->controller,
                    'action' => 'list')
            );

        }
    }

    return array(
        'form' => $form,
        'id' => $id,
    );
}

update.phtml

<?php
$title = $this->translate('Editar Perfil');
$this->headTitle($title);
?>
<div class="panel panel-default">
    <div class="panel-heading">
        <div class="panel-title">
            <?php echo $title ?>
        </div>
    </div>
    <?php
    $form = $this->form;

    $form->setAttribute('action',
        $this->url(
            'tenil-user/default',
            array(
                'controller' => 'profile',
                'action' => 'update',
                'id' => $this->id
            ))
    );

    $form->prepare();

    echo $this->form()->openTag($form);
    ?>
    <div class="panel-body">
        <?php

        $id = $form->get('perfil')->get('id');
        echo $this->formHidden($id);

        $security = $form->get('security');
        echo $this->formElement($security);

        echo '<div class="form-group">';
        $nome = $form->get('perfil')->get('nome');
        echo $this->formLabel($nome);
        echo '<div class="col-md-10">';
        echo $this->formInput($nome);
        echo '</div>';
        echo '</div>';

        echo '<div class="form-group">';
        $sobrenome = $form->get('perfil')->get('sobrenome');
        echo $this->formLabel($sobrenome);
        echo '<div class="col-md-10">';
        echo $this->formInput($sobrenome);
        echo '</div>';
        echo '</div>';

        echo '<div class="form-group">';
        $apelido = $form->get('perfil')->get('apelido');
        echo $this->formLabel($apelido);
        echo '<div class="col-md-10">';
        echo $this->formInput($apelido);
        echo '</div>';
        echo '</div>';

        ?>
    </div>

    <div class="panel-footer">
        <div class="clearfix">
            <div class="pull-right">
                <?php
                $submit = $form->get('submit');
                $submit->setAttribute('class', 'btn btn-default');
                echo $this->formSubmit($submit);
                ?>
            </div>
        </div>
    </div>

    <?php


    echo $this->form()->closeTag();

    foreach ($form as $element) {
        echo $this->formElementErrors($element);
    }
?>

</div>

当我提交表单时,如果更改,$id param aways设置为null,则if (!$id)为真。所以数据永远不会更新。

0 个答案:

没有答案