menu

Phalcon - CRUD (Create)


Pada tutorial sebelumnya kita sudah membuat sistem read data user dari database dan menampilkannya dalam list maupun detail per masing-masing user. Pada tutorial kali ini kita akan membuat sistem create untuk menginputkan atau menambah data user pada database

Oke kita langsung saja memulai eksperiment create data pada phalcon,
Pertama, buat terlebih dahulu route untuk create 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->addGet(
    "/test",
    [
        "controller"        => "test",
        "action"            => "test"
    ]
);

$router->handle();

Selanjutnya, buat controller untuk menampilkan halaman create dan untuk menyimpan data user baru 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.');
        }
    }
}

Kemudian, buat file view untuk create dengan membuat file baru dengan nama create.volt pada path app/views/user/ dan isikan code berikut,
{% extends 'layouts/default1.volt' %}

{% block content %}
 <br/>
 Hello codedoct!!!
 <br><br>
 <div class="row">
  <div class="col-md-12">
   <form method="POST">
    <div class="form-group">
     <label>Nama user</label>
     <input type="text" name="name" class="form-control" placeholder="Type your name" required>
    </div>
    <div class="form-group">
     <label>Email user</label>
     <input type="email" name="email" class="form-control" placeholder="Type your email" required>
    </div>
    <div class="form-group">
     <label>Address user</label>
     <textarea name="address" class="form-control" placeholder="Type your address" required></textarea>
    </div>
    <input type="submit" value="Save"> <button type="button" onclick="window.location.href='../user'">Cancel</button>
   </form>
  </div>
 </div>
{% endblock %}

Terakhir, edit file view 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></td>
     </tr>
    {% endfor %}
   </table>
   <br>
   <button onclick="window.location.href='user/create'">Create</button>
  </div>
 </div>
{% endblock %}

Sehingga hasilnya akan tampak seperti ini,



===DONE!===