diff --git a/src/Attribute/Obj/Index.php b/src/Attribute/Obj/Index.php index 4cd773f..243d5bd 100644 --- a/src/Attribute/Obj/Index.php +++ b/src/Attribute/Obj/Index.php @@ -11,4 +11,4 @@ class Index { public IndexTypeEnum $type = IndexTypeEnum::Unique, public null|string $name = null, ) {} -} +} \ No newline at end of file diff --git a/src/Common/EntityResolver.php b/src/Common/EntityResolver.php index f61a7b6..2decb16 100644 --- a/src/Common/EntityResolver.php +++ b/src/Common/EntityResolver.php @@ -5,6 +5,7 @@ namespace Ulmus\Common; use Notes\Common\ReflectedClass; use Notes\Common\ReflectedProperty; use Psr\SimpleCache\CacheInterface; +use Ulmus\Attribute\Obj\Index; use Ulmus\Ulmus, Ulmus\Attribute\Obj\Table, Ulmus\Attribute\Obj\AdapterAttributeInterface, @@ -214,9 +215,9 @@ class EntityResolver { return null; } - public function getCompoundKeyFields() : ? array + public function getCompoundKeyFields() : ? Index { - return null; + return $this->getAttributeImplementing(Index::class); } public function getUniqueFields() : ? array diff --git a/src/EntityTrait.php b/src/EntityTrait.php index 70b6b84..4503287 100644 --- a/src/EntityTrait.php +++ b/src/EntityTrait.php @@ -144,13 +144,27 @@ trait EntityTrait { return false; } - if ( null === $pkField = $this->resolveEntity()->getPrimaryKeyField($this) ) { - throw new Exception\EntityPrimaryKeyUnknown(sprintf("Entity %s has no field containing attributes 'primary_key'", static::class)); + if ( null === $pkField = $this->resolveEntity()->getPrimaryKeyField() ) { + if ( null !== $compoundKeyFields = $this->resolveEntity()->getCompoundKeyFields() ) { + $loaded = false; + + foreach ($compoundKeyFields->column as $column) { + $field = $this->resolveEntity()->field($column); + + if (! $field->allowsNull() ) { + $loaded |= isset($this->{$field->name}); + } + } + + return $loaded; + }; + } + else { + $key = key($pkField); + return isset($this->$key); } - $key = key($pkField); - - return isset($this->$key); + throw new Exception\EntityPrimaryKeyUnknown(sprintf("Entity %s has no field containing attributes 'primary_key'", static::class)); } #[Ignore] diff --git a/src/Repository.php b/src/Repository.php index 30e5a97..1db7f43 100644 --- a/src/Repository.php +++ b/src/Repository.php @@ -97,7 +97,7 @@ class Repository implements RepositoryInterface else { $pk = Ulmus::resolveEntity($this->entityClass)->getPrimaryKeyField(); - if (count($pk) === 1) { + if ($pk && count($pk) === 1) { $field = key($pk); $this->select(Common\Sql::function('COUNT', Common\Sql::raw("DISTINCT " . $this->entityClass::field($field))));