コード例 #1
0
 public function testSimpleInsert()
 {
     $qb = QueryBuilder::insert('test', ['id' => 1, 'value' => 'foo']);
     list($sql, $data) = $qb->resolve();
     $this->assertEquals('INSERT INTO `test` SET `id` = ?, `value` = ?', $sql);
     $this->assertEquals(2, count($data));
     $this->assertEquals(1, $data[0]);
     $this->assertEquals('foo', $data[1]);
 }
コード例 #2
0
ファイル: ClosureTree.php プロジェクト: brokencube/automatorm
 public function createInitialClosure()
 {
     $query = new Query(static::getConnection());
     $query->sql(QueryBuilder::insert($this->closureTable, ['parent_id' => $this->id, 'child_id' => $this->id, 'depth' => 0]));
     $query->execute();
 }
コード例 #3
0
ファイル: Data.php プロジェクト: brokencube/automatorm
 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));
             }
         }
     }
 }