public function testSetRelation2() { $table1_row = new Pix_Table_Row(array('tableClass' => 'Pix_Table_TableRelationTest_Table', 'data' => array('t1_id' => 1001, 'value' => 'delete_me'))); $table3_row = new Pix_Table_Row(array('tableClass' => 'Pix_Table_TableRelationTest_Table3', 'data' => array('t3_id' => 4567, 't3_t1id' => 1, 'value' => 'delete_me'))); $db = $this->getMock('Pix_Table_Db_Adapter_Abstract', array('fetchOne', 'updateOne')); $db->expects($this->once())->method('fetchOne')->with($this->isInstanceOf('Pix_Table_TableRelationTest_Table3'), array(4567))->will($this->returnValue(array('t3_id' => 4567, 't3_t1id' => 1, 'value' => 'value'))); $db->expects($this->once())->method('updateOne')->with($this->isInstanceOf('Pix_Table_Row'), array('t1_t3_id' => 4567))->will($this->returnValue(null)); Pix_Table_TableRelationTest_Table::setDb($db); Pix_Table_TableRelationTest_Table3::setDb($db); $table1_row->table3 = $table3_row; $table1_row->save(); $this->assertTrue($table1_row->table3 instanceof Pix_Table_Row); $this->assertEquals($table1_row->table3->getTableClass(), 'Pix_Table_TableRelationTest_Table3'); $this->assertEquals($table1_row->table3->t3_id, 4567); $this->assertEquals($table1_row->t1_t3_id, 4567); }
/** * updateOne 從 db 上更新一個 $row 的 data * * @param Pix_Table_Row $row * @param array|string $data * @access public * @return void */ public function updateOne($row, $data) { $table = $row->getTable(); $sql = 'UPDATE ' . $this->column_quote($table->getTableName()); $sql .= ' SET ' . $this->_get_set_clause($data, $table); $sql .= ' WHERE '; $sql .= $this->_get_where_clause(Pix_Table_Search::factory(array_combine($table->getPrimaryColumns(), $row->getPrimaryValues()), $table)); return $this->query($sql); }
/** * updateOne 從 db 上更新一個 $row 的 data * * @param Pix_Table_Row $row * @param array $data * @access public * @return void */ public function updateOne($row, $data) { if (!is_array($data)) { throw new Pix_Table_Exception('Pix_Table_Db_Adapter_AmazonDynamoDb 只允許提供 update->(array)'); } $table = $row->getTable(); $primary_values = $row->getPrimaryValues(); $primary_keys = $table->getPrimaryColumns(); $get_key = array(); if (count($primary_keys) == 1) { $get_key['HashKeyElement'] = array($this->_getColumnType($table, $primary_keys[0]) => $primary_values[0]); } elseif (count($primary_keys) == 2) { $get_key['HashKeyElement'] = array($this->_getColumnType($table, $primary_keys[0]) => $primary_values[0]); $get_key['RangeKeyElement'] = array($this->_getColumnType($table, $primary_keys[1]) => $primary_values[1]); } else { throw new Pix_Table_Exception("AmazonDynamoDB 只支援最多兩個 Primary Key 的 Table"); } $updates = array(); foreach ($data as $key => $value) { $updates[$key] = array('Action' => AmazonDynamoDB::ACTION_PUT, 'Value' => array($this->_getColumnType($table, $key) => $value)); } $db = $this->_getDb(); $db->update_item(array('TableName' => $table->getTableName(), 'Key' => $get_key, 'AttributeUpdates' => $updates)); }
/** * is_a $object 是否是 $table 類型的 Row * * @param Pix_Table_Row $object * @param string $table * @static * @access public * @return boolean */ public static function is_a($object, $table) { if (!$object instanceof Pix_Table_Row) { return false; } return $object->getTableClass() == $table; }
/** * 測試 equals 是不是傳了不同的 Table * @expectedException Pix_Table_Exception */ public function testEqualsWrongTable() { $row = new Pix_Table_Row(array('tableClass' => 'Pix_Table_TableRowTest_Table', 'data' => array('t1_id' => 1004, 'value' => 'abc'))); $row2 = new Pix_Table_Row(array('tableClass' => 'Pix_Table_TableRowTest_Table2', 'data' => array('t2_id' => 1004, 'value' => 'abc'))); $row->equals($row2); }