Lançado Adianti Framework 7.6!
Clique aqui para saber mais
menu

Adianti Solutions

API

Adianti, Framework, PHP, MVC, Active record, Front controller, IDE, RAD, Web, multiplataforma, geração de código, desenvolvimento rápido, relatórios, formulários, listagens, datagrids, gráficos, banco de dados, padrões de projeto, design patterns API do Adianti Framework.
API Docs
code
Selecione a classe

Source for file AdiantiRecordService.php

Documentation is available at AdiantiRecordService.php

  1. <?php
  2. namespace Adianti\Service;
  3.  
  4. use Adianti\Database\TTransaction;
  5. use Adianti\Database\TRepository;
  6. use Adianti\Database\TCriteria;
  7. use Adianti\Database\TFilter;
  8.  
  9. /**
  10.  * Record rest service
  11.  *
  12.  * @version    7.4
  13.  * @package    service
  14.  * @author     Pablo Dall'Oglio
  15.  * @copyright  Copyright (c) 2006 Adianti Solutions Ltd. (http://www.adianti.com.br)
  16.  * @license    http://www.adianti.com.br/framework-license
  17.  */
  18. {
  19.     /**
  20.      * Find a Active Record and returns it
  21.      * @return The Active Record itself as array
  22.      * @param $param HTTP parameter
  23.      */
  24.     public function load($param)
  25.     {
  26.         $database     static::DATABASE;
  27.         $activeRecord static::ACTIVE_RECORD;
  28.         
  29.         TTransaction::open($database);
  30.         
  31.         $object new $activeRecord($param['id']FALSE);
  32.         
  33.         TTransaction::close();
  34.         $attributes defined('static::ATTRIBUTES'static::ATTRIBUTES null;
  35.         return $object->toArray$attributes );
  36.     }
  37.     
  38.     /**
  39.      * Delete an Active Record object from the database
  40.      * @param [$id]     HTTP parameter
  41.      */
  42.     public function delete($param)
  43.     {
  44.         $database     static::DATABASE;
  45.         $activeRecord static::ACTIVE_RECORD;
  46.         
  47.         TTransaction::open($database);
  48.         
  49.         $object new $activeRecord($param['id']);
  50.         $object->delete();
  51.         
  52.         TTransaction::close();
  53.         return;
  54.     }
  55.     
  56.     /**
  57.      * Store the objects into the database
  58.      * @param $param HTTP parameter
  59.      */
  60.     public function store($param)
  61.     {
  62.         $database     static::DATABASE;
  63.         $activeRecord static::ACTIVE_RECORD;
  64.         
  65.         TTransaction::open($database);
  66.         
  67.         $object new $activeRecord;
  68.         $pk $object->getPrimaryKey();
  69.         $param['data'][$pk$param['data']['id'?? NULL;
  70.         $object->fromArray(array) $param['data']);
  71.         $object->store();
  72.         
  73.         TTransaction::close();
  74.         return $object->toArray();
  75.     }
  76.     
  77.     /**
  78.      * List the Active Records by the filter
  79.      * @return The Active Record list as array
  80.      * @param $param HTTP parameter
  81.      */
  82.     public function loadAll($param)
  83.     {
  84.         $database     static::DATABASE;
  85.         $activeRecord static::ACTIVE_RECORD;
  86.         
  87.         TTransaction::open($database);
  88.         
  89.         $criteria new TCriteria;
  90.         if (isset($param['offset']))
  91.         {
  92.             $criteria->setProperty('offset'$param['offset']);
  93.         }
  94.         if (isset($param['limit']))
  95.         {
  96.             $criteria->setProperty('limit'$param['limit']);
  97.         }
  98.         if (isset($param['order']))
  99.         {
  100.             $criteria->setProperty('order'$param['order']);
  101.         }
  102.         if (isset($param['direction']))
  103.         {
  104.             $criteria->setProperty('direction'$param['direction']);
  105.         }
  106.         if (isset($param['filters']))
  107.         {
  108.             foreach ($param['filters'as $filter)
  109.             {
  110.                 $criteria->add(new TFilter($filter[0]$filter[1]$filter[2]));
  111.             }
  112.         }
  113.         
  114.         $repository new TRepository($activeRecord);
  115.         $objects $repository->load($criteriaFALSE);
  116.         $attributes defined('static::ATTRIBUTES'static::ATTRIBUTES null;
  117.         
  118.         $return [];
  119.         if ($objects)
  120.         {
  121.             foreach ($objects as $object)
  122.             {
  123.                 $return[$object->toArray$attributes );
  124.             }
  125.         }
  126.         TTransaction::close();
  127.         return $return;
  128.     }
  129.     
  130.     /**
  131.      * Delete the Active Records by the filter
  132.      * @return The result of operation
  133.      * @param $param HTTP parameter
  134.      */
  135.     public function deleteAll($param)
  136.     {
  137.         $database     static::DATABASE;
  138.         $activeRecord static::ACTIVE_RECORD;
  139.         
  140.         TTransaction::open($database);
  141.         
  142.         $criteria new TCriteria;
  143.         if (isset($param['filters']))
  144.         {
  145.             foreach ($param['filters'as $filter)
  146.             {
  147.                 $criteria->add(new TFilter($filter[0]$filter[1]$filter[2]));
  148.             }
  149.         }
  150.         
  151.         $repository new TRepository($activeRecord);
  152.         $return $repository->delete($criteria);
  153.         TTransaction::close();
  154.         return $return;
  155.     }
  156.  
  157.     /**
  158.      * Find the count Records by the filter
  159.      * @return The Active Record list as array
  160.      * @param $param HTTP parameter
  161.      */
  162.     public function countAll($param)
  163.     {
  164.         $database     static::DATABASE;
  165.         $activeRecord static::ACTIVE_RECORD;
  166.  
  167.         TTransaction::open($database);
  168.  
  169.         $criteria new TCriteria;
  170.         if (isset($param['offset']))
  171.         {
  172.             $criteria->setProperty('offset'$param['offset']);
  173.         }
  174.         if (isset($param['limit']))
  175.         {
  176.             $criteria->setProperty('limit'$param['limit']);
  177.         }
  178.         if (isset($param['order']))
  179.         {
  180.             $criteria->setProperty('order'$param['order']);
  181.         }
  182.         if (isset($param['direction']))
  183.         {
  184.             $criteria->setProperty('direction'$param['direction']);
  185.         }
  186.         if (isset($param['filters']))
  187.         {
  188.             foreach ($param['filters'as $filter)
  189.             {
  190.                 $criteria->add(new TFilter($filter[0]$filter[1]$filter[2]));
  191.             }
  192.         }
  193.  
  194.         $repository new TRepository($activeRecord);
  195.         $count $repository->count($criteriaFALSE);
  196.  
  197.         TTransaction::close();
  198.         return $count;
  199.     }
  200.     
  201.     /**
  202.      * Handle HTTP Request and dispatch
  203.      * @param $param HTTP POST and php input vars
  204.      */
  205.     public function handle($param)
  206.     {
  207.         $method strtoupper($_SERVER['REQUEST_METHOD']);
  208.         
  209.         unset($param['class']);
  210.         unset($param['method']);
  211.         $param['data'$param;
  212.         
  213.         switch$method )
  214.         {
  215.             case 'GET':
  216.                 if (!empty($param['id']))
  217.                 {
  218.                     return self::load($param);
  219.                 }
  220.                 else
  221.                 {
  222.                     return self::loadAll($param);
  223.                 }
  224.                 break;
  225.             case 'POST':
  226.                 return self::store($param);
  227.                 break;
  228.             case 'PUT':
  229.                 return self::store($param);
  230.                 break;        
  231.             case 'DELETE':
  232.                 if (!empty($param['id']))
  233.                 {
  234.                     return self::delete($param);
  235.                 }
  236.                 else
  237.                 {
  238.                     return self::deleteAll($param);
  239.                 }
  240.                 break;
  241.         }
  242.     }
  243. }