Skip to content

Commit

Permalink
Minor cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
hparadiz committed Jul 27, 2023
1 parent ee754cb commit e781d40
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 18 deletions.
14 changes: 10 additions & 4 deletions src/Models/ActiveRecord.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,11 @@ public function getPrimaryKeyValue()
public static function init()
{
$className = get_called_class();

$className::$rootClass = $className::$rootClass ?? $className;
$className::$defaultClass = $className::$defaultClass ?? $className;
$className::$subClasses = $className::$subClasses ?? [$className];

if (empty(static::$_fieldsDefined[$className])) {
static::_defineFields();
static::_initFields();
Expand Down Expand Up @@ -1413,27 +1418,28 @@ protected function _setFieldValue($field, $value)
}

if ($forceDirty || (empty($this->_record[$field]) && isset($value)) || ($this->_record[$field] !== $value)) {
$this->_setDirtyValue($field, $value);
$this->_setValueAndMarkDirty($field, $value, $fieldOptions);
return true;
} else {
return false;
}
}

protected function _setDirtyValue($field, $value)
protected function _setValueAndMarkDirty($field, $value, $fieldOptions)
{
$columnName = static::_cn($field);
if (isset($this->_record[$columnName])) {
$this->_originalValues[$field] = $this->_record[$columnName];
}
$this->_record[$columnName] = $value;
// only set value if this is an attribute mapped field
if (isset($this->_classFields[get_called_class()][$columnName]['attributeField'])) {
if (isset(static::$_classFields[get_called_class()][$columnName]['attributeField'])) {
$this->$columnName = $value;
}
$this->_isDirty = true;

// unset invalidated relationships
// If a model has been modified we should clear the relationship cache
// TODO: this can be smarter by only looking at fields that are used in the relationship configuration
if (!empty($fieldOptions['relationships']) && static::isRelational()) {
foreach ($fieldOptions['relationships'] as $relationship => $isCached) {
if ($isCached) {
Expand Down
6 changes: 3 additions & 3 deletions tests/MockSite/Models/Forum/Category.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ class Category extends \Divergence\Models\Model

public static $indexes = [];

protected $Name;
protected string $Name;

#[Relation(
type:'one-many',
class:Thread::class,
local: 'ID',
foreign: 'CategoryID'
)]
protected $Threads;
protected ?array $Threads;

#[Relation(
type:'one-many',
Expand All @@ -57,7 +57,7 @@ class:Thread::class,
],
order: ['Title'=>'ASC']
)]
protected $ThreadsAlpha;
protected ?array $ThreadsAlpha;

public static function getProtected($field)
{
Expand Down
8 changes: 4 additions & 4 deletions tests/MockSite/Models/Forum/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ class Post extends \Divergence\Models\Model
];

#[Column(type: "clob", required:true, notnull: true)]
protected $Content;
protected string $Content;

#[Column(type: "integer", required:true, notnull: true)]
protected $ThreadID;
protected int $ThreadID;

/*
* The first one is testing the minimal configuration of a one-to-one relationship
Expand All @@ -60,7 +60,7 @@ class Post extends \Divergence\Models\Model
#[Relation(
class:Thread::class,
)]
protected $Thread;
protected ?Thread $Thread;

#[Relation(
type:'one-one',
Expand All @@ -72,5 +72,5 @@ class:Thread::class,
],
order: ['Title'=>'ASC']
)]
protected $ThreadExplicit;
protected ?Thread $ThreadExplicit;
}
8 changes: 4 additions & 4 deletions tests/MockSite/Models/Forum/TagPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ class TagPost extends \Divergence\Models\Model
public static $createRevisionOnSave = true;

#[Column(type: "integer", required:true, notnull: true)]
protected $TagID;
protected int $TagID;

#[Column(type: "integer", required:true, notnull: true)]
protected $PostID;
protected int $PostID;

public static $indexes = [
'TagPost' => [
Expand All @@ -61,13 +61,13 @@ class:Tag::class,
local: 'ThreadID',
foreign: 'ID',
)]
protected $Tag;
protected ?Tag $Tag;

#[Relation(
type:'one-one',
class:Post::class,
local: 'PostID',
foreign: 'ID',
)]
protected $Post;
protected ?Post $Post;
}
6 changes: 3 additions & 3 deletions tests/MockSite/Models/Forum/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ class Thread extends \Divergence\Models\Model
public static $indexes = [];

#[Column(type: "string", required:true, notnull: true)]
protected $Title;
protected string $Title;

#[Column(type: "integer", required:true, notnull: true)]
protected $CategoryID;
protected int $CategoryID;

#[Relation(
type:'one-many',
class:Category::class,
local: 'ID',
foreign: 'ThreadID',
)]
protected $Categories;
protected ?Category $Categories;
}

0 comments on commit e781d40

Please sign in to comment.