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

Cannot update meta of existing OrderLine instance #1942

Closed
ken717w opened this issue Sep 9, 2024 · 6 comments · Fixed by #1949 or #1964
Closed

Cannot update meta of existing OrderLine instance #1942

ken717w opened this issue Sep 9, 2024 · 6 comments · Fixed by #1949 or #1964
Labels
bug Something isn't working unconfirmed

Comments

@ken717w
Copy link

ken717w commented Sep 9, 2024

  • Lunar version: 1.0.0-beta.1
  • Laravel Version: 11.22.0
  • PHP Version: 8.3.11
  • Database Driver & Version: MySQL 8.0.32

Expected Behaviour:

Successfully save the updated meta data to database.

Actual Behaviour:

Gets error "class_implements(): Class product_variant does not exist and could not be loaded".

Steps To Reproduce:

When creating the order line object, I followed the docs and set purchasable_type to product_variant.

$item = new OrderLine;
$item->purchasable_type = 'product_variant';
// ... Setting other attributes
$item->save();

// ... Do something else
$item->meta = [
  'key' => 'value',
]
$item->save();

Currently my workaround to make it works is to set purchasable_type to either \Lunar\Models\ProductVariant::class or get_class($variant).

@ken717w ken717w added bug Something isn't working unconfirmed labels Sep 9, 2024
@nathanjansen
Copy link

Isn't this a duplicate or at least a relation of #1939 ?

@ken717w
Copy link
Author

ken717w commented Sep 9, 2024

Isn't this a duplicate or at least a relation of #1939 ?

I'm not 100% sure because in my case I've extended Product but not ProductVariant which could make a difference.

@nathanjansen
Copy link

Isn't this a duplicate or at least a relation of #1939 ?

I'm not 100% sure because in my case I've extended Product but not ProductVariant which could make a difference.

That makes sense because the 'default' (Laravel) way right now is the actual class name so in this case Lunar\Models\ProductVariant
I think if you take a look at your order_lines table this is the morph model added to the purchasable_type column. Not the 'product_variant'. If you use this github version: https://github.com/theimerj/lunar.git. It should be 'product_variant'. But check your database.

@theimerj
Copy link
Contributor

Hey @ken717w, can you try by installing this version?

"require": {
    "lunarphp/lunar": "dev-update-container-resolution as 1.0.0-beta.1",
},
"repositories": [
    {
      "type": "vcs",
      "url": "https://github.com/theimerj/lunar.git"
    }
]

That directly corelated to #1933 which will hopefully fix most of the problems with extending.

@ken717w
Copy link
Author

ken717w commented Sep 14, 2024

@theimerj Thanks for the update. Your version works for me after I rollbacked the changes I mentioned here.

glennjacobs pushed a commit that referenced this issue Sep 17, 2024
This PR looks to solve some anomalies that have been discovered with
model extending.

## Problem A

When replacing a Lunar model, there are instances where the model
provided by Lunar is still used for example, if you replace the model
like so

```php
\Lunar\Facades\ModelManifest::replace(
    \Lunar\Models\Contracts\Order::class,
    \App\Models\Order::class
);
```

Later in the Lunar core, we are still referencing
`Lunar\Models\Order::first()` or via relationships
`$orderLine->order()`. We attempted to resolve these queries to their
concrete replacements by overriding the `newModelQuery` method, however
there were issues with castable attributes which caused them to be cast
multiple times, resulting in errors.

### Solution

This has been rewritten so Lunar doesn't try to `fill` the extended
instance of the model from the Lunar base class anymore and instead just
populates the attributes as they are. Since this should just be a simple
class swap it should no longer result in duplicate calls to attribute
casters.

There are some additional checks to see if we are actually working with
a model that hasn't been extended to ensure we are not getting into a
situation where we try and rehydrate with the same class.

### Affected issues

This change should resolve #1942 #1930 

## Problem B

If your own custom model was named something other than it's
counterpart, for example:

```php
\Lunar\Facades\ModelManifest::replace(
    \Lunar\Models\Contracts\Order::class,
    \App\Models\MyCustomOrder::class
);
```

This would result in the table name and subsequent foreign key naming to
be incorrect i.e. `my_custom_order` and `my_custom_order_id`. This would
mean developers would need to add their own methods to override this in
order for the naming to resolve properly, which is a bit of a
maintenance burden and easily missed when encountering errors.


### Solution

The `HasModelExtending` trait now provides its own `getTable` and
`getForeignKey` methods which will check which class within Lunar we are
extending and return the appropriate table name and foreign key.

## How this slipped through testing

Looks like there was an oversight and although there were tests for
extending models, the tests themselves didn't use any extending when
performing more complex tasks, like creating orders from carts.

This PR has now added some model extending to tests which were affected
by the issues referenced above to hopefully keep this in check and make
for a more "real world" test environment.

---------

Co-authored-by: alecritson <[email protected]>
@ken717w
Copy link
Author

ken717w commented Sep 19, 2024

#1949 doesn't seem to have fixed this issue. I can only get it to work by setting to lunar_order_lines.purchasable_type to Lunar\Models\ProductVariant.

alecritson added a commit that referenced this issue Oct 21, 2024
fixes #1942 

fix `class_implements` error on string 'product_variant'

Co-authored-by: Alec Ritson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working unconfirmed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants