Skip to content

Commit

Permalink
feat(Model): 移动模型基类到 wei
Browse files Browse the repository at this point in the history
  • Loading branch information
twinh committed Aug 2, 2022
1 parent 52b4de3 commit 5c532ca
Show file tree
Hide file tree
Showing 11 changed files with 3,390 additions and 0 deletions.
123 changes: 123 additions & 0 deletions lib/BaseModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
<?php

namespace Wei;

use ArrayAccess;
use Countable;
use IteratorAggregate;
use JsonSerializable;
use Wei\Db\QueryBuilderPropsTrait;

/**
* @internal 逐步完善后移到 Wei 中
* @phpstan-ignore-next-line PHPStorm allows trait type to prompt code call
* @mixin ModelTrait
*/
abstract class BaseModel extends Base implements ArrayAccess, IteratorAggregate, Countable, JsonSerializable
{
use QueryBuilderPropsTrait;

/**
* @var bool
*/
protected static $createNewInstance = true;

protected $createdAtColumn = 'created_at';

protected $createdByColumn = 'created_by';

protected $updatedAtColumn = 'updated_at';

protected $updatedByColumn = 'updated_by';

/**
* The primary key column
*
* @var string
*/
protected $primaryKey = 'id';

/**
* Whether it's a new record and has not save to database
*
* @var bool
*/
protected $new = true;

/**
* The data of model
*
* @var array
*/
protected $attributes = [];

/**
* The fields that are assignable through fromArray method
*
* @var array
*/
protected $fillable = [];

/**
* The fields that aren't assignable through fromArray method
*
* @var array
*/
protected $guarded = [
'id',
'created_at',
'created_by',
'updated_at',
'updated_by',
];

/**
* The attribute values before changed
*
* @var array
*/
protected $changes = [];

/**
* @var array
*/
protected $virtual = [];

/**
* @var string[]
*/
protected $hidden = [];

protected static $booted = [];

/**
* Returns whether the model was inserted in the this request
*
* @var bool
*/
protected $wasRecentlyCreated = false;

/**
* The attribute is set by the user, such as calling `$model->xxx = $value`.
*/
protected const ATTRIBUTE_SOURCE_USER = 1;

/**
* The attribute is loaded from the database and is an undecoded/unconverted type string.
*/
protected const ATTRIBUTE_SOURCE_DB = 2;

/**
* The attribute have been convert by PHP, such as type cast.
*/
protected const ATTRIBUTE_SOURCE_PHP = 3;

/**
* The source of the current attribute values
*
* @var array
*/
protected $attributeSources = [
'*' => self::ATTRIBUTE_SOURCE_USER,
];
}
33 changes: 33 additions & 0 deletions lib/Model/CacheTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Wei\Model;

/**
* Add cache functions to the model
*/
trait CacheTrait
{
/**
* Remove the model cache
*
* @return $this
*/
public function removeModelCache(): self
{
if ($this->getColumnValue('id')) {
$this->cache->delete($this->getModelCacheKey());
}
return $this;
}

/**
* Return the model cache key
*
* @param string|int|null $id
* @return string
*/
public function getModelCacheKey($id = null): string
{
return $this->getDb()->getDbname() . ':' . $this->getTable() . ':' . ($id ?: $this->getColumnValue('id'));
}
}
17 changes: 17 additions & 0 deletions lib/Model/CamelCaseTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Wei\Model;

trait CamelCaseTrait
{
public static function bootCamelCaseTrait(): void
{
static::onModelEvent('init', 'setCamelCaseKeyConverter');
}

public function setCamelCaseKeyConverter(): void
{
$this->setDbKeyConverter([$this->wei->str, 'snake']);
$this->setPhpKeyConverter([$this->wei->str, 'camel']);
}
}
Loading

0 comments on commit 5c532ca

Please sign in to comment.