-
Notifications
You must be signed in to change notification settings - Fork 47
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
Remove Model::{asModel, saveAs, newInstance} methods #856
Changes from all commits
be4d79d
cad526c
2793c8c
80a3192
44a9a49
961cb98
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1287,23 +1287,6 @@ public function duplicate() | |
return $duplicate; | ||
} | ||
|
||
/** | ||
* Saves the current record by using a different | ||
* model class. This is similar to:. | ||
* | ||
* $m2 = $m->newInstance($class); | ||
* $m2->load($m->getId()); | ||
* $m2->set($m->get()); | ||
* $m2->save(); | ||
* | ||
* but will assume that both models are compatible, | ||
* therefore will not perform any loading. | ||
*/ | ||
public function saveAs(string $class, array $options = []): self | ||
{ | ||
return $this->asModel($class, $options)->save(); | ||
} | ||
|
||
/** | ||
* Store the data into database, but will never attempt to | ||
* reload the data. Additionally any data will be unloaded. | ||
|
@@ -1327,47 +1310,9 @@ public function saveAndUnload(array $data = []) | |
return $this; | ||
} | ||
|
||
/** | ||
* This will cast Model into another class without | ||
* loosing state of your active record. | ||
*/ | ||
public function asModel(string $class, array $options = []): self | ||
{ | ||
$m = $this->newInstance($class, $options); | ||
|
||
foreach ($this->data as $field => $value) { | ||
if ($value !== null && $value !== $this->getField($field)->default && $field !== $this->id_field) { | ||
// Copying only non-default value | ||
$m->set($field, $value); | ||
} | ||
} | ||
|
||
// next we need to go over fields to see if any system | ||
// values have changed and mark them as dirty | ||
|
||
return $m; | ||
} | ||
|
||
/** | ||
* Create new model from the same base class | ||
* as $this. | ||
* | ||
* @return static | ||
*/ | ||
public function newInstance(string $class = null, array $options = []) | ||
{ | ||
$model = (self::class)::fromSeed([$class ?? static::class], $options); | ||
|
||
if ($this->persistence) { | ||
return $this->persistence->add($model); // @phpstan-ignore-line | ||
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 is the essence of newInstance(). At least for me. 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. I am aware, I can not be fixed in the constructor as construtor is not aware of the previous instance/persistence. The solution is to call Safety is handled, all DB operations throws if persistence is not set. |
||
} | ||
|
||
return $model; | ||
} | ||
|
||
/** | ||
* Create new model from the same base class | ||
* as $this. If you omit $id,then when saving | ||
* as $this. If you omit $id then when saving | ||
* a new record will be created with default ID. | ||
* If you specify $id then it will be used | ||
* to save/update your record. If set $id | ||
|
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.
And this was quite a nice feature we used in one project :)
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.
See code below, it tries to return different class from load method.
Notice, that a lot of custom code is needed already, you can adjust the code for the removed method easily. Usually, in the case below, you want to retain ID!