Skip to content
This repository was archived by the owner on Jan 24, 2024. It is now read-only.

Commit 8327b2d

Browse files
authored
Merge pull request #11 from spiral/develop
Update readme, exception message, orm bugfix
2 parents e6b7125 + f18755a commit 8327b2d

File tree

4 files changed

+109
-8
lines changed

4 files changed

+109
-8
lines changed

README.md

+96-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,99 @@ Spiral ORM
66
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/spiral/orm/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/spiral/orm/?branch=master)
77
[![Coverage Status](https://coveralls.io/repos/github/spiral/orm/badge.svg?branch=master)](https://coveralls.io/github/spiral/orm?branch=master)
88

9-
<b>[Documentation](http://spiral-framework.com/guide)</b> | [CHANGELOG](/CHANGELOG.md)
9+
ORM engine with automatic database scaffolding, strict schemas, code discovery, modular database partitions and various relation loaders. Hackable!
10+
11+
<b>[Full Documentation](http://spiral-framework.com/guide)</b> | [CHANGELOG](/CHANGELOG.md)
12+
13+
# Documentation
14+
* [Overview](https://spiral-framework.com/guide/orm/overview.md)
15+
* [Record and RecordEntity](https://spiral-framework.com/guide/orm/entities.md)
16+
* [Repositories and Selectors](https://spiral-framework.com/guide/orm/repositories.md)
17+
* [Accessors and Filters](https://spiral-framework.com/guide/orm/accessors.md)
18+
* [Column Objects](https://spiral-framework.com/guide/orm/columns.md)
19+
* [Scaffolding and Migrations](https://spiral-framework.com/guide/orm/scaffolding.md)
20+
* [Transactions](https://spiral-framework.com/guide/orm/transactions.md)
21+
* [Relations](https://spiral-framework.com/guide/orm/relations.md)
22+
* [Morphed Relations](https://spiral-framework.com/guide/orm/morphed-relations.md)
23+
* [Pre-compiled Relations](https://spiral-framework.com/guide/orm/late-binding.md)
24+
* [Query Models](https://spiral-framework.com/guide/orm/query.md)
25+
* [Eager loading](https://spiral-framework.com/guide/orm/loading.md)
26+
* [Recursive Relations](https://spiral-framework.com/guide/orm/recursive-relations.md)
27+
* [Hybrid Databases](https://spiral-framework.com/guide/orm/odm-bridge.md)
28+
* [Custom Relations](https://spiral-framework.com/guide/orm/custom-relations.md)
29+
30+
# Examples
31+
32+
```php
33+
class Post extends RecordEntity
34+
{
35+
use TimestampsTrait;
36+
37+
//Database partitions, isolation and aliasing
38+
const DATABASE = 'blog';
39+
40+
const SCHEMA = [
41+
'id' => 'bigPrimary',
42+
'title' => 'string(64)',
43+
'status' => 'enum(published,draft)',
44+
'body' => 'text',
45+
46+
//Simple relation definitions
47+
'comments' => [self::HAS_MANY => Comment::class],
48+
49+
//Not very simple relation definitions
50+
'collaborators' => [
51+
self::MANY_TO_MANY => User::class,
52+
self::PIVOT_TABLE => 'post_collaborators_map',
53+
self::PIVOT_COLUMNS => [
54+
'time_assigned' => 'datetime',
55+
'type' => 'string, nullable',
56+
],
57+
User::INVERSE => 'collaborated_posts'
58+
],
59+
60+
//Pre-compiled relations
61+
'author' => [
62+
self::BELONGS_TO => AuthorInterface::class,
63+
self::LATE_BINDING => true
64+
],
65+
66+
//Hybrid databases
67+
'metadata' => [
68+
Document::ONE => Mongo\Metadata::class
69+
]
70+
];
71+
}
72+
```
73+
74+
```php
75+
$posts = $postSource->find()->distinct()
76+
->with('comments', ['where' => ['{@}.approved' => true]]) //Automatic joins
77+
->with('author')->where('author_name', 'LIKE', $authorName) //Fluent
78+
->load('comments.author') //Cascade eager-loading (joins or external query)
79+
->paginate(10) //Quick pagination using active request
80+
->getIterator();
81+
82+
foreach ($posts as $post) {
83+
echo $post->author->getName();
84+
}
85+
```
86+
87+
```php
88+
$post = new Post();
89+
$post->publish_at = 'tomorrow 8am';
90+
$post->author = new User(['name' => 'Antony']);
91+
92+
$post->tags->link(new Tag(['name' => 'tag A']));
93+
$post->tags->link($tags->findOne(['name' => 'tag B']));
94+
95+
$transaction = new Transaction();
96+
$transaction->store($post);
97+
$transaction->run();
98+
99+
//--or--: Active record (optional)
100+
$post->save();
101+
102+
//--or--: request specific transaction
103+
$this->transaction->store($post);
104+
```

source/Spiral/ORM/Entities/Loaders/AbstractLoader.php

+5-3
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,10 @@ final public function loadRelation(
184184
}
185185

186186
if ($join) {
187-
//Let's tell our loaded that it's method is JOIN (forced)
188-
$options['method'] = self::JOIN;
187+
if (empty($options['method']) || !in_array($options['method'], [self::JOIN, self::LEFT_JOIN])) {
188+
//Let's tell our loaded that it's method is JOIN (forced)
189+
$options['method'] = self::JOIN;
190+
}
189191
}
190192

191193
if (isset($loaders[$relation])) {
@@ -364,4 +366,4 @@ private function loadChain(string $chain, array $options, bool $join): LoaderInt
364366
$join
365367
);
366368
}
367-
}
369+
}

source/Spiral/ORM/Entities/Relations/Traits/SyncedTrait.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ trait SyncedTrait
2525
*/
2626
protected function isSynced(RecordInterface $inner, RecordInterface $outer): bool
2727
{
28-
if (empty($outer->primaryKey())) {
28+
if (empty($inner->primaryKey()) || empty($outer->primaryKey())) {
2929
//Parent not saved
3030
return false;
3131
}
@@ -46,4 +46,4 @@ protected function isSynced(RecordInterface $inner, RecordInterface $outer): boo
4646
* @return string|null
4747
*/
4848
abstract protected function key(int $key);
49-
}
49+
}

source/Spiral/ORM/Schemas/Relations/BelongsToMorphedSchema.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,11 @@ public function declareTables(SchemaBuilder $builder): array
152152

153153
$outerKey = $this->findOuter($builder);
154154
if (empty($outerKey)) {
155-
throw new RelationSchemaException("Unable to build morphed relation, no outer record found");
155+
throw new RelationSchemaException(sprintf(
156+
"Unable to build morphed relation, no outer record(s) found for '%s' from '%s'",
157+
$this->getDefinition()->getTarget(),
158+
$this->getDefinition()->sourceContext()->getClass()
159+
));
156160
}
157161

158162
//Make sure all tables has same outer
@@ -198,4 +202,4 @@ public function packRelation(SchemaBuilder $builder): array
198202

199203
return $packed;
200204
}
201-
}
205+
}

0 commit comments

Comments
 (0)