menu

Phalcon - CRUD (Delete)


Akhirnya sampai juga pada tutorial terakhir sistem CRUD Phalcon, pada tutorial terakhir ini kita akan membuat feature delete yang akan digunakan untuk menghapus data user pada database

Oke langsung saja kita mulai tutorialnya,
Pertama, buat terlebih dahulu routenya dengan mengedit file router.php pada path app/config/ menjadi seperti ini,
<?php

$router = $di->getRouter();

// Define your routes here
/**
 * Get user lists data by query
 */
$router->addGet(
    "/user",
    [
        "controller"        => "user",
        "action"            => "index"
    ]
);
$router->addGet(
    "/user/{userId}",
    [
        "controller"        => "user",
        "action"            => "show"
    ]
);
$router->addGet(
    "/user/create",
    [
        "controller"        => "user",
        "action"            => "create"
    ]
);
$router->addPost(
    "/user/create",
    [
        "controller"        => "user",
        "action"            => "save"
    ]
);
$router->addDelete(
    "/user/delete/{userId}",
    [
        "controller"        => "user",
        "action"            => "delete"
    ]
);
$router->addPut(
    "/user/edit/{userId}",
    [
        "controller"        => "user",
        "action"            => "edit"
    ]
);

$router->addGet(
    "/test",
    [
        "controller"        => "test",
        "action"            => "test"
    ]
);

$router->handle();

Selanjutnya, buat method delete dengan mengedit file UserController.php pada path app/controllers/ menjadi seperti ini,
<?php

class UserController extends ControllerBase
{
    public function indexAction()
    {
     $users = Users::find();
     $this->view->title = "Phalcon - Users";
     $this->view->users = $users;
     // dd($users);
    }

    public function showAction($userId)
    {
     $conditions = ['userId'=>$userId];
     $user = Users::findFirst([
      'conditions' => 'id=:userId:',
      'bind' => $conditions,
  ]);
     $this->view->title = "Phalcon - User";
     $this->view->user = $user;
     // dd($users);
    }

    public function createAction()
    {
        $this->view->title = "Phalcon - Create user";
    }

    public function saveAction()
    {
        $name = $this->request->get('name');
        $email = $this->request->get('email');
        $address = $this->request->get('address');

        $user = new Users();
        $user->name = $name;
        $user->email = $email;
        $user->address = $address;

        if ($this->request->isPost()) {
            if ($user->create()) {
                return $this->response->redirect('user');
            }
        } else {
            $this->flash->error('User form is not valid.');
        }
    }

    public function deleteAction($userId)
    {
        $conditions = ['userId'=>$userId];
        $user = Users::findFirst([
            'conditions' => 'id=:userId:',
            'bind' => $conditions,
        ]);
        if ($user->delete() === false) {
            $messages = $user->getMessages();
            foreach ($messages as $message) {
                $this->flash->error($message);
            }
        } else {
            return $this->response->redirect('user');
        }
    }

    public function editAction($userId)
    {
        $conditions = ['userId'=>$userId];
        $user = Users::findFirst([
            'conditions' => 'id=:userId:',
            'bind' => $conditions,
        ]);
        $this->view->title = "Phalcon - User";
        $this->view->user = $user;

        if ($this->request->isPost()) {
            $name = $this->request->get('name');
            $email = $this->request->get('email');
            $address = $this->request->get('address');

            $user->name = $name;
            $user->email = $email;
            $user->address = $address;

            if ($user->save()) {
                return $this->response->redirect('user');
            } else {
                $messages = $user->getMessages();
                foreach ($messages as $message) {
                    $this->flash->error($message);
                }
            }
        }
    }
}

Terakhir buat button atau link untuk menghapus data pada view index dengan mengedit file index.volt pada path app/views/user/ menjadi seperti ini,
{% extends 'layouts/default1.volt' %}

{% block content %}
 <br/>
 Hello codedoct!!!
 <br><br>
 <div class="row">
  <div class="col-md-12">
   user list:
   <br><br>
   <table width="100%">
    <tr>
     <th>Name</th>
     <th>Action</th>
    </tr>
    {% for user in users %}
     <tr>
      <td>{{ user.name }}</td>
      <td><a href="user/{{ user.id }}">show</a> || <a href="user/edit/{{ user.id }}">edit</a> || <a href="user/delete/{{ user.id }}">delete</a></td>
     </tr>
    {% endfor %}
   </table>
   <br>
   <button onclick="window.location.href='user/create'">Create</button>
  </div>
 </div>
{% endblock %}

Sehingga tampilan halaman utamanya akan tampak seperti ini,


===DONE!===