This commit is contained in:
2025-12-16 20:20:08 +08:00
parent 0dcd548645
commit ee1a5a993d
9 changed files with 142 additions and 29 deletions
+23 -8
View File
@@ -30,6 +30,14 @@ use ReturnTypeWillChange;
use ReflectionException;
use validator\Validator;
enum Driver
{
case Mysql;
case Pgsql;
}
/**
* Class BOrm
*
@@ -139,7 +147,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
*/
public function clean(): void
{
$this->_attributes = [];
$this->_attributes = [];
$this->_oldAttributes = [];
}
@@ -449,7 +457,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
*/
private function insert(): bool|static
{
$sql = SqlBuilder::builder($query = static::query())->insert($this->_attributes);
$sql = SqlBuilder::builder($query = static::query())->insert($this->_attributes);
$lastId = $this->getConnection()->createCommand($sql, $query->params)->exec();
if ($lastId === FALSE) {
return FALSE;
@@ -512,7 +520,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
protected function arrayIntersect(array $params): array
{
$condition = [];
$oldPrams = [];
$oldPrams = [];
foreach ($this->_oldAttributes as $key => $attribute) {
if (!array_key_exists($key, $params) || $params[$key] == $attribute) {
$condition[$key] = $attribute;
@@ -547,7 +555,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
*/
public function populates(array $value): static
{
$this->_attributes = $value;
$this->_attributes = $value;
$this->_oldAttributes = $value;
$this->setIsNowExample();
return $this;
@@ -635,7 +643,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
*/
public function getTable(): string
{
$connection = $this->getConnection();
$connection = $this->getConnection();
$tablePrefix = $connection->tablePrefix;
if (empty($this->table)) {
throw new Exception('You need add static method `tableName` and return table name.');
@@ -644,6 +652,13 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
if (!empty($tablePrefix) && !str_starts_with($table, $tablePrefix)) {
$table = $tablePrefix . $table;
}
$driver = strtolower($connection->driver ?? 'mysql');
if (in_array($driver, ['pgsql', 'postgresql'])) {
// PostgreSQL 使用双引号,并且 schema.table 格式
return '"' . $connection->database . '"."' . $table . '"';
}
// MySQL 使用反引号
return '`' . $connection->database . '`.' . $table;
}
@@ -678,7 +693,7 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
public function refresh(): static
{
$this->_oldAttributes = $this->_attributes;
$this->isNewExample = FALSE;
$this->isNewExample = FALSE;
return $this;
}
@@ -853,8 +868,8 @@ abstract class Model extends Component implements ModelInterface, ArrayAccess, \
*/
public static function populate(array $data): static
{
$model = new static();
$model->_attributes = $data;
$model = new static();
$model->_attributes = $data;
$model->_oldAttributes = $data;
$model->setIsNowExample();
return $model;