Rabu, 10 Desember 2014

Membuat CRUD dengan Doctrine Zend Framework 2


Untuk membuat CRUD pada doctrine anda ikuti langkah-langkah pada tutorial saya sebelumnya dari mulai menginstall doctrine sampai membuat database pada doctrine. jika telah anda lakukan keduanya lanjutkan kepada lanjutan ini. pertama anda edit terlebih dahulu atau lebih cocoknya anda tambahkan source code dibawah ini ke module/Application/connfig/module.config.php.


...
'user' => array(
    'type'    => 'segment',
    'options' => array(
        'route'    => '/user[/][:action][/:id]',
        'constraints' => array(
            'action' => '[a-zA-Z][a-zA-Z0-9_-]*',
            'id'     => '[0-9]+',
        ),
        'defaults' => array(
            'controller' => 'Application\Controller\Index',
            'action'     => 'index',
        ),
    ),
),
...

Selanjutnya, anda update IndexController untuk CRUD action. buka file module/Application/src/Application/Controller/IndexController.php dan masukan source code dibawah ini.


 <?php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Entity\User;

class IndexController extends AbstractActionController
{
    protected $_objectManager;

    public function indexAction()
    {
        $users = $this->getObjectManager()->getRepository('\Application\Entity\User')->findAll();

        return new ViewModel(array('users' => $users));
    }

    public function addAction()
    {
        if ($this->request->isPost()) {
            $user = new User();
            $user->setFullName($this->getRequest()->getPost('fullname'));

            $this->getObjectManager()->persist($user);
            $this->getObjectManager()->flush();
            $newId = $user->getId();

            return $this->redirect()->toRoute('home');
        }
        return new ViewModel();
    }

    public function editAction()
    {
        $id = (int) $this->params()->fromRoute('id', 0);
        $user = $this->getObjectManager()->find('\Application\Entity\User', $id);

        if ($this->request->isPost()) {
            $user->setFullName($this->getRequest()->getPost('fullname'));

            $this->getObjectManager()->persist($user);
            $this->getObjectManager()->flush();

            return $this->redirect()->toRoute('home');
        }

        return new ViewModel(array('user' => $user));
    }

    public function deleteAction()
    {
        $id = (int) $this->params()->fromRoute('id', 0);
        $user = $this->getObjectManager()->find('\Application\Entity\User', $id);

        if ($this->request->isPost()) {
            $this->getObjectManager()->remove($user);
            $this->getObjectManager()->flush();

            return $this->redirect()->toRoute('home');
        }

        return new ViewModel(array('user' => $user));
    }

    protected function getObjectManager()
    {
        if (!$this->_objectManager) {
            $this->_objectManager = $this->getServiceLocator()->get('Doctrine\ORM\EntityManager');
        }

        return $this->_objectManager;
    }
}
Selanjutnya kita akan membuat halaman utama dari CRUD dengan mengakses file module/Application/view/application/index/index.phtml dan masukan source code berikut ini.

 <div class="row">
    <div class="col-md-12">
        <div class="panel panel-default">
            <div class="panel-heading">
                <h3 class="panel-title">Users</h3>
            </div>
            <div class="panel-body">
                <a href="<?php echo $this->url('user', array('action'=>'add'));?>">Add User</a>

                <?php if (isset($users)) : ?>
                <table class="table">
                    <thead>
                    <tr>
                        <th>Id</th>
                        <th>Full name</th>
                        <th></th>
                    </tr>
                    </thead>
                <?php foreach($users as $user): ?>
                    <tbody>
                    <tr>
                        <td><?php echo $user->getId(); ?></td>
                        <td><?php echo $user->getFullName(); ?></td>
                        <td>
                            <a href="<?php echo $this->url('user', array('action'=>'edit', 'id' => $user->getId()));?>">Edit</a> |
                            <a href="<?php echo $this->url('user', array('action'=>'delete', 'id' => $user->getId()));?>">Delete</a>
                        </td>
                    </tr>
                    </tbody>
                <?php endforeach; ?>
                </table>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>
Selanjutnya anda buat file baru dengan nama add.phtml di module/Application/view/application/index/add.phtml dan masukan source code dibawah ini.

<?php

$title = 'Add new User';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<form method="post">
  fullname: <input type="text" name="fullname"><br>
  <input type="submit" value="Submit">
</form>

 
Selanjutnya anda buat file baru lagi dengan nama edit.phtml di module/Application/view/index/application/edit.phtml dan masukan source code berikut ini.

<?php

$title = 'Edit User';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
<form method="post">
  fullname: <input type="text" name="fullname" value="<?php echo $user->getFullname(); ?>"><br>
  <input type="submit" value="Submit">
</form>
 
 
Selanjutnya anda buat file baru lagi dengan nama delete.phtml di module/Application/view/application/index/delete.phtml dan masukan source code berikut ini.

<?php

$title = 'Delete User';
$this->headTitle($title);
?>
<h1><?php echo $this->escapeHtml($title); ?></h1>
Are you sure you want to delete user <?php echo $user->getFullname(); ?>? <br/>
<form method="post">
  <input type="submit" value="Delete">
</form>

Selanjutnya anda jalankan project anda, sehingga akan terlihat seperti dibawah ini.

Untuk selanjutnya cobakan untuk menambahkan user baru serta mengedit nama user dan menghapus user. terima kasih.

Tidak ada komentar:

Posting Komentar