Skip to content
New issue

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

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

Already on GitHub? # to your account

Call to undefined method stdClass::unsetRelation() #2671

Closed
antonioribeiro opened this issue Oct 15, 2024 · 13 comments · Fixed by #2672
Closed

Call to undefined method stdClass::unsetRelation() #2671

antonioribeiro opened this issue Oct 15, 2024 · 13 comments · Fixed by #2672

Comments

@antonioribeiro
Copy link
Member

antonioribeiro commented Oct 15, 2024

Description

We re getting this error when rendering blocks on preview. This is the line that might have introduced the bug.

Steps to reproduce

It's being generated by having browsers to related items in the block editor.

Versions

Twill version: 3.4
Laravel version: 11
PHP version: 8.3

/cc @Tofandel

@Tofandel
Copy link
Contributor

Tofandel commented Oct 15, 2024

$item is assumed to be a Model because it is coming from a relationship/query, which should absolutely have this method, why are you getting a stdClass instead of a model?

Did you somewhere maybe force the relation relatedItems or change the relatedItems method? Do you maybe have a global scope that's doing something it shouldn't?

@antonioribeiro
Copy link
Member Author

Hard to tell, can't dive deep down this now. But it's a block editor with many repeaters and related browsers to multiple modules:

Browser::make()
    ->name($browserName ?? 'linked_item')
    ->label('Internal page')
    ->modules(Constants::BROWSER_ALL_ENDPOINTS)
    ->connectedTo($prefix . 'type', 'internal', ['keepAlive' => true]),

@Tofandel
Copy link
Contributor

Tofandel commented Oct 15, 2024

This might be coming from this line which is doing something it shouldn't

$relatedItems->push(
(object) [
'related' => $repository->getById($item['id']),
'browser_name' => $browserName,
]
);
} catch (ReflectionException $reflectionException) {
Log::error($reflectionException);
}
});
});
$model->setRelation('relatedItems', $relatedItems);
}

@antonioribeiro
Copy link
Member Author

Yes, I just saw that $item is a stdClass that has the model in $item->related

@ifox
Copy link
Member

ifox commented Oct 15, 2024

@antonioribeiro by "when rendering blocks", do you mean inside Twill's editor, or your frontend controllers?

@antonioribeiro
Copy link
Member Author

For some reason we only have this on preview, @ifox, I forgot to add to the ticket

@ifox
Copy link
Member

ifox commented Oct 15, 2024

This other PR might be related: #2535

@Tofandel
Copy link
Contributor

Tofandel commented Oct 15, 2024

I was looking at it but it's not this, it's the code I pointed to that stores objects into the relatedItem relation, sending a PR in 3 minutes

@Tofandel
Copy link
Contributor

Btw @ifox I see a lot of Schema::hasTable(config('twill.related_table', 'twill_related')) in this class (which is a somewhat expensive query) running in a very heavy loop

Is there any actual case where this table would not exist? Isn't related a non disableable feature?

@ifox
Copy link
Member

ifox commented Oct 15, 2024

This was for backwards compatibility when the migrations were not all loaded automatically like they are now. This can be safely removed.

@Tofandel
Copy link
Contributor

Tofandel commented Oct 15, 2024

@antonioribeiro Any chance you could give a try to the PR?

Adding to your composer.json

    "repositories": [
        {
            "type": "vcs",
            "url": "https://github.com/tofandel/twill"
        }
    ],
    "require": {
        "area17/twill": "dev-patch-18 as 3.4.1"
    }

And then composer upgrade

@antonioribeiro
Copy link
Member Author

antonioribeiro commented Oct 15, 2024 via email

@cambad
Copy link
Contributor

cambad commented Oct 16, 2024

Good morning here! I tried it with the PR @Tofandel, and I can confirm it is all good, I can preview the pages with related content!

Thanks, @Tofandel, for the fix 🙏🏽 🥳

cc @antonioribeiro

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants