NokitaKaze

SModelStream

Apr 18th, 2016
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.62 KB | None | 0 0
  1. <?php
  2. /**
  3.  *
  4. CREATE TABLE `ascet_test` (
  5.   `id` int(11) NOT NULL AUTO_INCREMENT,
  6.   `i1` int(11) DEFAULT NULL,
  7.   `i2` int(11) NOT NULL,
  8.   `t1` text COLLATE utf8_unicode_ci,
  9.   `t2` text COLLATE utf8_unicode_ci NOT NULL,
  10.   `date` date DEFAULT NULL,
  11.   `datetime` datetime DEFAULT NULL,
  12.   `timestamp` timestamp NULL DEFAULT NULL,
  13.   PRIMARY KEY (`id`)
  14. ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  15. INSERT INTO `ascet_test` VALUES ('1', null, '817', null, '', null, null, '2015-10-19 16:55:27'), ('3', null, '789', null, 'nyn', '2015-10-15', null, null), ('6', null, '0', null, '', '2015-10-19', '2015-10-19 18:43:40', '2015-10-19 18:43:40'), ('7', null, '0', null, '', '2015-10-19', '2015-10-19 19:10:17', '2015-10-19 19:10:17');
  16.  */
  17.  
  18.     /**
  19.      * @var string       $sad_root
  20.      *
  21.      * @property integer $seed
  22.      */
  23.     class TestModel extends SModel {
  24.         private $_random_seed;
  25.  
  26.         static function get_attributes_list_overload() {
  27.             return [
  28.                 'seed' => [
  29.                     'type' => 'integer',
  30.                 ],
  31.             ];
  32.         }
  33.  
  34.         function __construct($scenario = 'create') {
  35.             parent::__construct($scenario);
  36.             $this->_random_seed = mt_rand(0, 1000000);
  37.         }
  38.  
  39.         /**
  40.          * @return string
  41.          */
  42.         static function getClass_name() {
  43.             return __CLASS__;
  44.         }
  45.  
  46.         function get_setting() {
  47.             return [$this->seed, $this->_random_seed];
  48.         }
  49.     }
  50.  
  51.     class FooBar {
  52.         private $_models = [];
  53.  
  54.         function __construct() {
  55.             $count = mt_rand(50, 100);
  56.             for ($i = 0; $i < $count; $i++) {
  57.                 $model = new TestModel();
  58.                 $model->seed = mt_rand(0, 1000000);
  59.                 $this->_models[] = $model;
  60.             }
  61.         }
  62.  
  63.         /**
  64.          * @return SModelStream
  65.          */
  66.         function get_stream() {
  67.             $fenrir = $this;
  68.             $get_models = function ($offset, $count, $order, $select) use ($fenrir) {
  69.                 $ret = [];
  70.                 for ($real_offset = $offset; $real_offset < min($offset + $count, count($fenrir->_models)); $real_offset++) {
  71.                     $ret[] = $fenrir->_models[$real_offset];
  72.                 }
  73.  
  74.                 return $ret;
  75.             };
  76.             $get_count = function () use ($fenrir) {
  77.                 return count($fenrir->_models);
  78.             };
  79.  
  80.             $stream = new SModelStream([
  81.                 'get_models' => $get_models,
  82.                 'get_count' => $get_count,
  83.             ]);
  84.  
  85.             return $stream;
  86.         }
  87.     }
  88.  
  89.     /**
  90.      * Class TestModel
  91.      */
  92.     class TestRecord extends SDBRecord {
  93.         /**
  94.          * @return string
  95.          */
  96.         static function getTable_name() {
  97.             return 'ascet_test';
  98.         }
  99.  
  100.         /**
  101.          * @return string
  102.          */
  103.         static function getClass_name() {
  104.             return __CLASS__;
  105.         }
  106.     }
  107.  
  108.     echo '<h2>Foo bar</h2>';
  109.     $bar = new FooBar();
  110.     $stream = $bar->get_stream();
  111.     unset($bar);
  112.  
  113.     /**
  114.      * @var TestModel[] $chunk
  115.      * @var TestModel   $model
  116.      */
  117.     while ($model = $stream->get_next_model()) {
  118.         var_dump($model->get_setting());
  119.     }
  120.  
  121.     echo '<h2>Test Record</h2>';
  122.     $record_search = new TestRecord('search');
  123.     $criteria = $record_search->export_search_sdbcriteria();
  124.     $stream = $record_search::export_model_stream($criteria);
  125.     while ($record = $stream->get_next_model()) {
  126.         var_dump($record->getAttributes());
  127.     }
  128.  
  129.  
  130. ?>
Add Comment
Please, Sign In to add comment