Zend Framework2: Tutorial Part4: Building a CRUD app by using Table Gateway1

The previous post I already wrote how to add a references to the databases connection, this post I will tell you how to building a CRUD app.

First, create a new module named “CsnUser”, or just copy the entire “Application” fold, and change the name to “CsnUser” .

then you file struction should look like this.

crud1

Second, go to open your Module.php

Here is the code:inside it quick just standard proceed, just tell where is the config file and  the namespace’s path.

<?php

namespace CsnUser;//this namespace must match your module’s name

class Module
{
public function getConfig()
{
return include __DIR__ . ‘/config/module.config.php’;
}

public function getAutoloaderConfig()
{
return array(
‘Zend\Loader\StandardAutoloader’ => array(
‘namespaces’ => array(
__NAMESPACE__ => __DIR__ . ‘/src/’ . __NAMESPACE__,
),
),
);
}

}

Third, create your UserController.php this file should path is CsnUser/src/CsnUser/controller/UserController.php

here is the code for you:

<?php

namespace CsnUser\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;

use Zend\Db\TableGateway\TableGateway;
//you create a separate file like UserForm and UserFilter, so here /just use it in your controller
use CsnUser\Form\UserForm;
use CsnUser\Form\UserFilter;

class UserController extends AbstractActionController
{
private $usersTable;

//R-retrieve CRUD
public function indexAction() {

return new ViewModel(array(‘rowset’ => $this->getUsersTable()->select()));
}

//C – create
public function createAction()
{
$form = new UserForm();
$request = $this->getRequest();
if ($request->isPost()) {
$form->setInputFilter(new UserFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$data = $form->getData();
unset($data[‘submit’]);
if (empty($data[‘usr_registration_date’])) $data[‘usr_registration_date’] = ‘2013-07-19 12:00:00’;
$this->getUsersTable()->insert($data);
return $this->redirect()->toRoute(‘csn_user/default’, array(‘controller’ => ‘user’, ‘action’ => ‘index’));
}
}

return new ViewModel(array(‘form’ => $form));
}

//U-update
public function updateAction() {
$id = $this->params()->fromRoute(‘id’);
if(!$id)
return $this->redirect ()->toRoute(‘csn_user/default’, array(‘controller’ => ‘user’, ‘action’ => ‘index’));
$form=new UserForm();
$request=$this->getRequest();
if($request->isPost())
{
$form->setInputFilter(new UserFilter());
$form->setData($request->getPost());
if ($form->isValid()) {
$data = $form->getData();
unset($data[‘submit’]);
if (empty($data[‘usr_registration_date’])) $data[‘usr_registration_date’] = ‘2013-07-19 12:00:00’;
$this->getUsersTable()->update($data,array(‘usr_id’=>$id));
return $this->redirect()->toRoute(‘csn_user/default’, array(‘controller’ => ‘user’, ‘action’ => ‘index’));
}

}
else
{
$form->setData($this->getUsersTable()->select(array(‘usr_id’ => $id))->current());
}
return new ViewModel(array(‘form’ => $form, ‘id’ => $id));
}
//D-Delete

public function deleteAction() {
$id = $this->params()->fromRoute(‘id’);
if ($id) {
$this->getUsersTable()->delete(array(‘usr_id’ => $id));
}

return $this->redirect()->toRoute(‘csn_user/default’, array(‘controller’ => ‘user’, ‘action’ => ‘index’));
}

public function getUsersTable()
{
if (!$this->usersTable) {
$this->usersTable = new TableGateway(
‘users’,
$this->getServiceLocator()->get(‘Zend\Db\Adapter\Adapter’)
//                new \Zend\Db\TableGateway\Feature\RowGatewayFeature(‘usr_id’) // Zend\Db\RowGateway\RowGateway Object
//                ResultSetPrototype
);
}
return $this->usersTable;
}
}

4. here is your model.config.php code,

<?php

return array(

‘controllers’ => array(
‘invokables’ => array(
‘CsnUser\Controller\User’ => ‘CsnUser\Controller\UserController’,
),
),

‘router’ => array(
‘routes’ => array(
‘csn_user’ => array(
‘type’    => ‘Literal’,
‘options’ => array(
‘route’    => ‘/csn-user’,
‘defaults’ => array(
‘__NAMESPACE__’ => ‘CsnUser\Controller’,
‘controller’    => ‘User’,
‘action’        => ‘index’,
),
),
‘may_terminate’ => true,
‘child_routes’ => array(
‘default’ => array(
‘type’    => ‘Segment’,
‘options’ => array(
‘route’    => ‘/[:controller[/:action[/:id]]]’,
‘constraints’ => array(
‘controller’ => ‘[a-zA-Z][a-zA-Z0-9_-]*’,
‘action’     => ‘[a-zA-Z][a-zA-Z0-9_-]*’,
‘id’          => ‘[0-9]*’,
),
‘defaults’ => array(
),
),
),

),
),
),
),
‘view_manager’ => array(
//        ‘template_map’ => array(
//            ‘layout/Auth’           => __DIR__ . ‘/../view/layout/Auth.phtml’,
//        ),
‘template_path_stack’ => array(
‘csn_user’ => __DIR__ . ‘/../view’
),
),

);

5. now time for your view, here is the struction should look like:

crud2

the post get longer now. see next post give your the code of those 3 phtml files

One thought on “Zend Framework2: Tutorial Part4: Building a CRUD app by using Table Gateway1”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s