/** * Commit all deferred bindings to this model. */ public function commitDeferred($sessionKey) { if (!strlen($sessionKey)) { return; } $bindings = DeferredBindingModel::where('master_type', get_class($this))->where('session_key', $sessionKey)->get(); foreach ($bindings as $binding) { if (!($relationName = $binding->master_field)) { continue; } if (!$this->isDeferrable($relationName)) { continue; } /* * Find the slave model */ $slaveClass = $binding->slave_type; $slaveModel = new $slaveClass(); $slaveModel = $slaveModel->find($binding->slave_id); if (!$slaveModel) { continue; } /* * Bind/Unbind the relationship, save the related model with any * deferred bindings it might have and delete the binding action */ $relationObj = $this->{$relationName}(); if ($binding->is_bind) { $relationObj->add($slaveModel); } else { $relationObj->remove($slaveModel); } $binding->delete(); } }
/** * Rollback */ public function down() { DB::transaction(function () { // Clean up all current bindings \October\Rain\Database\Models\DeferredBinding::cleanUp(0); Schema::table('cosmicradiotv_podcast_releases', function (Blueprint $table) { $table->unsignedInteger('episode_id')->change(); }); }); }
public function testCommitBinding() { $sessionKey = uniqid('session_key', true); DeferredBinding::truncate(); Model::unguard(); $author = Author::make(['name' => 'Stevie']); $post = Post::create(['title' => "First post"]); Model::reguard(); $author->posts()->add($post, $sessionKey); $this->assertEquals(1, DeferredBinding::count()); $author->commitDeferred($sessionKey); $this->assertEquals(0, DeferredBinding::count()); }
/** * Returns any outstanding binding records for this model. * @return October\Rain\Database\Collection */ protected function getDeferredBindingRecords($sessionKey, $force = false) { if ($this->deferredBindingCache !== null && !$force) { return $this->deferredBindingCache; } return $this->deferredBindingCache = DeferredBindingModel::make()->where('master_type', get_class($this))->where('session_key', $sessionKey)->get(); }