Skip to content

Commit

Permalink
Merge pull request #93 from siamak2/master
Browse files Browse the repository at this point in the history
register saved event listener in bootMetable
  • Loading branch information
kodeine committed Mar 11, 2022
2 parents 74d5aab + 358027a commit 15961bf
Showing 1 changed file with 7 additions and 18 deletions.
25 changes: 7 additions & 18 deletions src/Kodeine/Metable/Metable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@

trait Metable
{

// Static property registration sigleton for save observation and slow large set hotfix
public static $_isObserverRegistered;

public static $_columnNames;

/**
Expand Down Expand Up @@ -194,20 +192,6 @@ public function whereMeta($key, $value)
->get();
}

/**
* Trait specific functions
* -------------------------.
*/
protected function setObserver()
{
if(!isset(self::$_isObserverRegistered)) {
$this->saved(function ($model) {
$model->saveMeta();
});
self::$_isObserverRegistered = true;
}
}

protected function getModelStub()
{
// get new meta model instance
Expand Down Expand Up @@ -244,7 +228,6 @@ protected function saveMeta()
protected function getMetaData()
{
if (!isset($this->metaLoaded)) {
$this->setObserver();

if ($this->exists) {
$objects = $this->metas
Expand Down Expand Up @@ -364,6 +347,12 @@ public function hasColumn($column) {
if(empty(self::$_columnNames)) self::$_columnNames = array_map('strtolower',\Schema::connection($this->getConnectionName())->getColumnListing($this->getTable()));
return in_array(strtolower($column), self::$_columnNames);
}

public static function bootMetable(){
static::saved(function ($model) {
$model->saveMeta();
});
}

public function __unset($key)
{
Expand Down

0 comments on commit 15961bf

Please sign in to comment.