ZF1 resulting from controller in view

Asked

Viewed 278 times

1

I’m not sure how in this case to play the values in the project view, follow the code:

public function indexAction()
{
    $actor = new Application_Model_Actor();
    $resultadoActores = $actor->listarAtor();

    foreach($resultadoActores as $ator)
    {

        $idActor = $ator->actor_id;

        $film_actor = new Application_Model_FilmActor();
        $resultadoFilmeAtores = $film_actor->listarFilmeActorId($idActor);

        foreach ($resultadoFilmeAtores as $filmeAtor)
        {

            $idFilm = $filmeAtor->film_id;

            $film = new Application_Model_Film();
            $resultadoFilmes = $film->listarFilmeId($idFilm);

            foreach ($resultadoFilmes as $filme)
            {
                echo $ator->first_name . " " . $filme->title . "<br/>"; // <-- Esse resultado na view?
            }

        }
    }
}

I tried to do something like: $this->view->movies = $resultadoFilmes; and inside the controller view create the foreach and assign the values, but unsuccessfully too.

2 answers

2

I believe the problem is more logical than with Zend Framework 1.

If you make the code you said

$this->view->filmes = $resultadoFilmes;

You’ll just take the last actor’s movies, which might be no movie.

Rewriting your controller code:

public function indexAction()
{
    $actor = new Application_Model_Actor();
    $resultadoActores = $actor->listarAtor();

    //variável para guardar filmes
    $filmes = array();

    foreach($resultadoActores as $ator)
    {

        $idActor = $ator->actor_id;


        $film_actor = new Application_Model_FilmActor();
        $resultadoFilmeAtores = $film_actor->listarFilmeActorId($idActor);



        foreach ($resultadoFilmeAtores as $filmeAtor)
        {

            $idFilm = $filmeAtor->film_id;

            $film = new Application_Model_Film();
            $resultadoFilmes = $film->listarFilmeId($idFilm);

            foreach($resultadoFilmes as $resultadoFilme){
                $filmes[] = array(
                    'ator' => $ator->first_name, 
                    'filme' => $resultadoFilme->title
                );
            }
        }
    }

    $this->view->filmes = $filmes;
}

In the view, simply:

<?php foreach($this->filmes as $filme): ?>
<?php echo $filme['ator'] ?> <?php echo $filme['filme'] ?>
<?php endforeach?>

Sources:

http://framework.zend.com/manual/1.10/en/zend.view.controllers.html

http://framework.zend.com/manual/1.10/en/zend.view.scripts.html

1

How are you doing in the view to consume the data? should be the type:

<?php foreach($resultadoFilmes as $filme): ?>
<?php echo $filme->title; ?>
<?php endforeach;?>

Helped?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.