dkanavis

CI ORM CRUD

Jan 23rd, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.33 KB | None | 0 0
  1. /** MODEL **/
  2. include(APPPATH.DIRECTORY_SEPARATOR.'core'.DIRECTORY_SEPARATOR.'API_ORM_Model.php');
  3.  
  4. // Tokens model
  5.  
  6. class Tokens_model extends API_ORM_Model {
  7.     public function __construct() {
  8.         parent::__construct();
  9.         $this->load->database();
  10.  
  11.         $this->table = 'tokens';
  12.  
  13.         $this->fields = [
  14.             'id' => new ORM_Field_Int([
  15.                 'pkey' => true,
  16.                 'auto' => true
  17.             ]),
  18.             'name' => new ORM_Field_String([
  19.                 'label' => 'Name',
  20.                 'required' => true,
  21.                 'trim' => true,
  22.                 'unique' => true,
  23.             ]),
  24.             'host' => new ORM_Field_String([
  25.                 'label' => 'Host',
  26.                 'trim' => true,
  27.                 'required' => true,
  28.                 'validators' => function () {
  29.                     return preg_match('/^'.RE_HOST.'$/', $this->val)? True : 'Wrong host format';
  30.                 }
  31.             ]),
  32.             'token' => new ORM_Field_String([
  33.                 'label' => 'Token',
  34.                 'generated' => true,
  35.                 'generator' => function () {
  36.                     return hash('sha256', time().rand(1, 10000000000));
  37.                 }
  38.             ])
  39.         ];
  40.     }
  41. }
  42.  
  43.  
  44. /** CONTROLLER **/
  45. include(APPPATH.DIRECTORY_SEPARATOR.'core'.DIRECTORY_SEPARATOR.'API_CRUD_Controller.php');
  46.  
  47. // Tokens controller
  48.  
  49. class Tokens extends API_CRUD_Controller {
  50.  
  51.     public function __construct() {
  52.         parent::__construct();
  53.  
  54.         $this->path = [
  55.             ['Tokens', '/tokens']
  56.         ];
  57.         $this->model('tokens_model');
  58.     }
  59.  
  60.     public function index() {
  61.         $this->grid();
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment