Skip to content
AlexFloppy edited this page Jan 29, 2015 · 12 revisions

Description

Implemented default CRUD functionality such as view/create/edit/delete entity.

Usage

Controller

Your controller must extends AbstractCrudController . The following methods must be implemented:

  • getEntity() - return entity
  • getCreateForm() - set form for create action
  • getEditForm() - set form for edit action

Example of CRUD implementation in module Pages:

use Pages\Grid\Grid;
use Fury\Mvc\Controller\AbstractCrudController;
use Zend\View\Model\ViewModel;
use Pages\Form\Create;
use Pages\Entity\Pages;

/**
 * Class ManagementController
 * @package Pages\Controller
 */
class ManagementController extends AbstractCrudController
{
    /**
     * @return mixed|\Pages\Entity\Pages
     */
    protected function getEntity()
    {
        /**
         * @var $entity = \Pages\Entity\Pages $entity
         */
        $entity = new Pages();
        // set authorId to entity
        $entity->setAuthorId($this->identity()->getUser()->getId());
        return $entity;
    }

    /**
     * @return mixed|Create
     */
    protected function getCreateForm()
    {
        return new Create(null, ['serviceLocator' => $this->getServiceLocator()]);
    }

    /**
     * @return mixed|Create
     */
    protected function getEditForm()
    {
        $form = new Create(null, ['serviceLocator' => $this->getServiceLocator()]);
        // set custom name for submit button
        $form->get('submit')->setValue('Save');
        return $form;
    }

    /**
     * @return array|ViewModel
     */
    public function indexAction()
    {
        $sm = $this->getServiceLocator();
        $grid = new Grid($sm);
        $viewModel = new ViewModel(['grid' => $grid]);
        // disable layout if xml request 
        $viewModel->setTerminal($this->getRequest()->isXmlHttpRequest());
        return $viewModel;
    }
}

View

Implemented default create/edit views: /module/Application/view/layout/crud.

Customizing

Controller

Each action can be redefined:

  • loadEntity() - if custom query for entity required
  • createAction() - custom create action
  • editAction() - custom edit action
  • deleteAction() - custom delete action

View

Implemented ability to attach javascript to the view:

return new ViewModel(['scripts' => ['script-alias-from-config-requirejs'] ]);

Using AJAX

Implemented ability to get create/edit views using ajax. Just add class="dialog" to the action's link and set method data-ajax-method="get":

<a class="btn btn-primary pull-left dialog"
       data-ajax-method="get"
       href="<?= $this->url('pages/default', ['controller' => 'management', 'action' => 'create']) ?>">Create</a>

<a class="btn btn-primary btn-xs dialog" data-ajax-method="get" 
  href="<?=
    $this->url('pages/default',
       [
          'controller' => 'management',
          'action' => 'edit',
          'id' => $row['id']
       ])
?>">Edit</a>

Implemented ability to delete using ajax, this feature enabled by using class="ajax" and set method data-ajax-method="delete", also you can use class="confirm" to get confirm dialog:

<a class="btn btn-danger btn-xs confirm ajax" data-ajax-method="delete" 
  href="<?=
    $this->url('options/default',
       [
          'controller' => 'management',
          'action' => 'delete',          
          'id' => $row['id']
       ])
?>">Delete</a>

Clone this wiki locally