NokitaKaze icon

SModelStream

NokitaKaze | PRO | 04/18/16 06:01:36 PM UTC | 0 ⭐ | 304 👁️ | Never ⏰ | []
PHP |

3.62 KB

|

None

|

0 👍

/

0 👎

<?php
/**
 *
CREATE TABLE `ascet_test` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `i1` int(11) DEFAULT NULL,
  `i2` int(11) NOT NULL,
  `t1` text COLLATE utf8_unicode_ci,
  `t2` text COLLATE utf8_unicode_ci NOT NULL,
  `date` date DEFAULT NULL,
  `datetime` datetime DEFAULT NULL,
  `timestamp` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
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');
 */
 
    /**
     * @var string       $sad_root
     *
     * @property integer $seed
     */
    class TestModel extends SModel {
        private $_random_seed;
 
        static function get_attributes_list_overload() {
            return [
                'seed' => [
                    'type' => 'integer',
                ],
            ];
        }
 
        function __construct($scenario = 'create') {
            parent::__construct($scenario);
            $this->_random_seed = mt_rand(0, 1000000);
        }
 
        /**
         * @return string
         */
        static function getClass_name() {
            return __CLASS__;
        }
 
        function get_setting() {
            return [$this->seed, $this->_random_seed];
        }
    }
 
    class FooBar {
        private $_models = [];
 
        function __construct() {
            $count = mt_rand(50, 100);
            for ($i = 0; $i < $count; $i++) {
                $model = new TestModel();
                $model->seed = mt_rand(0, 1000000);
                $this->_models[] = $model;
            }
        }
 
        /**
         * @return SModelStream
         */
        function get_stream() {
            $fenrir = $this;
            $get_models = function ($offset, $count, $order, $select) use ($fenrir) {
                $ret = [];
                for ($real_offset = $offset; $real_offset < min($offset + $count, count($fenrir->_models)); $real_offset++) {
                    $ret[] = $fenrir->_models[$real_offset];
                }
 
                return $ret;
            };
            $get_count = function () use ($fenrir) {
                return count($fenrir->_models);
            };
 
            $stream = new SModelStream([
                'get_models' => $get_models,
                'get_count' => $get_count,
            ]);
 
            return $stream;
        }
    }
 
    /**
     * Class TestModel
     */
    class TestRecord extends SDBRecord {
        /**
         * @return string
         */
        static function getTable_name() {
            return 'ascet_test';
        }
 
        /**
         * @return string
         */
        static function getClass_name() {
            return __CLASS__;
        }
    }
 
    echo '<h2>Foo bar</h2>';
    $bar = new FooBar();
    $stream = $bar->get_stream();
    unset($bar);
 
    /**
     * @var TestModel[] $chunk
     * @var TestModel   $model
     */
    while ($model = $stream->get_next_model()) {
        var_dump($model->get_setting());
    }
 
    echo '<h2>Test Record</h2>';
    $record_search = new TestRecord('search');
    $criteria = $record_search->export_search_sdbcriteria();
    $stream = $record_search::export_model_stream($criteria);
    while ($record = $stream->get_next_model()) {
        var_dump($record->getAttributes());
    }
 
 
?>

Comments