- Added a new @PrimaryKey annotation to replace the too-specific property-wise @Id
This commit is contained in:
parent
0e8443b1c1
commit
b20e537038
@ -6,11 +6,11 @@ namespace Ulmus\Annotation\Property\Field;
|
|||||||
* Since we need consistancy between the declaration of our ID and FK fields, it
|
* Since we need consistancy between the declaration of our ID and FK fields, it
|
||||||
* was decided to extend the Id class instead of Field for this case.
|
* was decided to extend the Id class instead of Field for this case.
|
||||||
*/
|
*/
|
||||||
class ForeignKey extends Id {
|
class ForeignKey extends PrimaryKey {
|
||||||
|
|
||||||
public function __construct()
|
public function __construct(? string $type = null, ? int $length = null)
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct($type, $length);
|
||||||
|
|
||||||
unset($this->nullable);
|
unset($this->nullable);
|
||||||
$this->attributes['primary_key'] = false;
|
$this->attributes['primary_key'] = false;
|
||||||
|
@ -2,15 +2,13 @@
|
|||||||
|
|
||||||
namespace Ulmus\Annotation\Property\Field;
|
namespace Ulmus\Annotation\Property\Field;
|
||||||
|
|
||||||
class Id extends \Ulmus\Annotation\Property\Field {
|
class Id extends \Ulmus\Annotation\Property\Field\PrimaryKey {
|
||||||
|
|
||||||
public function __construct()
|
public function __construct()
|
||||||
{
|
{
|
||||||
$this->nullable = false;
|
|
||||||
$this->attributes['unsigned'] = true;
|
$this->attributes['unsigned'] = true;
|
||||||
$this->attributes['primary_key'] = true;
|
|
||||||
$this->attributes['auto_increment'] = true;
|
$this->attributes['auto_increment'] = true;
|
||||||
|
|
||||||
parent::__construct('bigint');
|
parent::__construct('bigint');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
15
src/Annotation/Property/Field/PrimaryKey.php
Normal file
15
src/Annotation/Property/Field/PrimaryKey.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Ulmus\Annotation\Property\Field;
|
||||||
|
|
||||||
|
class PrimaryKey extends \Ulmus\Annotation\Property\Field {
|
||||||
|
|
||||||
|
public function __construct(? string $type = null, ? int $length = null)
|
||||||
|
{
|
||||||
|
$this->nullable = false;
|
||||||
|
$this->attributes['primary_key'] = true;
|
||||||
|
|
||||||
|
parent::__construct($type, $length);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -5,7 +5,7 @@ namespace Ulmus;
|
|||||||
use Ulmus\{ Repository, Query, Common\EntityResolver, Common\EntityField };
|
use Ulmus\{ Repository, Query, Common\EntityResolver, Common\EntityField };
|
||||||
use Ulmus\Annotation\Classes\{ Method, Table, Collation, };
|
use Ulmus\Annotation\Classes\{ Method, Table, Collation, };
|
||||||
use Ulmus\Annotation\Property\{ Field, Filter, FilterJoin, Relation, OrderBy, Where, OrWhere, Join, Virtual, On, WithJoin, };
|
use Ulmus\Annotation\Property\{ Field, Filter, FilterJoin, Relation, OrderBy, Where, OrWhere, Join, Virtual, On, WithJoin, };
|
||||||
use Ulmus\Annotation\Property\Field\{ Id, ForeignKey, CreatedAt, UpdatedAt, Datetime as DateTime, Date, Time, Bigint, Tinyint, Text, Mediumtext, Longtext, Blob, Mediumblob, Longblob, };
|
use Ulmus\Annotation\Property\Field\{ PrimaryKey, Id, ForeignKey, CreatedAt, UpdatedAt, Datetime as DateTime, Date, Time, Bigint, Tinyint, Text, Mediumtext, Longtext, Blob, Mediumblob, Longblob, };
|
||||||
use Ulmus\Annotation\Property\Relation\{ Ignore as RelationIgnore };
|
use Ulmus\Annotation\Property\Relation\{ Ignore as RelationIgnore };
|
||||||
|
|
||||||
trait EntityTrait {
|
trait EntityTrait {
|
||||||
|
@ -8,6 +8,7 @@ class Where extends Fragment {
|
|||||||
const OPERATOR_LIKE = "LIKE";
|
const OPERATOR_LIKE = "LIKE";
|
||||||
const OPERATOR_EQUAL = "=";
|
const OPERATOR_EQUAL = "=";
|
||||||
const OPERATOR_NOT_EQUAL = "<>";
|
const OPERATOR_NOT_EQUAL = "<>";
|
||||||
|
const OPERATOR_REGEXP = "REGEXP";
|
||||||
const CONDITION_AND = "AND";
|
const CONDITION_AND = "AND";
|
||||||
const CONDITION_OR = "OR";
|
const CONDITION_OR = "OR";
|
||||||
const CONDITION_NOT = "NOT";
|
const CONDITION_NOT = "NOT";
|
||||||
@ -113,7 +114,7 @@ class Where extends Fragment {
|
|||||||
}
|
}
|
||||||
|
|
||||||
# whitelisting operators
|
# whitelisting operators
|
||||||
return in_array(strtoupper($this->operator), [ '=', '!=', '>', '>=', '<', '<=', '<>', 'LIKE', 'IS', 'IS NOT' ]) ? $this->operator : "=";
|
return in_array(strtoupper($this->operator), [ '=', '!=', '>', '>=', '<', '<=', '<>', 'LIKE', 'IS', 'IS NOT', 'REGEXP' ]) ? $this->operator : "=";
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function value()
|
protected function value()
|
||||||
|
@ -46,7 +46,7 @@ class Repository
|
|||||||
return $this->where($field, $value)->loadOne();
|
return $this->where($field, $value)->loadOne();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function loadFromPk($value, /* ? stringable */ $primaryKey = null) : ? object
|
public function loadFromPk($value, null|string|\Stringable $primaryKey = null) : ? object
|
||||||
{
|
{
|
||||||
return $primaryKey ? $this->loadOneFromField($primaryKey, $value) : $this->wherePrimaryKey($value)->loadOne();
|
return $primaryKey ? $this->loadOneFromField($primaryKey, $value) : $this->wherePrimaryKey($value)->loadOne();
|
||||||
}
|
}
|
||||||
@ -130,7 +130,7 @@ class Repository
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function save(/*object|array*/ $entity, ? array $fieldsAndValue = null, bool $replace = false) : bool
|
public function save(object|array $entity, ? array $fieldsAndValue = null, bool $replace = false) : bool
|
||||||
{
|
{
|
||||||
if ( is_array($entity) ) {
|
if ( is_array($entity) ) {
|
||||||
$entity = ( new $this->entityClass() )->fromArray($entity);
|
$entity = ( new $this->entityClass() )->fromArray($entity);
|
||||||
@ -198,7 +198,7 @@ class Repository
|
|||||||
return $changed;
|
return $changed;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function insertAll(/* EntityCollection | array */ $collection, int $size = 1000) : int
|
public function insertAll(EntityCollection|array $collection, int $size = 1000) : int
|
||||||
{
|
{
|
||||||
if ( empty($collection) ) {
|
if ( empty($collection) ) {
|
||||||
return 0;
|
return 0;
|
||||||
@ -254,12 +254,12 @@ class Repository
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function replace(/*object|array*/ $entity, ? array $fieldsAndValue = null) : bool
|
public function replace(object|array $entity, ? array $fieldsAndValue = null) : bool
|
||||||
{
|
{
|
||||||
return $this->save($entity, $fieldsAndValue, true);
|
return $this->save($entity, $fieldsAndValue, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function replaceAll(/*EntityCollection|array*/ $collection) : int
|
public function replaceAll(EntityCollection|array $collection) : int
|
||||||
{
|
{
|
||||||
$changed = 0;
|
$changed = 0;
|
||||||
|
|
||||||
@ -339,7 +339,7 @@ class Repository
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function removeQueryFragment(/*? Query\Fragment|string*/ $fragment) : self
|
public function removeQueryFragment(null|Query\Fragment|string|array $fragment) : self
|
||||||
{
|
{
|
||||||
foreach((array) $fragment as $item) {
|
foreach((array) $fragment as $item) {
|
||||||
$this->queryBuilder->removeFragment($item);
|
$this->queryBuilder->removeFragment($item);
|
||||||
@ -381,14 +381,14 @@ class Repository
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function select(/*array|Stringable*/ $fields, bool $distinct = false) : self
|
public function select(array|string|\Stringable $fields, bool $distinct = false) : self
|
||||||
{
|
{
|
||||||
$this->queryBuilder->select($fields, $distinct);
|
$this->queryBuilder->select($fields, $distinct);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function distinct(/*array|Stringable*/ $fields) : self
|
public function distinct(array|string|\Stringable $fields) : self
|
||||||
{
|
{
|
||||||
$this->queryBuilder->select($fields);
|
$this->queryBuilder->select($fields);
|
||||||
$this->queryBuilder->getFragment(Query\Select::class)->distinct = true;
|
$this->queryBuilder->getFragment(Query\Select::class)->distinct = true;
|
||||||
@ -642,7 +642,7 @@ class Repository
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function withSubquery(/*string|array*/ $fields) : self
|
public function withSubquery(string|array $fields) : self
|
||||||
{
|
{
|
||||||
# We skip subqueries when counting results since it should not affect the row count.
|
# We skip subqueries when counting results since it should not affect the row count.
|
||||||
if ( $this instanceof Repository\ServerRequestCountRepository ) {
|
if ( $this instanceof Repository\ServerRequestCountRepository ) {
|
||||||
@ -704,7 +704,7 @@ class Repository
|
|||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function loadCollectionRelation(EntityCollection $collection, /*array|string*/ $fields) : void
|
public function loadCollectionRelation(EntityCollection $collection, array|string $fields) : void
|
||||||
{
|
{
|
||||||
foreach ((array)$fields as $name) {
|
foreach ((array)$fields as $name) {
|
||||||
if (null !== ($relation = $this->entityResolver->searchFieldAnnotation($name, new Annotation\Property\Relation()))) {
|
if (null !== ($relation = $this->entityResolver->searchFieldAnnotation($name, new Annotation\Property\Relation()))) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user