Things to Remember

Friday, 1 October 2010

zf10 tutorial

zf create project zf10

zf create action add Index
zf create action edit Index
zf create action edit Index

in public/index.php, change development environment from production, to development

in application.ini, add in the production section:

phpSettings.date.timezone = "Europe/London"

resources.db.adapter = PDO_MYSQL
resources.db.params.host = localhost
resources.db.params.username = web
resources.db.params.password = 123456
resources.db.params.dbname = zf10

resources.view.doctype = "XHTML1_STRICT"

run a mysql query:
CREATE TABLE albums ( id int(11) NOT NULL auto_increment, artist varchar(100) NOT NULL, title varchar(100) NOT NULL, PRIMARY KEY (id)
);

INSERT INTO albums (artist, title) VALUES ('Paolo Nutine', 'Sunny Side Up'), ('Florence + The Machine', 'Lungs'), ('Massive Attack', 'Heligoland'), ('Andre Rieu', 'Forever Vienna'), ('Sade', 'Soldier of Love');

create a zend db table model thing:
zf create db-table Albums albums

edit the models/dbtable/albums.php

class Application_Model_DbTable_Albums extends Zend_Db_Table_Abstract
{

protected $_name = 'albums';

public function getAlbum($id)
{
$id = (int)$id; $row = $this->fetchRow('id = ' . $id);
if (!$row)
{
throw new Exception("Could not find row $id"); return $row->toArray();
}
}

public function addAlbum($artist, $title)
{
$data = array( 'artist' => $artist, 'title' => $title,);
$this->insert($data);
}


public function updateAlbum($id, $artist, $title)
{
$data = array( 'artist' => $artist, 'title' => $title,);
$this->update($data, 'id = '. (int)$id);
}


public function deleteAlbum($id)
{
$this->delete('id =' . (int)$id);
}

}

use zend tool again
zf enable layout

open application/layouts/scripts/layout.phtml
edit:

$this->headMeta()->appendHttpEquiv('Content-Type', 'text/html;charset=utf-8');
$this->headTitle()->setSeparator(' - ');
$this->headTitle('Zend Framework Tutorial');
echo $this->doctype();
?>


headMeta(); ?>
headTitle(); ?>
headLink()->prependStylesheet($this->baseUrl().'/css/site.css'); ?>




escape($this->title); ?>


layout()->content; ?>





Create /public/css folder
create site.css:

body,html
{
margin: 0 5px;
font-family: Verdana,sans-serif;
}

h1
{
font-size: 1.4em;
color: #008000;
}

a
{
font-size: 1.4em;
color: #008000;
}

/* Table */

th
{
text-align: left;
}

td, th
{
padding-right: 5px;
}

/* style form */
form dt
{
width: 100px;
display: block;
float: left;
clear: left;
}

form dd
{
margin-left: 0; float: left;
}

form #submitbutton
{
margin-left: 100px;
}


edit views/scripts/index.phtml

$this->title = "My Albums"; $this->headTitle($this->title);
?>


Add new album








albums as $album) : ?>





Title Artist  

escape($album->title);?>

escape($album->artist);?>

Edit
Delete


zf tool:
zf create form Album

edit application/forms/Album.php


class Application_Form_Album extends Zend_Form
{

public function init()
{
$this->setName('album');

$id = new Zend_Form_Element_Hidden('id');
$id->addFilter('Int');

$artist = new Zend_Form_Element_Text('artist');
$artist->setLabel('Artist')->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');

$title = new Zend_Form_Element_Text('title');
$title->setLabel('Title')
->setRequired(true)
->addFilter('StripTags')
->addFilter('StringTrim')
->addValidator('NotEmpty');

$submit = new Zend_Form_Element_Submit('submit');
$submit->setAttrib('id', 'submitbutton');

$this->addElements(array($id, $artist, $title, $submit));
}


}



edit index/add action:

public function addAction()
{
$form = new Application_Form_Album();
$form->submit->setLabel('Add');
$this->view->form = $form;

if ($this->getRequest()->isPost())
{
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData)) {
$artist = $form->getValue('artist');
$title = $form->getValue('title');
$albums = new Application_Model_DbTable_Albums();
$albums->addAlbum($artist, $title);
$this->_helper->redirector('index');
}
else
{
$form->populate($formData);
}
}
}


edit views/scripts/index/add.phtml


$this->title = "Add new album";
$this->headTitle($this->title);
echo $this->form ;

?>

edit index/edit action:

$form = new Application_Form_Album();
$form->submit->setLabel('Save');
$this->view->form = $form;
if ($this->getRequest()->isPost())
{
$formData = $this->getRequest()->getPost();
if ($form->isValid($formData))
{
$id = (int)$form->getValue('id');
$artist = $form->getValue('artist');
$title = $form->getValue('title');
$albums = new Application_Model_DbTable_Albums();
$albums->updateAlbum($id, $artist, $title);
$this->_helper->redirector('index');
}
else
{
$form->populate($formData);
}
}
else
{

$id = $this->_getParam('id', 0);
if ($id > 0)
{
$albums = new Application_Model_DbTable_Albums();
$form->populate($albums->getAlbum($id));
}
}

edit views/scripts/index/edit.phtml
$this->title = "Edit album";
$this->headTitle($this->title);
echo $this->form ;
?>

edit index/delete

if ($this->getRequest()->isPost())
{
$del = $this->getRequest()->getPost('del');
if ($del == 'Yes')
{
$id = $this->getRequest()->getPost('id');
$albums = new Application_Model_DbTable_Albums();
$albums->deleteAlbum($id);
}
$this->_helper->redirector('index');
}
else
{
$id = $this->_getParam('id', 0);
$albums = new Application_Model_DbTable_Albums();
$this->view->album = $albums->getAlbum($id);
}


finally edit views/scripts/index/delete.phtml

$this->title = "Delete album";
$this->headTitle($this->title);
?>

Are you sure that you want to delete'escape($this->album['title']); ?>' by 'escape($this->album['artist']); ?>'?