Example #1
0
 public function testIt()
 {
     $model = new Kwf_Model_Proxy(array('proxyModel' => new Kwf_Model_FnF(), 'exprs' => array('foo' => new Kwf_Model_Select_Expr_String('bar'))));
     $row = $model->createRow();
     $row->xy = 'xy';
     $row->save();
     $this->assertEquals('bar', $row->foo);
 }
 public function testToArrayProxy()
 {
     $fnf = new Kwf_Model_FnF(array('uniqueIdentifier' => 'unique', 'columns' => array('id', 'firstname', 'timefield'), 'uniqueColumns' => array('id'), 'data' => array(array('id' => 1, 'firstname' => 'Max', 'timefield' => '2008-06-09 00:00:00'))));
     $proxy = new Kwf_Model_Proxy(array('proxyModel' => $fnf));
     $result = $proxy->getRow(1)->toArray();
     $this->assertEquals(1, $result['id']);
     $this->assertEquals('Max', $result['firstname']);
     $this->assertEquals('2008-06-09 00:00:00', $result['timefield']);
 }
Example #3
0
 public function testWithProxy()
 {
     $fnf = new Kwf_Model_FnF(array('columns' => array('id', 'foo1', 'data')));
     $model = new Kwf_Model_Proxy(array('proxyModel' => $fnf, 'siblingModels' => array(new Kwf_Model_Field(array('fieldName' => 'data')))));
     $row = $model->createRow();
     $row->foo1 = 'bar';
     $row->blub = 'bum';
     $row->save();
     $this->assertEquals($fnf->getData(), array(array('id' => 1, 'foo1' => 'bar', 'data' => json_encode(array('blub' => 'bum')))));
 }
 public function __construct()
 {
     $config = array();
     $config['proxyModel'] = 'Kwf_Model_DbWithConnection_SiblingRelationExpr_RelationDbModel';
     $this->_siblingModels[] = new Kwf_Model_DbWithConnection_SiblingRelationExpr_RelationSiblingModel();
     parent::__construct($config);
 }
Example #5
0
 public function __construct($config)
 {
     if (isset($config['proxyModel'])) {
         $config['proxyModel'] = Kwf_Model_Abstract::getInstance($config['proxyModel']);
     }
     if (isset($config['trlModel'])) {
         $this->_trlModel = Kwf_Model_Abstract::getInstance($config['trlModel']);
     }
     parent::__construct($config);
 }
Example #6
0
 protected function _init()
 {
     parent::_init();
     if (!$this->getProxyModel() instanceof Kwf_Model_RowsSubModel_Interface) {
         throw new Kwf_Exception("proxyModel doesn't implement Kwf_Model_RowsSubModel_Interface");
     }
     if ($this->_parentModel) {
         $this->getProxyModel()->setParentModel($this->_parentModel);
     }
 }
 public function testIssetWithProxySiblings()
 {
     $fnf = new Kwf_Model_FnF(array('columns' => array('id', 'name', 'data'), 'data' => array(array('id' => 4, 'name' => 'foo', 'data' => json_encode(array('name1' => 'foo1'))))));
     $model = new Kwf_Model_Proxy(array('proxyModel' => $fnf, 'siblingModels' => array(new Kwf_Model_Field(array('fieldName' => 'data', 'columns' => array('name1'))))));
     $this->assertEquals(array('id', 'name', 'data'), $fnf->getColumns());
     $this->assertTrue($fnf->hasColumn('id'));
     $this->assertTrue($fnf->hasColumn('name'));
     $this->assertFalse($fnf->hasColumn('name1'));
     $this->assertEquals(array('id', 'name', 'data', 'name1'), $model->getColumns());
     $this->assertTrue($model->hasColumn('id'));
     $this->assertTrue($model->hasColumn('name'));
     $this->assertTrue($model->hasColumn('name1'));
     $row = $model->getRow(4);
     // isset
     $this->assertTrue(isset($row->name));
     $this->assertFalse(isset($row->bar));
     $this->assertTrue(isset($row->name1));
     // get
     $this->assertEquals(4, $row->id);
     $this->assertEquals('foo', $row->name);
     $this->assertEquals('foo1', $row->name1);
     // set
     $row->name = 'bar';
     $this->assertEquals('bar', $row->name);
 }
Example #8
0
 public function __construct($config)
 {
     if (isset($config['proxyModel'])) {
         $config['proxyModel'] = Kwf_Model_Abstract::getInstance($config['proxyModel']);
     }
     if (isset($config['trlModel'])) {
         $this->_trlModel = Kwf_Model_Abstract::getInstance($config['trlModel']);
     }
     if (!$this->_trlModel) {
         throw new Kwf_Exception('Kwc_Directories_Item_Directory_Trl needs to be set a child model.');
     }
     parent::__construct($config);
 }
 public function getRows($where = null, $order = null, $limit = null, $start = null)
 {
     if (!is_object($where)) {
         if (is_string($where)) {
             $where = array($where);
         }
         $select = $this->select($where, $order, $limit, $start);
     } else {
         $select = $where;
     }
     if ($select->getPart(Kwf_Model_Select::WHERE_EQUALS) || $select->getPart(Kwf_Model_Select::WHERE_NULL)) {
         $cacheSetting = $this->_getCacheSetting($select);
         if ($cacheSetting) {
             $cacheId = $this->_getCacheId($cacheSetting);
             if (!isset($this->_cacheData[$cacheId])) {
                 if (!($this->_cacheData[$cacheId] = $this->_getCache()->load($cacheId))) {
                     $this->_cacheData[$cacheId] = $this->_getCacheData($cacheSetting);
                     $this->_getCache()->save($this->_cacheData[$cacheId], $cacheId);
                 }
             }
             $whereEquals = $select->getPart(Kwf_Model_Select::WHERE_EQUALS);
             $whereNull = $select->getPart(Kwf_Model_Select::WHERE_NULL);
             $values = array();
             foreach ($cacheSetting['index'] as $value) {
                 if ($whereEquals) {
                     if (isset($whereEquals[$value])) {
                         $values[] = $this->_escapeSearchKeyElement($whereEquals[$value]);
                     }
                 }
                 if ($whereNull) {
                     foreach (array_values($whereNull) as $whereKey => $whereValue) {
                         if ($whereValue == $value) {
                             $values[] = $this->_escapeSearchKeyElement(null);
                         }
                     }
                 }
             }
             $valuesbefore = $values;
             $v = implode($values, '_');
             if (isset($this->_cacheData[$cacheId][$v])) {
                 $data = $this->_cacheData[$cacheId][$v];
             } else {
                 $data = array();
             }
             return new $this->_rowsetClass(array('model' => $this, 'rowClass' => $this->_rowClass, 'cacheData' => $data));
         }
     }
     return parent::getRows($where, $order, $limit, $start);
 }
Example #10
0
 public function __construct(array $config = array())
 {
     if (!isset($config['proxyModel'])) {
         if (isset($config['table'])) {
             $table = $config['table'];
         } else {
             $table = $this->_table;
         }
         if (!$table) {
             throw new Kwf_Exception('You must specify a table (protected _table or config table) or a proxyModel');
         }
         $config['proxyModel'] = new Kwf_Model_Db(array('table' => $table, 'hasDeletedFlag' => $this->_hasDeletedFlag));
     }
     parent::__construct($config);
 }
 /**
  * @param string E-Mail address of user
  */
 public function createUserRow($email)
 {
     $row = parent::createRow(array('email' => $email));
     $this->_resetPermissions($row);
     return $row;
 }
 protected function _init()
 {
     $this->_proxyModel = new Kwf_Model_Mongo_TestModel();
     $this->_dependentModels['Foo'] = new Kwf_Model_Mongo_RowsSubModel(array('parentModel' => $this, 'fieldName' => 'foo', 'referenceMap' => array('Parent' => array('refModelClass' => 'Kwf_Model_Mongo_ChildRowsWithParentExpr_BothProxy_ParentModel', 'column' => 'parent_id'), 'Mongo' => Kwf_Model_RowsSubModel_Interface::SUBMODEL_PARENT), 'exprs' => array('parent_name' => new Kwf_Model_Select_Expr_Parent('Parent', 'name'), 'mongo_name' => new Kwf_Model_Select_Expr_Parent('Mongo', 'name'))));
     parent::_init();
 }
 public function __construct()
 {
     $config['proxyModel'] = new Kwf_Model_FnFFile(array('primaryKey' => 'component_id', 'uniqueIdentifier' => get_class($this) . '-Proxy'));
     parent::__construct($config);
 }
Example #14
0
 public function __construct(array $config = array())
 {
     $config['proxyModel'] = new Kwf_Model_Db(array('table' => Kwf_Model_Abstract::getInstance('Kwf_Model_DbWithConnection_SelectExpr_Model1')->getTableName()));
     $config['exprs'] = array('count_model2' => new Kwf_Model_Select_Expr_Child_Count('Model2'));
     parent::__construct($config);
 }
 public function clearRows()
 {
     parent::clearRows();
     $this->_sourceModel->clearRows();
 }
 protected function _init()
 {
     $this->_proxyModel = new Kwf_Model_FnF(array('uniqueIdentifier' => 'unique', 'columns' => array('id', 'firstname', 'timefield'), 'uniqueColumns' => array('id'), 'data' => array(array('id' => 1, 'firstname' => 'mch', 'timefield' => '1234'))));
     parent::_init();
 }
 protected function _init()
 {
     parent::_init();
     $this->_exprs['child_count'] = new Kwf_Model_Select_Expr_Child_Count('Child');
 }
 public function testDefaultValues()
 {
     $fnf = new Kwf_Model_FnF(array('default' => array('foo' => 'defaultFoo')));
     $proxy = new Kwf_Model_Proxy(array('proxyModel' => $fnf));
     $row = $proxy->createRow();
     $this->assertEquals('defaultFoo', $row->foo);
 }
 protected function _init()
 {
     parent::_init();
     $this->_exprs['parent_name'] = new Kwf_Model_Select_Expr_Parent('Parent', 'name');
 }
Example #20
0
 public function __construct()
 {
     parent::__construct(array('proxyModel' => new Kwf_Model_FnF()));
 }
 public function __construct(array $config = array())
 {
     $config['proxyModel'] = 'Kwf_Model_FnF_ProxyFnFExtendedReference_ChildModel';
     parent::__construct($config);
 }
 protected function _init()
 {
     parent::_init();
     $this->_referenceMap = array('Parent' => 'parent_id->Kwf_Model_DbWithConnection_ParentExprFieldProxyReference_ParentModel');
     $this->_exprs['foo'] = new Kwf_Model_Select_Expr_Parent('Parent', 'foo');
 }
 public function __construct()
 {
     $config = array('proxyModel' => 'Kwf_Model_Events_ProxyReFire_ProxyModel');
     parent::__construct($config);
 }
 public function getEventSubscribers()
 {
     $ret = parent::getEventSubscribers();
     $ret[] = Kwf_Model_EventSubscriber::getInstance('Kwf_Model_RowCache_Events', array('modelFactoryConfig' => $this->getFactoryConfig()));
     return $ret;
 }
Example #25
0
 public function testDirtyColumnsWithProxy()
 {
     $model = new Kwf_Model_Proxy(array('proxyModel' => new Kwf_Model_Db(array('table' => $this->_tableName))));
     $row = $model->getRow(1);
     $this->assertEquals($row->getDirtyColumns(), array());
     $this->assertEquals($row->isDirty(), false);
     $this->assertEquals($row->getCleanValue('test1'), 'foo');
     $row->test1 = 'blubb';
     $this->assertEquals($row->getDirtyColumns(), array('test1'));
     $this->assertEquals($row->isDirty(), true);
     $this->assertEquals($row->getCleanValue('test1'), 'foo');
 }
 protected function _init()
 {
     parent::_init();
     $this->_exprs['sum_foo'] = new Kwf_Model_Select_Expr_Child('Relation', new Kwf_Model_Select_Expr_Sum('foo'));
     $this->_exprs['sum_fooplusone'] = new Kwf_Model_Select_Expr_Child('Relation', new Kwf_Model_Select_Expr_Sum(new Kwf_Model_Select_Expr_Sql('foo+1', array('foo'))));
 }
Example #27
0
 public function __construct(array $config = array())
 {
     $config['proxyModel'] = new Kwf_Model_Db(array('table' => Kwf_Model_Abstract::getInstance('Kwf_Model_DbWithConnection_SelectExpr_Model2')->getTableName()));
     parent::__construct($config);
 }
 private function _synchronize($overrideMaxSyncDelay = self::SYNC_AFTER_DELAY)
 {
     $select = $this->_getSynchronizeVars($overrideMaxSyncDelay);
     if ($select['type'] !== self::SYNC_SELECT_TYPE_NOSYNC) {
         $start = microtime(true);
         $this->_beforeSynchronize();
         // it's possible to use $this->getProxyModel()->copyDataFromModel()
         // but if < 20 rows are copied, array is faster than sql or csv
         $format = null;
         if ($select['type'] === self::SYNC_SELECT_TYPE_SELECT) {
             if (in_array(self::FORMAT_ARRAY, $this->getProxyModel()->getSupportedImportExportFormats()) && in_array(self::FORMAT_ARRAY, $this->getSourceModel()->getSupportedImportExportFormats())) {
                 $format = self::FORMAT_ARRAY;
             }
         }
         if (!$format) {
             $format = self::_optimalImportExportFormat($this->getProxyModel(), $this->getSourceModel());
         }
         $options = array();
         $data = $this->getSourceModel()->export($format, $select['select']);
         $exportTime = microtime(true) - $start;
         $start = microtime(true);
         if ($select['type'] === self::SYNC_SELECT_TYPE_ALL && $this->_truncateBeforeFullImport) {
             $this->getProxyModel()->deleteRows($this->getProxyModel()->select());
         } else {
             $options['replace'] = true;
         }
         if ($data) {
             if ($this->_callObserverForRowUpdates) {
                 $pk = $this->getProxyModel()->getPrimaryKey();
                 $s = $this->getProxyModel()->select()->order($pk, 'DESC')->limit(1);
                 $maxRow = $this->getProxyModel()->getRow($s);
             }
             $this->getProxyModel()->import($format, $data, $options);
             if ($this->_callObserverForRowUpdates) {
                 foreach (parent::getRows($select['select']) as $row) {
                     if (!$maxRow || $row->{$pk} > $maxRow->{$pk}) {
                         $this->_observedRows['insert'][] = $row;
                     } else {
                         $this->_observedRows['update'][] = $row;
                     }
                 }
             }
         }
         $importTime = microtime(true) - $start;
         $tableName = '';
         if ($this->getProxyModel() instanceof Kwf_Model_Db) {
             $tableName = $this->getProxyModel()->getTableName();
         }
         $msg = date('Y-m-d H:i:s') . ' ' . str_replace('cache_', '', $tableName) . ' ' . $format;
         if (is_array($data)) {
             $msg .= " " . count($data) . " entries";
         } else {
             if (is_string($data)) {
                 $msg .= " " . strlen($data) . " bytes";
             }
         }
         $msg .= ' export: ' . round($exportTime, 2) . 's';
         $msg .= ' import: ' . round($importTime, 2) . 's';
         //$msg .= ' SELECT: '.str_replace("\n", " ", print_r($select, true));
         file_put_contents('log/mirrorcache', $msg . "\n", FILE_APPEND);
         return true;
     }
     return false;
 }