Skip to content

Commit 745be08

Browse files
committed
Removed embedded relations
Removed embedded relations Updated documenation Added changelog
1 parent a804118 commit 745be08

File tree

13 files changed

+8
-2126
lines changed

13 files changed

+8
-2126
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
All notable changes to this project will be documented in this file.
33

44
## [Unreleased]
5+
### Removed
6+
- EmbedsOne and EmbedsMany relations by [@divine](https://github.com/divine).

README.md

Lines changed: 5 additions & 186 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ This package adds functionalities to the Eloquent model and Query builder for Mo
3030
- [Relationships](#relationships)
3131
- [Basic Usage](#basic-usage-1)
3232
- [belongsToMany and pivots](#belongstomany-and-pivots)
33-
- [EmbedsMany Relationship](#embedsmany-relationship)
34-
- [EmbedsOne Relationship](#embedsone-relationship)
3533
- [Query Builder](#query-builder)
3634
- [Basic Usage](#basic-usage-2)
3735
- [Available operations](#available-operations)
@@ -419,7 +417,7 @@ Aggregations can be also used on sub-documents:
419417
$total = Order::max('suborder.price');
420418
```
421419

422-
**NOTE**: This aggregation only works with single sub-documents (like `EmbedsOne`) not subdocument arrays (like `EmbedsMany`).
420+
**NOTE**: This aggregation only works with single sub-documents not arrays.
423421

424422
**Incrementing/Decrementing the value of a column**
425423

@@ -712,10 +710,6 @@ The only available relationships are:
712710
- belongsTo
713711
- belongsToMany
714712

715-
The MongoDB-specific relationships are:
716-
- embedsOne
717-
- embedsMany
718-
719713
Here is a small example:
720714

721715
```php
@@ -764,152 +758,6 @@ class User extends Model
764758
}
765759
```
766760

767-
### EmbedsMany Relationship
768-
769-
If you want to embed models, rather than referencing them, you can use the `embedsMany` relation. This relation is similar to the `hasMany` relation but embeds the models inside the parent object.
770-
771-
**REMEMBER**: These relations return Eloquent collections, they don't return query builder objects!
772-
773-
```php
774-
use Jenssegers\Mongodb\Eloquent\Model;
775-
776-
class User extends Model
777-
{
778-
public function books()
779-
{
780-
return $this->embedsMany(Book::class);
781-
}
782-
}
783-
```
784-
785-
You can access the embedded models through the dynamic property:
786-
787-
```php
788-
$user = User::first();
789-
790-
foreach ($user->books as $book) {
791-
//
792-
}
793-
```
794-
795-
The inverse relation is auto*magically* available. You don't need to define this reverse relation.
796-
797-
```php
798-
$book = Book::first();
799-
800-
$user = $book->user;
801-
```
802-
803-
Inserting and updating embedded models works similar to the `hasMany` relation:
804-
805-
```php
806-
$book = $user->books()->save(
807-
new Book(['title' => 'A Game of Thrones'])
808-
);
809-
810-
// or
811-
$book =
812-
$user->books()
813-
->create(['title' => 'A Game of Thrones']);
814-
```
815-
816-
You can update embedded models using their `save` method (available since release 2.0.0):
817-
818-
```php
819-
$book = $user->books()->first();
820-
821-
$book->title = 'A Game of Thrones';
822-
$book->save();
823-
```
824-
825-
You can remove an embedded model by using the `destroy` method on the relation, or the `delete` method on the model (available since release 2.0.0):
826-
827-
```php
828-
$book->delete();
829-
830-
// Similar operation
831-
$user->books()->destroy($book);
832-
```
833-
834-
If you want to add or remove an embedded model, without touching the database, you can use the `associate` and `dissociate` methods.
835-
836-
To eventually write the changes to the database, save the parent object:
837-
838-
```php
839-
$user->books()->associate($book);
840-
$user->save();
841-
```
842-
843-
Like other relations, embedsMany assumes the local key of the relationship based on the model name. You can override the default local key by passing a second argument to the embedsMany method:
844-
845-
```php
846-
use Jenssegers\Mongodb\Eloquent\Model;
847-
848-
class User extends Model
849-
{
850-
public function books()
851-
{
852-
return $this->embedsMany(Book::class, 'local_key');
853-
}
854-
}
855-
```
856-
857-
Embedded relations will return a Collection of embedded items instead of a query builder. Check out the available operations here: https://laravel.com/docs/master/collections
858-
859-
860-
### EmbedsOne Relationship
861-
862-
The embedsOne relation is similar to the embedsMany relation, but only embeds a single model.
863-
864-
```php
865-
use Jenssegers\Mongodb\Eloquent\Model;
866-
867-
class Book extends Model
868-
{
869-
public function author()
870-
{
871-
return $this->embedsOne(Author::class);
872-
}
873-
}
874-
```
875-
876-
You can access the embedded models through the dynamic property:
877-
878-
```php
879-
$book = Book::first();
880-
$author = $book->author;
881-
```
882-
883-
Inserting and updating embedded models works similar to the `hasOne` relation:
884-
885-
```php
886-
$author = $book->author()->save(
887-
new Author(['name' => 'John Doe'])
888-
);
889-
890-
// Similar
891-
$author =
892-
$book->author()
893-
->create(['name' => 'John Doe']);
894-
```
895-
896-
You can update the embedded model using the `save` method (available since release 2.0.0):
897-
898-
```php
899-
$author = $book->author;
900-
901-
$author->name = 'Jane Doe';
902-
$author->save();
903-
```
904-
905-
You can replace the embedded model with a new model like this:
906-
907-
```php
908-
$newAuthor = new Author(['name' => 'Jane Doe']);
909-
910-
$book->author()->save($newAuthor);
911-
```
912-
913761
Query Builder
914762
-------------
915763

@@ -1098,39 +946,10 @@ Jenssegers\Mongodb\MongodbQueueServiceProvider::class,
1098946
Upgrading
1099947
---------
1100948

1101-
#### Upgrading from version 2 to 3
1102-
1103-
In this new major release which supports the new MongoDB PHP extension, we also moved the location of the Model class and replaced the MySQL model class with a trait.
949+
#### Upgrading from version 3 to 4
1104950

1105-
Please change all `Jenssegers\Mongodb\Model` references to `Jenssegers\Mongodb\Eloquent\Model` either at the top of your model files or your registered alias.
951+
This new major release contains breaking changes which is listed below:
1106952

1107-
```php
1108-
use Jenssegers\Mongodb\Eloquent\Model;
953+
- EmbedsOne and EmbedsMany relations has been removed completely. See explanation [here](https://github.com/jenssegers/laravel-mongodb/issues/1974#issuecomment-592859508)
1109954

1110-
class User extends Model
1111-
{
1112-
//
1113-
}
1114-
```
1115-
1116-
If you are using hybrid relations, your MySQL classes should now extend the original Eloquent model class `Illuminate\Database\Eloquent\Model` instead of the removed `Jenssegers\Eloquent\Model`.
1117-
1118-
Instead use the new `Jenssegers\Mongodb\Eloquent\HybridRelations` trait. This should make things more clear as there is only one single model class in this package.
1119-
1120-
```php
1121-
use Jenssegers\Mongodb\Eloquent\HybridRelations;
1122-
1123-
class User extends Model
1124-
{
1125-
1126-
use HybridRelations;
1127-
1128-
protected $connection = 'mysql';
1129-
}
1130-
```
1131-
1132-
Embedded relations now return an `Illuminate\Database\Eloquent\Collection` rather than a custom Collection class. If you were using one of the special methods that were available, convert them to Collection operations.
1133-
1134-
```php
1135-
$books = $user->books()->sortBy('title')->get();
1136-
```
955+
For any other minor changes, please take a look at our [changelog](CHANGELOG.md)

phpunit.xml.dist

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
</testsuite>
3232
<testsuite name="relations">
3333
<file>tests/RelationsTest.php</file>
34-
<file>tests/EmbeddedRelationsTest.php</file>
3534
</testsuite>
3635
<testsuite name="mysqlrelations">
3736
<file>tests/RelationsTest.php</file>

src/Jenssegers/Mongodb/Eloquent/Builder.php

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,6 @@ class Builder extends EloquentBuilder
3535
*/
3636
public function update(array $values, array $options = [])
3737
{
38-
// Intercept operations on embedded models and delegate logic
39-
// to the parent relation instance.
40-
if ($relation = $this->model->getParentRelation()) {
41-
$relation->performUpdate($this->model, $values);
42-
43-
return 1;
44-
}
45-
4638
return $this->toBase()->update($this->addUpdatedAtColumn($values), $options);
4739
}
4840

@@ -51,14 +43,6 @@ public function update(array $values, array $options = [])
5143
*/
5244
public function insert(array $values)
5345
{
54-
// Intercept operations on embedded models and delegate logic
55-
// to the parent relation instance.
56-
if ($relation = $this->model->getParentRelation()) {
57-
$relation->performInsert($this->model, $values);
58-
59-
return true;
60-
}
61-
6246
return parent::insert($values);
6347
}
6448

@@ -67,14 +51,6 @@ public function insert(array $values)
6751
*/
6852
public function insertGetId(array $values, $sequence = null)
6953
{
70-
// Intercept operations on embedded models and delegate logic
71-
// to the parent relation instance.
72-
if ($relation = $this->model->getParentRelation()) {
73-
$relation->performInsert($this->model, $values);
74-
75-
return $this->model->getKey();
76-
}
77-
7854
return parent::insertGetId($values, $sequence);
7955
}
8056

@@ -83,14 +59,6 @@ public function insertGetId(array $values, $sequence = null)
8359
*/
8460
public function delete()
8561
{
86-
// Intercept operations on embedded models and delegate logic
87-
// to the parent relation instance.
88-
if ($relation = $this->model->getParentRelation()) {
89-
$relation->performDelete($this->model);
90-
91-
return $this->model->getKey();
92-
}
93-
9462
return parent::delete();
9563
}
9664

@@ -99,23 +67,6 @@ public function delete()
9967
*/
10068
public function increment($column, $amount = 1, array $extra = [])
10169
{
102-
// Intercept operations on embedded models and delegate logic
103-
// to the parent relation instance.
104-
if ($relation = $this->model->getParentRelation()) {
105-
$value = $this->model->{$column};
106-
107-
// When doing increment and decrements, Eloquent will automatically
108-
// sync the original attributes. We need to change the attribute
109-
// temporary in order to trigger an update query.
110-
$this->model->{$column} = null;
111-
112-
$this->model->syncOriginalAttribute($column);
113-
114-
$result = $this->model->update([$column => $value]);
115-
116-
return $result;
117-
}
118-
11970
return parent::increment($column, $amount, $extra);
12071
}
12172

@@ -124,21 +75,6 @@ public function increment($column, $amount = 1, array $extra = [])
12475
*/
12576
public function decrement($column, $amount = 1, array $extra = [])
12677
{
127-
// Intercept operations on embedded models and delegate logic
128-
// to the parent relation instance.
129-
if ($relation = $this->model->getParentRelation()) {
130-
$value = $this->model->{$column};
131-
132-
// When doing increment and decrements, Eloquent will automatically
133-
// sync the original attributes. We need to change the attribute
134-
// temporary in order to trigger an update query.
135-
$this->model->{$column} = null;
136-
137-
$this->model->syncOriginalAttribute($column);
138-
139-
return $this->model->update([$column => $value]);
140-
}
141-
14278
return parent::decrement($column, $amount, $extra);
14379
}
14480

0 commit comments

Comments
 (0)