Пример #1
0
 public function pushM2M($col, $target)
 {
     $key = 'join_' . $this->joinCount;
     $key2 = 'join_' . ++$this->joinCount;
     $key2a = $key2 . 'a';
     $this->route[] = 'Join `' . Schema::underscoreCase($target['pivot']) . '` as ' . $key2a . ' on ' . $key . '.id = ' . $key2a . '.`' . $target['id'] . '` ';
     $this->route[] = 'Join `' . Schema::underscoreCase($target['connections'][0]['table']) . '` as ' . $key2 . ' on ' . $key2 . '.id = ' . $key2a . '.`' . $target['connections'][0]['column'] . '` ';
     $this->next(Schema::underscoreCase($target['connections'][0]['table']));
     $this->multiresult = true;
     return $this;
 }
Пример #2
0
 public final function dataRefresh()
 {
     list($data) = Model::factoryData(['id' => $this->id], $this->table, $this->database);
     // Database data object unique to this object
     $this->_data = new Data($data, $this->table, Schema::get($this->database));
     Data::updateCache($this->_data);
     // Call replacement constructor after storing in the cache list (to prevent recursion)
     $this->dataClearCache();
     $this->_init();
     return $this;
 }
Пример #3
0
 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));
             }
         }
     }
 }