public function __construct($conf) { if (!isset($conf['tableClass'])) { throw new Exception('new ResultSet 時必需要指定 tableClass'); } $this->_tableClass = $conf['tableClass']; $this->_table = Pix_Table::getTable($this->_tableClass); if (isset($conf['belongs_row'])) { $this->_belongs_row = $conf['belongs_row']; } $this->_search_object = Pix_Table_Search::factory(); }
/** * 如果在 __construct 呼叫 $this->xxx() 會 failed 的測試 * @expectedException Pix_Table_Exception */ public function testGetTableConstructFailed() { Pix_Table::getTable('Pix_Table_TableTest_TableContstructFailed'); }
/** * declare a new empty Pix Table * * @static * @access public * @return Pix_Table */ public static function newEmptyTable($table_name = null) { if (is_null($table_name)) { while (true) { $table_name = 'Pix_Table_EmptyTable_' . crc32(uniqid()); if (!class_exists($table_name, false)) { break; } } } if (class_exists($table_name, false)) { throw new Pix_Table_Exception("newEmptyTable failed, {$table_name} is existed."); } // XXX: In Pix Table, a Table is mapping to a PHP class. If you want dynamically new a table, // you must declare a new Pix_Table class. // class_alias doesn't worked here. eval("class {$table_name} extends Pix_Table {}"); return Pix_Table::getTable($table_name); }
public function setRelation($name, $value) { $table = $this->getTable(); // 如果是 has_many 不給 set if ($table->_relations[$name]['rel'] == 'has_many') { throw new Pix_Table_Exception("has_many 不能夠 ->{$name} = \$value; 請改用 ->{$name}[] = \$value"); } elseif ('has_one' == $table->_relations[$name]['rel'] || 'belongs_to' == $table->_relations[$name]['rel']) { $this->_relation_data[$name] = null; $type = $table->_relations[$name]['type']; $type_table = Pix_Table::getTable($type); $foreign_keys = $table->getRelationForeignKeys($name); if (is_scalar($value)) { $value = array($value); } if ($value instanceof Pix_Table_Row and $value->getTableClass() == $type) { $value = $value->getPrimaryValues(); } elseif ($value == null) { $value = array(null); } elseif (!is_array($value)) { throw new Pix_Table_Exception(' = 右邊的東西只能是 Row 或是 PK' . $type . get_class($value)); } if (count($value) != count($foreign_keys)) { throw new Pix_Table_Exception('參數不夠'); } $column_values = array_combine($foreign_keys, $value); foreach ($column_values as $column => $value) { $this->{$column} = $value; } return; } else { throw new Pix_Table_Exception('relation 的 rel 只能是 has_many, has_one 或是 belongs_to 喔'); } }
public function testUpdate() { $db = $this->getMock('Pix_Table_Db_Adapter_Abstract', array('updateOne')); $table = Pix_Table::getTable('Pix_Table_TableRowTest_Table'); $row = new Pix_Table_Row(array('tableClass' => 'Pix_Table_TableRowTest_Table', 'data' => array('t1_id' => 1005, 'value' => 'abc'))); // array $db->expects($this->once())->method('updateOne')->with($this->isInstanceOf(get_class($row)), array('value' => '9'))->will($this->returnValue(null)); Pix_Table_TableRowTest_Table::setDb($db); $row->update(array('value' => '9')); $this->assertEquals($row->value, '9'); // string $db = $this->getMock('Pix_Table_Db_Adapter_Abstract', array('updateOne', 'fetchOne', 'support')); $db->expects($this->any())->method('support')->with($this->logicalOr('immediate_consistency', 'force_master'))->will($this->returnValue(true)); $db->expects($this->once())->method('updateOne')->with($this->isInstanceOf(get_class($row)), 'value = value + 1')->will($this->returnValue(null)); $db->expects($this->once())->method('fetchOne')->with($this->isInstanceOf('Pix_Table_TableRowTest_Table', array(1005)))->will($this->returnValue(array('t1_id' => 1005, 'value' => '10'))); Pix_Table_TableRowTest_Table::setDb($db); $row->update("value = value + 1"); $this->assertEquals($row->value, 10); }
/** * getRelationForeignTable 取得 relation 對應的 table * * @param string $relation relation 名稱 * @static * @access public * @return Pix_Table 對應的 table */ public static function getRelationForeignTable($relation) { $table = self::getTable(); if (!array_key_exists($relation, $table->_relations)) { throw new Pix_Table_Exception("{$table->getClass()} 找不到 {$relation} 這個 relation"); } $relation_data = $table->_relations[$relation]; if (!array_key_exists('type', $relation_data)) { throw new Pix_Table_Exception("{$table->getClass()}->{$relation} 沒有指定 Table Type"); } $table_name = $relation_data['type']; return Pix_Table::getTable($table_name); }
public function testNullOrFalse() { Pix_Table::$_save_memory = false; $cache = new Pix_Table_TableCacheTest_Cache(); Pix_Table::setCache($cache); $table = Pix_Table::getTable('Pix_Table_TableCacheTest_User'); $table->enableTableCache(); // 什麼都沒有是 false $this->assertTrue($table->getRowFromCache(6) === false); // cacheRow null 後是 null (表示被刪除) $table->cacheRow(7, null); $this->assertTrue($table->getRowFromCache(7) === null); // cacheRow false 後是 false $table->cacheRow(8, false); $this->assertTrue($table->getRowFromCache(8) === false); }