Zend framework, database connection

I set up a database connection in application/config/application.ini

resources.db.adapter = "PDO_MYSQL"
resources.db.params.host = "localhost"
resources.db.params cialis preise holland.username = "username"
resources.db.params.password = "password"
resources.db.params.dbname = "montesnegro"

Next I created models:

zf create db-table Content content
zf create model ContentMapper
zf create model Content
zf create controller Content

I created the PHP for the Content model in application/models/content

class Application_Model_Content
{
    private $id;
    private $menu_id;
    private $content_text;
    private $update_date;

    function __construct($id = null,$menu_id = null,$content_text = null,$update_date = null)
    {
        $this->id = $id;
        $this->menu_id = $menu_id;
        $this->content_text = $content_text;
        $this->update_date = $update_date;
    }


    public function setContentText($content_text)
    {
        $this->content_text = $content_text;
        return $this;
    }

    public function getContentText()
    {
        return $this->content_text;
    }

    //other getters and setters.....
}

I created a ContentMapper class

class Application_Model_ContentMapper
{

    /**
     * @var Zend_Db_Table_Abstract
     */
    protected $_dbTable;

    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Application_Model_DbTable_Content');
        }
        return $this->_dbTable;
    }

    public function save(Application_Model_Content $content)
    {
        $data = array(
            'content_text'   => $content->getContentText(),
            'menu_id' => $content->getMenuId(),
            'update_text' => date('Y-m-d H:i:s'),
        );

        if (null === ($id = $content->getId())) {
            unset($data['id']);
            $this->getDbTable()->insert($data);
        } else {
            $this->getDbTable()->update($data, array('id = ?' => $id));
        }
    }

    public function getMenu()
    {
        $select = $this->getDbTable()->select()->order("order ASC ");
        $results = $this->getDbTable()->fetchAll($select);
        $menu   = array();
        foreach ($results as $row) {
            $menu[$row->url] = $row->title;
        }
        return $menu;

    }

    public function find($id, Application_Model_Content $content)
    {
        $result = $this->getDbTable()->find($id);
        if (0 == count($result)) {
            return;
        }
        $this->setContent($result->current(), $content);
    }

    public function findByUrl($url,Application_Model_Content $content)
    {
        if(!$url){
            $url = 'home';
        }
        $select = $this->getDbTable()->select()->where("url = '$url' ");
        $result = $this->getDbTable()->fetchRow($select);
        if (0 == count($result)) {
            return;
        }
        $this->setContent($result->toArray(), $content);
    }

    private function setContent($row, Application_Model_Content $content)
    {
        $content->setId($row['id'])
            ->setContentText($row['content_text'])
            ->setMenuId($row['menu_id'])
            ->setUpdateDate($row['update_date'])
            ->setUrl($row['url'])
            ->setTitle($row['title'])
        ;
    }

    public function fetchAll()
    {
        $resultSet = $this->getDbTable()->fetchAll();
        $entries   = array();
        foreach ($resultSet as $row) {
            $entry = new Application_Model_Content();
            $this->setContent($row, $entry);
            $entries[] = $entry;
        }
        return $entries;
    }

}

And then I created the controller

class ContentController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        // action body
        $content_mapper = new Application_Model_ContentMapper();

        $url = $this->getRequest()->getParam('url');
        $content = new Application_Model_Content();
        $content_mapper->findByUrl($url, $content);
        $this->view->content = $content;

    }


}

Finally, I created a MySQL table called content and put some stuff in there.

When I did http://localhost/content, all was well.

Comments are closed.