Skip to content

Move model to a trait #2580

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Use the trait for every test model
  • Loading branch information
GromNaN committed Jul 5, 2024
commit c8d6c42f397723b1381c737846e6d598206ba7ff
19 changes: 4 additions & 15 deletions src/Eloquent/DocumentModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,19 +53,10 @@ trait DocumentModel
use HybridRelations;
use EmbedsRelations;

/**
* The collection associated with the model.
*
* @var string
*/
protected $collection;

/**
* The parent relation instance.
*
* @var Relation
*/
protected $parentRelation;
private Relation $parentRelation;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changing the visibility to private. There is a getter and an setter for this method. It doesn't need to be protected. Since it's a trait, it can still be accessed directly in a class that uses the trait.


/**
* List of field names to unset from the document on save.
Expand Down Expand Up @@ -150,7 +141,7 @@ public function freshTimestamp()
/** @inheritdoc */
public function getTable()
{
return $this->collection ?: parent::getTable();
return $this->collection ?? parent::getTable();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This property is not defined in the trait, otherwise it's allowed to redefine in the class that uses the trait.

}

/** @inheritdoc */
Expand Down Expand Up @@ -552,12 +543,10 @@ public function setParentRelation(Relation $relation)

/**
* Get the parent relation.
*
* @return Relation
*/
public function getParentRelation()
public function getParentRelation(): ?Relation
{
return $this->parentRelation;
return $this->parentRelation ?? null;
}

/** @inheritdoc */
Expand Down
8 changes: 7 additions & 1 deletion src/Eloquent/Model.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,13 @@ abstract class Model extends BaseModel
*/
protected $keyType = 'string';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the trait is used, the properties $keyType and $primaryKey must be defined, because they can't be redefined by the trait.


/** @param class-string|object $related */
/**
* Indicates if the given model class is a MongoDB document model.
* It must be a subclass of {@see BaseModel} and use the
* {@see DocumentModel} trait.
*
* @param class-string|object $related
*/
public static function isDocumentModel(string|object $related): bool
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method replaces $class instanceof MongoDB\Laravel\Eloquent\Model to check if the model class is for a MongoDB connection. It is public and can be used in projects.

{
return is_subclass_of($related, BaseModel::class)
Expand Down
11 changes: 8 additions & 3 deletions tests/Models/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

namespace MongoDB\Laravel\Tests\Models;

use MongoDB\Laravel\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\Model;
use MongoDB\Laravel\Eloquent\DocumentModel;
use MongoDB\Laravel\Relations\EmbedsMany;

class Address extends Eloquent
class Address extends Model
{
protected $connection = 'mongodb';
use DocumentModel;

protected $primaryKey = '_id';
protected $keyType = 'string';
protected $connection = 'mongodb';
protected static $unguarded = true;

public function addresses(): EmbedsMany
Expand Down
11 changes: 8 additions & 3 deletions tests/Models/Birthday.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@

namespace MongoDB\Laravel\Tests\Models;

use MongoDB\Laravel\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\Model;
use MongoDB\Laravel\Eloquent\DocumentModel;

/**
* @property string $name
* @property string $birthday
* @property string $time
*/
class Birthday extends Eloquent
class Birthday extends Model
{
use DocumentModel;

protected $primaryKey = '_id';
protected $keyType = 'string';
protected $connection = 'mongodb';
protected $collection = 'birthday';
protected string $collection = 'birthday';
protected $fillable = ['name', 'birthday'];

protected $casts = ['birthday' => 'datetime'];
Expand Down
14 changes: 9 additions & 5 deletions tests/Models/Book.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@

namespace MongoDB\Laravel\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use MongoDB\Laravel\Eloquent\Model as Eloquent;
use MongoDB\Laravel\Eloquent\DocumentModel;

/**
* @property string $title
* @property string $author
* @property array $chapters
*/
class Book extends Eloquent
class Book extends Model
{
protected $connection = 'mongodb';
protected $collection = 'books';
use DocumentModel;

protected $primaryKey = 'title';
protected $keyType = 'string';
protected $connection = 'mongodb';
protected string $collection = 'books';
protected static $unguarded = true;
protected $primaryKey = 'title';

public function author(): BelongsTo
{
Expand Down
11 changes: 8 additions & 3 deletions tests/Models/CastObjectId.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

namespace MongoDB\Laravel\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use MongoDB\Laravel\Eloquent\Casts\ObjectId;
use MongoDB\Laravel\Eloquent\Model as Eloquent;
use MongoDB\Laravel\Eloquent\DocumentModel;

class CastObjectId extends Eloquent
class CastObjectId extends Model
{
protected $connection = 'mongodb';
use DocumentModel;

protected $primaryKey = '_id';
protected $keyType = 'string';
protected $connection = 'mongodb';
protected static $unguarded = true;
protected $casts = [
'oid' => ObjectId::class,
Expand Down
11 changes: 8 additions & 3 deletions tests/Models/Casting.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

namespace MongoDB\Laravel\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use MongoDB\Laravel\Eloquent\Casts\BinaryUuid;
use MongoDB\Laravel\Eloquent\Model as Eloquent;
use MongoDB\Laravel\Eloquent\DocumentModel;

class Casting extends Eloquent
class Casting extends Model
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class continues to extend MongoDB\Laravel\Eloquent\Model

{
use DocumentModel;

protected $primaryKey = '_id';
protected $keyType = 'string';
protected $connection = 'mongodb';
protected $collection = 'casting';
protected string $collection = 'casting';

protected $fillable = [
'uuid',
Expand Down
13 changes: 9 additions & 4 deletions tests/Models/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@

namespace MongoDB\Laravel\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphOne;
use MongoDB\Laravel\Eloquent\Model as Eloquent;
use MongoDB\Laravel\Eloquent\DocumentModel;

class Client extends Eloquent
class Client extends Model
{
protected $connection = 'mongodb';
protected $collection = 'clients';
use DocumentModel;

protected $primaryKey = '_id';
protected $keyType = 'string';
protected $connection = 'mongodb';
protected string $collection = 'clients';
protected static $unguarded = true;

public function users(): BelongsToMany
Expand Down
13 changes: 9 additions & 4 deletions tests/Models/Experience.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

namespace MongoDB\Laravel\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use MongoDB\Laravel\Eloquent\Model as Eloquent;
use MongoDB\Laravel\Eloquent\DocumentModel;

class Experience extends Eloquent
class Experience extends Model
{
protected $connection = 'mongodb';
protected $collection = 'experiences';
use DocumentModel;

protected $primaryKey = '_id';
protected $keyType = 'string';
protected $connection = 'mongodb';
protected string $collection = 'experiences';
protected static $unguarded = true;

protected $casts = ['years' => 'int'];
Expand Down
13 changes: 9 additions & 4 deletions tests/Models/Group.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@

namespace MongoDB\Laravel\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use MongoDB\Laravel\Eloquent\Model as Eloquent;
use MongoDB\Laravel\Eloquent\DocumentModel;

class Group extends Eloquent
class Group extends Model
{
protected $connection = 'mongodb';
protected $collection = 'groups';
use DocumentModel;

protected $primaryKey = '_id';
protected $keyType = 'string';
protected $connection = 'mongodb';
protected string $collection = 'groups';
protected static $unguarded = true;

public function users(): BelongsToMany
Expand Down
13 changes: 9 additions & 4 deletions tests/Models/Guarded.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

namespace MongoDB\Laravel\Tests\Models;

use MongoDB\Laravel\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\Model;
use MongoDB\Laravel\Eloquent\DocumentModel;

class Guarded extends Eloquent
class Guarded extends Model
{
use DocumentModel;

protected $primaryKey = '_id';
protected $keyType = 'string';
protected $connection = 'mongodb';
protected $collection = 'guarded';
protected $guarded = ['foobar', 'level1->level2'];
protected string $collection = 'guarded';
protected $guarded = ['foobar', 'level1->level2'];
}
8 changes: 7 additions & 1 deletion tests/Models/HiddenAnimal.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace MongoDB\Laravel\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use MongoDB\Laravel\Eloquent\DocumentModel;
use MongoDB\Laravel\Eloquent\Model as Eloquent;
use MongoDB\Laravel\Query\Builder;

Expand All @@ -16,8 +18,12 @@
* @method static Builder truncate()
* @method static Eloquent sole(...$parameters)
*/
final class HiddenAnimal extends Eloquent
final class HiddenAnimal extends Model
{
use DocumentModel;

protected $primaryKey = '_id';
protected $keyType = 'string';
protected $fillable = [
'name',
'country',
Expand Down
13 changes: 9 additions & 4 deletions tests/Models/IdIsBinaryUuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@

namespace MongoDB\Laravel\Tests\Models;

use Illuminate\Database\Eloquent\Model;
use MongoDB\Laravel\Eloquent\Casts\BinaryUuid;
use MongoDB\Laravel\Eloquent\Model as Eloquent;
use MongoDB\Laravel\Eloquent\DocumentModel;

class IdIsBinaryUuid extends Eloquent
class IdIsBinaryUuid extends Model
{
protected $connection = 'mongodb';
use DocumentModel;

protected $primaryKey = '_id';
protected $keyType = 'string';
protected $connection = 'mongodb';
protected static $unguarded = true;
protected $casts = [
protected $casts = [
'_id' => BinaryUuid::class,
];
}
14 changes: 9 additions & 5 deletions tests/Models/IdIsInt.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

namespace MongoDB\Laravel\Tests\Models;

use MongoDB\Laravel\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\Model;
use MongoDB\Laravel\Eloquent\DocumentModel;

class IdIsInt extends Eloquent
class IdIsInt extends Model
{
protected $keyType = 'int';
protected $connection = 'mongodb';
use DocumentModel;

protected $primaryKey = '_id';
protected $keyType = 'int';
protected $connection = 'mongodb';
protected static $unguarded = true;
protected $casts = ['_id' => 'int'];
protected $casts = ['_id' => 'int'];
}
13 changes: 9 additions & 4 deletions tests/Models/IdIsString.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@

namespace MongoDB\Laravel\Tests\Models;

use MongoDB\Laravel\Eloquent\Model as Eloquent;
use Illuminate\Database\Eloquent\Model;
use MongoDB\Laravel\Eloquent\DocumentModel;

class IdIsString extends Eloquent
class IdIsString extends Model
{
protected $connection = 'mongodb';
use DocumentModel;

protected $primaryKey = '_id';
protected $keyType = 'string';
protected $connection = 'mongodb';
protected static $unguarded = true;
protected $casts = ['_id' => 'string'];
protected $casts = ['_id' => 'string'];
}
13 changes: 9 additions & 4 deletions tests/Models/Item.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
namespace MongoDB\Laravel\Tests\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use MongoDB\Laravel\Eloquent\Builder;
use MongoDB\Laravel\Eloquent\Model as Eloquent;
use MongoDB\Laravel\Eloquent\DocumentModel;

/** @property Carbon $created_at */
class Item extends Eloquent
class Item extends Model
{
protected $connection = 'mongodb';
protected $collection = 'items';
use DocumentModel;

protected $primaryKey = '_id';
protected $keyType = 'string';
protected $connection = 'mongodb';
protected string $collection = 'items';
protected static $unguarded = true;

public function user(): BelongsTo
Expand Down
Loading
Loading