public function testSimpleUpdate() { $qb = QueryBuilder::update('test', ['id' => 1, 'value' => 'foo']); list($sql, $data) = $qb->resolve(); $this->assertEquals('UPDATE `test` SET `id` = ?, `value` = ?', $sql); $this->assertEquals(2, count($data)); $this->assertEquals(1, $data[0]); $this->assertEquals('foo', $data[1]); }
protected function buildQuery(&$query) { // [FIXME] [NikB] Why did I split this back out to update/insert rather than replace? // Log says "Fixed major overwriting problem in commit()" but what was getting overwritten? // Insert/Update the data, and store the insert id into a variable if ($this->__delete) { $q = QueryBuilder::delete($this->__table, ['id' => $this->__data['id']]); $query->sql($q); } elseif ($this->__new) { $q = QueryBuilder::insert($this->__table, $this->__data); $query->sql($q)->sql("SELECT last_insert_id() into @id"); } else { $q = QueryBuilder::update($this->__table, $this->__data)->where(['id' => $this->__data['id']]); $query->sql($q)->sql("SELECT " . $this->__data['id'] . " into @id"); } if (!$this->__delete) { $origin_id = new SqlString('@id'); // Foreign tables foreach ($this->__external as $property_name => $value) { // Skip property if this isn't an M-M table (M-1 and 1-M tables are dealt with in other ways) if (!($pivot = $this->__model['many-to-many'][$property_name])) { continue; } // We can only do updates support simple connection access for 2 key pivots. if (count($pivot['connections']) != 1) { continue; } // Get the table name of the pivot table for this property $table = Schema::underscoreCase($pivot['pivot']); // Clear out any existing data for this object - this is safe because we are in an atomic transaction. $query->sql("Delete from {$table} where {$pivot['id']} = @id"); // Loops through the list of objects to link to this table foreach ($value as $object) { $newdata = [$pivot['id'] => $origin_id, $pivot['connections'][0]['column'] => $object->id]; $query->sql(QueryBuilder::insert($table, $newdata)); } } } }