Skip to content

Commit

Permalink
pkp#10292 fixed issue when guarded attribute defined
Browse files Browse the repository at this point in the history
  • Loading branch information
touhidurabir committed Nov 15, 2024
1 parent a827536 commit 35d4d65
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 17 deletions.
16 changes: 2 additions & 14 deletions classes/controlledVocab/ControlledVocabEntry.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,11 @@ class ControlledVocabEntry extends Model
*/
protected $primaryKey = 'controlled_vocab_entry_id';

// TODO: Investigate why defining any guarded props causing no data to store in settings table
/**
* @copydoc \Illuminate\Database\Eloquent\Concerns\GuardsAttributes::$guarded
*/
protected $guarded = [
// 'id',
'controlledVocabEntryId',
];

/**
Expand All @@ -66,7 +65,7 @@ protected function casts(): array
{
return [
'controlled_vocab_entry_id' => 'string',
'controlled_vocab_id' => 'int',
'controlled_vocab_id' => 'integer',
'seq' => 'float',
];
}
Expand Down Expand Up @@ -103,17 +102,6 @@ public function getSettings(): array
);
}

/**
* Accessor and Mutator for primary key => id
*/
protected function id(): Attribute
{
return Attribute::make(
get: fn($value, $attributes) => $attributes[$this->primaryKey] ?? null,
set: fn($value) => [$this->primaryKey => $value],
)->shouldCache();
}

/**
* The controlled vocab associated with this controlled vocab entry
*/
Expand Down
46 changes: 43 additions & 3 deletions classes/core/traits/ModelWithSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

namespace PKP\core\traits;

use Eloquence\Behaviours\HasCamelCasing;
use Exception;
use Eloquence\Behaviours\HasCamelCasing;
use Illuminate\Support\Str;
use Illuminate\Database\Eloquent\Casts\Attribute;
use PKP\core\maps\Schema;
use PKP\core\SettingsBuilder;
Expand All @@ -30,6 +31,11 @@ trait ModelWithSettings
{
use HasCamelCasing;

/**
* @see \Illuminate\Database\Eloquent\Concerns\GuardsAttributes::$guardableColumns
*/
protected static $guardableColumns = [];

// The list of attributes associated with the model settings
protected array $settings = [];

Expand All @@ -46,22 +52,25 @@ abstract public function getTable();
/**
* Get settings table name
*/
abstract public function getSettingsTable();
abstract public function getSettingsTable(): string;

/**
* The name of the schema for the Model if exists, null otherwise
*/
abstract public static function getSchemaName(): ?string;

/**
* See Illuminate\Database\Eloquent\Concerns\HasAttributes::mergeCasts()
* @see Illuminate\Database\Eloquent\Concerns\HasAttributes::mergeCasts()
*
* @param array $casts
*
* @return array
*/
abstract protected function ensureCastsAreStringValues($casts);

/**
* @see \Illuminate\Database\Eloquent\Model::__construct()
*/
public function __construct(array $attributes = [])
{
parent::__construct($attributes);
Expand Down Expand Up @@ -188,4 +197,35 @@ protected function id(): Attribute
set: fn ($value) => [$this->primaryKey => $value],
);
}

/**
* @see \Illuminate\Database\Eloquent\Concerns\GuardsAttributes::isGuardableColumn()
*/
protected function isGuardableColumn($key)
{
// Need the snake like to key to check for main table to compare with column listing
$key = Str::snake($key);

if (! isset(static::$guardableColumns[get_class($this)])) {
$columns = $this->getConnection()
->getSchemaBuilder()
->getColumnListing($this->getTable());

if (empty($columns)) {
return true;
}
static::$guardableColumns[get_class($this)] = $columns;
}


$settingsWithMultilingual = array_merge($this->getSettings(), $this->getMultilingualProps());
$camelKey = Str::camel($key);

// Check if this column included in setting and multilingula props and not set to guarded
if (in_array($camelKey, $settingsWithMultilingual) && !in_array($camelKey, $this->getGuarded())) {
return true;
}

return in_array($key, (array)static::$guardableColumns[get_class($this)]);
}
}

0 comments on commit 35d4d65

Please sign in to comment.