PHP code example of sp4ike / phalcon4.1-datatables

1. Go to this page and download the library: Download sp4ike/phalcon4.1-datatables library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sp4ike / phalcon4.1-datatables example snippets



use \DataTables\DataTable;

class TestController extends \Phalcon\Mvc\Controller {
    public function indexAction() {
        if ($this->request->isAjax()) {
          $builder = $this->modelsManager->createBuilder()
                          ->columns('id, name, email, balance')
                          ->from('Example\Models\User');

          $dataTables = new DataTable();
          $dataTables->fromBuilder($builder)->sendResponse();
        }
    }
}


use \DataTables\DataTable;

class TestController extends \Phalcon\Mvc\Controller {
    public function indexAction() {
        if ($this->request->isAjax()) {
          $resultset  = $this->modelsManager->createQuery("SELECT * FROM \Example\Models\User")
                             ->execute();

          $dataTables = new DataTable();
          $dataTables->fromResultSet($resultset)->sendResponse();
        }
    }
}


use \DataTables\DataTable;

class TestController extends \Phalcon\Mvc\Controller {
    public function indexAction() {
        if ($this->request->isAjax()) {
          $array  = $this->modelsManager->createQuery("SELECT * FROM \Example\Models\User")
                             ->execute()->toArray();

          $dataTables = new DataTable();
          $dataTables->fromArray($array)->sendResponse();
        }
    }
}


use \DataTables\DataTable;

class TestController extends \Phalcon\Mvc\Controller {
    public function indexAction() {
        if ($this->request->isAjax()) {
          $builder = $this->modelsManager->createBuilder()
                          ->columns('u.id, u.name, u.email, u.name as role_name')
                          ->addFrom('Example\Models\User', 'u')
                          ->addFrom('Example\Models\Role', 'r')
                          ->where('u.role_id = r.id')
           
          $dataTables = new DataTable();
          $dataTables->fromBuilder($builder)->sendResponse();
          
           // or pass an array of columns to the builder
           $columns = ['u.id', 'u.name', 'u.email', ['u.name', 'alias' => 'role_name']];
           $dataTables = new DataTable();
           $dataTables->fromBuilder($builder, $columns)->sendResponse();
        }
    }
}


use \DataTables\DataTable;

class TestController extends \Phalcon\Mvc\Controller {
    public function exportAction($filters, $type) {
          $builder = $this->modelsManager->createBuilder()
                          ->columns('id, name, email, balance')
                          ->from('Example\Models\User');

          $dataTables = new DataTable();
          $dataTables->fromBuilder($builder)->exportResponse($type);
    }
}


/**
* @property integer id
* @property string name
* @property string email
* @property float balance
* @property integer role_id
*/
class User extends \Phalcon\Mvc\Model {
}

/**
* @property integer id
* @property string name
*/
class Role extends \Phalcon\Mvc\Model {
}