user2294256 user2294256 - 5 months ago 11
PHP Question

issue with some codes in zend framework1

I am following this tutorial:Getting Started with Zend Framework 1.11


http://akrabat.com/wp-content/uploads/Getting-Started-with-Zend-Framework.pdf


, on page 12, "Listing albums"


  1. D:\program files (x86)\Zend\Apache2\htdocs\zf-tutorial\application\models\DbTable\Albums.php


    <?php

    class Application_Model_DbTable_Albums extends Zend_Db_Table_Abstract {

    protected $_name = 'albums';

    public function getAlbum($id)
    {
    ...
    }
    public function addAlbum($artist, $title)
    {
    ...
    }

    public function updateAlbum($id, $artist, $title)
    {
    ...
    }
    public function deleteAlbum($id)
    {
    ...
    }


    }







  1. D:\program files (x86)\Zend\Apache2\htdocs\zf-tutorial\application\controllers\IndexController.php


    <?php

    class IndexController extends Zend_Controller_Action
    {
    public function indexAction()
    $albums = new Application_Model_DbTable_Albums();
    $this->view->albums = $albums->fetchAll();
    }
    }







  1. D:\program files (x86)\Zend\Apache2\htdocs\zf-tutorial\application\views\scripts\index\index.phtml


    <?php
    $this->title = "My Albums";
    $this->headTitle($this->title);
    ?>
    <p><a href="<?php echo $this->url(array('controller'=>'index',
    'action'=>'add'));?>">Add new album</a></p>
    <table>
    <tr>
    <th>Title</th>
    <th>Artist</th>
    <th>&nbsp;</th>
    </tr>
    <?php foreach($this->albums as $album) : ?>
    <tr>
    <td><?php echo $this->escape($album->title);?></td>
    <td><?php echo $this->escape($album->artist);?></td>
    <td>
    <a href="<?php echo $this->url(array('controller'=>'index',
    'action'=>'edit', 'id'=>$album->id));?>">Edit</a>
    <a href="<?php echo $this->url(array('controller'=>'index',
    'action'=>'delete', 'id'=>$album->id));?>">Delete</a>
    </td>
    </tr>
    <?php endforeach; ?>
    </table>






Questions:


  1. Albums.php, why we need this one:
    protected $_name = 'albums';
    ?

  2. IndexController.php

    a.
    $albums = new Application_Model_DbTable_Albums();
    we did not use include/require "Albums.php", how could we use thi class:
    Application_Model_DbTable_Albums
    ?

    b.
    $this->view->albums = $albums->fetchAll();
    what does this mean:
    $this->view->albums
    ? why not use
    $this->albums
    ?

  3. index.phtml

    a.
    $this->url(array('controller'=>'index','action'=>'add'))
    , where can I check this method:
    $this->url()
    ?

    b. why we use
    foreach($this->albums as $album)
    here? not
    $this->view->albums
    as in IndexController.php?


Answer

Albums.php, why we need this one: protected $_name = 'albums';?

When using a DbTable model in Zend Framework (Application_Model_DbTable_Albums) the protected member $_name must be the name of the database table that the model is associated with. The DbTable model will in effect become the database adapter for that one table.

$albums = new Application_Model_DbTable_Albums(); we did not use include/require "Albums.php", how could we use thi class:Application_Model_DbTable_Albums?

Classes and files that conform to the expected naming conventions and belong to predefined resources are automatically autoloaded by Zend_Loader. The class Application_Model_DbTable_Albums and the file Application/Model/DbTable/Albums.php both conform to the naming conventions and belong to a predefined resources.

$this->view->albums = $albums->fetchAll(); what does this mean: $this->view->albums? why not use $this->albums?

$this->view->albums = $ablums->fetchAll(); is a shorthand way of assigning the values obtained from $albums->fetchAll() to the Zend_View object for display in the view script. The values can be echoed in the view script as echo $this->albums or more likely they can be looped with a foreach and then displayed.

$this->url(array('controller'=>'index','action'=>'add')), where can I check this method: $this->url()?

This is actually a little hard to pin down easily, you'll find a basic definition in the Zend_View helper reference, just scroll down until you see it. A somewhat better example can be found in the Zend_Controller reference router section. Essentially the url() helper is the preferred method of creating a link in the view script, it is not the only method and sometimes not the best.

why we use foreach($this->albums as $album) here? not $this->view->albums as in IndexController.php?

in the controller we assign data to the view object $this->view->data = $data in the view script (index.phtml) we are in the view object so we just need to access the supplied data <?php echo $this->data ?>. In the case of $this->albums the data is a rowset object (an array of Zend_Db_Row objects) which can be iterated using a foreach loop. Most valid data types can be assigned to the view using this method.

This is a pretty simple overview of these concepts, I hope it helps.

Comments