Skip to content
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

Merged
merged 6 commits into from
Apr 19, 2021
Merged

Conversation

mvorisek
Copy link
Member

@mvorisek mvorisek commented Apr 19, 2021

The removed methods were more or less confusing...

Model::asModel replacement:

see removed code, adjust for your usecase (that is why we removed this method, sometimes ID or default values should be copied, sometimes not)

Model::saveAs(string $class, array $options = []) replacement:

$model->asModel($class, $options)->save();

Model::newInstance(string $class = null, array $options = []) replacement:

new $class(null, $options);

This works even with $class beiing an object - https://3v4l.org/EfOiW

This PR also improve/simplify Model::asModel() method.

No change in atk4/ui repo needed.

@mvorisek mvorisek changed the title Remove Model::saveAs() method Remove Model::saveAs() and Model::newInstance() methods Apr 19, 2021
@mvorisek mvorisek removed the RTM label Apr 19, 2021
src/Model.php Outdated
// Copying only non-default value
$m->set($field, $value);
}
$m->set($field, $value);
Copy link
Member

Choose a reason for hiding this comment

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

You can do simply $m->set($this->data); here without loop.
But I'm not sure why you removed condition lines above. There was some logic - to not copy null and default values and also of course not copy id.

Copy link
Member Author

Choose a reason for hiding this comment

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

  • ignoring null - why? (I think too much crazy logic)
  • ignoring v = default - saves only a call, nothin else :) (should be removed)
  • ignoring ID - why (seems wrong to me, different model usually still targets the same data/table)

We would then need loadAs... This is too much methods for nothing.

$model = (self::class)::fromSeed([$class ?? static::class], $options);

if ($this->persistence) {
return $this->persistence->add($model); // @phpstan-ignore-line
Copy link
Member

Choose a reason for hiding this comment

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

This is the essence of newInstance(). At least for me.
It creates new model object and already links it with same persistence of old object.

Copy link
Member Author

@mvorisek mvorisek Apr 19, 2021

Choose a reason for hiding this comment

The 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 new $class($model->persistence), eg. pass the persistence manually when needed.

Safety is handled, all DB operations throws if persistence is not set.

@@ -434,17 +434,6 @@ This introduces a new business object, which is a sub-set of User. The new class
inherit all the fields, methods and actions of "User" class but will introduce one new
action - `send_gift`.

There are some advanced techniques like "SubTypes" or class substitution,
Copy link
Member

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 :)

Copy link
Member Author

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!

Copy link
Member

@DarkSide666 DarkSide666 left a comment

Choose a reason for hiding this comment

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

Is George around in Discord sometime? I would love to hear his opinion too on this.
I kind of understand that we can live without these methods (including asModel), but on other hand - why should we? Everything can be done manually in project-specific way, but it's also good if framework does some work for you (at least in one specific way and if you don't like that, then you can overwrite or extend).

So to wrap this up - I will approve this PR, but please try to discuss it also with George before merging. Maybe he can come up with better example of use-case then I could.

@mvorisek
Copy link
Member Author

mvorisek commented Apr 19, 2021

@georgehristov Please approve as well, asModel is not good as the copy conditions depends on use case and all core functions must behave strictly from the name itself, which is not possible here. newInstance is more or less like an alias for constructor and confusing for the developer. Even for me, and I know a lot of internal things... We cleared this with @DarkSide666, but we want to make sure our decisions are aligned together. Thanks.

Copy link
Collaborator

@georgehristov georgehristov left a comment

Choose a reason for hiding this comment

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

Ok, approving

@mvorisek mvorisek changed the title Remove Model::saveAs() and Model::newInstance() methods Remove Model::asModel, Model::saveAs() and Model::newInstance() methods Apr 19, 2021
@mvorisek mvorisek changed the title Remove Model::asModel, Model::saveAs() and Model::newInstance() methods Remove Model::{asModel, saveAs, newInstance} methods Apr 19, 2021
@mvorisek mvorisek merged commit c7c3fd4 into develop Apr 19, 2021
@mvorisek mvorisek deleted the rem_saveas branch April 19, 2021 21:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants