-
Notifications
You must be signed in to change notification settings - Fork 78
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
Parent and URI not populating when creating a Child entry with Eloquent 4.0 / Statamic 5 #284
Comments
The parent field isn't the right approach to creating a child, you need to add it to the tree. Something like: $collection = Collection::findByHandle('handle');
$collectionStructure = $collection->structure()->in('default');
// make a new entry
$entry = Entry::make()
->collection('handle')
->locale('default')
->slug($termSlug)
->published(true)
->data([...]);
// parent of entry
$entryParent = Entry::newQuery()->where('x', 'y');
// after save, update tree
$entry->afterSave(function ($entry) use ($entryParent, $collectionStructure) {
$collectionStructure->syncOriginal(); // this seems to be necessary or you end up with an error about flip() when creating multiple entries in the same request
if (!$entryParent) {
$collectionStructure->append($entry);
} else {
$collectionStructure->appendTo($entryParent->id(), $entry->id());
}
$collectionStructure->save();
}); |
Thank you - I will try that.
…--
Kind Regards,
Dian Kolarov
Increate Ltd
T: 02037720000 | 07971921142
***@***.***
On 10/05/2024 12:40, Ryan Mitchell wrote:
The parent field isn't the right approach to creating a child, you
need to add it to the tree. Something like:
$collection =Collection::findByHandle('handle');
$collectionStructure =$collection->structure()->in('default');
// make a new entry
$entry =Entry::make()
->collection('handle')
->locale('default')
->slug($termSlug)
->published(true)
->data([...]);
// parent of entry
$entryParent =Entry::newQuery()->where('x','y');
// after save, update tree
$entry->afterSave(function ($entry)use ($entryParent,$collectionStructure) {
$collectionStructure->syncOriginal();// this seems to be necessary or you end up with an error about flip()
when creating multiple entries in the same request
if (!$entryParent) {
$collectionStructure->append($entry);
}else {
$collectionStructure->appendTo($entryParent->id(),$entry->id());
}
$collectionStructure->save();
});
—
Reply to this email directly, view it on GitHub
<#284 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AC6SWHSXBK4MJE5JVAXJYFLZBSWZHAVCNFSM6AAAAABHQO2WE6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMBUGQ2TSMRWHE>.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Hi Ryan,
This all worked fine, but I am getting the error below on
CollectionStructure->save() and I get the same error in the Control
Panel as well, if I make a change to the order of entries or try to
delete one.
I have upgraded to Statamic 5 / Eloquent 4 yesterday and did not had
this issue before, but I also wonder if deleting entries directly form
the database (while running tests) can cause something like this? Can't
really think of another reason.
…On 10/05/2024 12:40, Ryan Mitchell wrote:
The parent field isn't the right approach to creating a child, you
need to add it to the tree. Something like:
$collection =Collection::findByHandle('handle');
$collectionStructure =$collection->structure()->in('default');
// make a new entry
$entry =Entry::make()
->collection('handle')
->locale('default')
->slug($termSlug)
->published(true)
->data([...]);
// parent of entry
$entryParent =Entry::newQuery()->where('x','y');
// after save, update tree
$entry->afterSave(function ($entry)use ($entryParent,$collectionStructure) {
$collectionStructure->syncOriginal();// this seems to be necessary or you end up with an error about flip()
when creating multiple entries in the same request
if (!$entryParent) {
$collectionStructure->append($entry);
}else {
$collectionStructure->appendTo($entryParent->id(),$entry->id());
}
$collectionStructure->save();
});
—
Reply to this email directly, view it on GitHub
<#284 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AC6SWHSXBK4MJE5JVAXJYFLZBSWZHAVCNFSM6AAAAABHQO2WE6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMBUGQ2TSMRWHE>.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
What error? Can you also post the result of ‘php please support:details’ |
Hi,
It looks like the screenshot did not go through - the error is
"Undefined array key 1 and I am providing screenshot on url
(https://cdn.frontserve.net/screenshot.png) and there is the error log
entry below...
Support output below...
Environment
Application Name: wfs.co.uk
Laravel Version: 10.48.10
PHP Version: 8.2.18
Composer Version: 2.7.2
Environment: local
Debug Mode: ENABLED
URL: wfs.local
Maintenance Mode: OFF
Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: CACHED
Views: CACHED
Drivers
Broadcasting: log
Cache: statamic
Database: mysql
Logs: stack / single
Mail: mailersend
Queue: sync
Session: file
Statamic
Addons: 3
Sites: 1
Stache Watcher: Disabled
Static Caching: Disabled
Version: 5.0.1 PRO
Statamic Addons
jacksleight/statamic-bard-texstyle: 3.2.1
statamic/eloquent-driver: 4.0.0
statamic/seo-pro: 6.0.0
Statamic Eloquent Driver
Asset Containers: eloquent
Assets: eloquent
Blueprints: eloquent
Collection Trees: eloquent
Collections: eloquent
Entries: eloquent
Forms: eloquent
Global Sets: eloquent
Global Variables: eloquent
Navigation Trees: eloquent
Navigations: eloquent
Revisions: eloquent
Taxonomies: eloquent
Terms: eloquent
|
Github doesn't allow markdown in email replies, so the stack trace is:
|
The error is strange as it's hitting the stache repositories for collections and trees, when your config is showing you using eloquent. I'll see if I can get some time on this early next week, if no one else beats me to it. |
Hi Ryan,
You were right - I fixed the issue by replacing the "Home page" in the
collection with a new one and clearing all cache. Not sure how this
relates, but I did start from file based set up and later moved to
eloquent and I did stache:clear and static:clear before reporting this.
Not sure if this was a one-off or needs further investigation, but your
reply hinted a direction - thanks!
***@***.***
…On 10/05/2024 16:33, Ryan Mitchell wrote:
The error is strange as it's hitting the stache repositories for
collections and trees, when your config is showing you using eloquent.
I'll see if I can get some time on this early next week, if no one
else beats me to it.
—
Reply to this email directly, view it on GitHub
<#284 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AC6SWHS2WOLYIRT6KQ42UK3ZBTSEFAVCNFSM6AAAAABHQO2WE6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMBUHAYTIOBYGM>.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Ah brilliant. Thanks for the update. |
Trying to create a child entry in eloquent set up (Statamic5, Eloquent 4.0), by setting the URI and the parent id of new entry as shown in code below, but they will remain unchanged. The Entry points to Home, instead of the parent and the URI remains null after saving the entry. Slug and all other fields are updated properly.
$entry = Entry::make()->collection('pages')->slug($slug_article);
$entry->site('default');
$entry->blueprint('pages');
$entry->date(Carbon::parse('now'));
$entry->published(true);
$entry->status('published');
$entry->locale('default');
$entry->set("uri", "/".$parent->slug."/".$slug_article);
$entry->data([
parent' => $parent->id,
...
]);
$entry->save();
The text was updated successfully, but these errors were encountered: