-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Move model to a trait #2580
Changes from 1 commit
855ff8e
c8d6c42
ef12043
114f918
16ff436
8e24ed7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
||
/** | ||
* List of field names to unset from the document on save. | ||
|
@@ -150,7 +141,7 @@ public function freshTimestamp() | |
/** @inheritdoc */ | ||
public function getTable() | ||
{ | ||
return $this->collection ?: parent::getTable(); | ||
return $this->collection ?? parent::getTable(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 */ | ||
|
@@ -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 */ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,13 @@ abstract class Model extends BaseModel | |
*/ | ||
protected $keyType = 'string'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the trait is used, the properties |
||
|
||
/** @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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method replaces |
||
{ | ||
return is_subclass_of($related, BaseModel::class) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This class continues to extend |
||
{ | ||
use DocumentModel; | ||
|
||
protected $primaryKey = '_id'; | ||
protected $keyType = 'string'; | ||
protected $connection = 'mongodb'; | ||
protected $collection = 'casting'; | ||
protected string $collection = 'casting'; | ||
|
||
protected $fillable = [ | ||
'uuid', | ||
|
There was a problem hiding this comment.
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.