statamic / cms

The core Laravel CMS Composer package
https://statamic.com
Other
3.71k stars 508 forks source link

Programatically creating Entry with Parent fails #9569

Closed plexus77 closed 7 months ago

plexus77 commented 7 months ago

Bug description

I am programatically creating ordered entries and while the entry MD looks fine the parent/child relationships are not visible on the tree list screen or when editing the child

Collection config

title: Areas
template: default
layout: layout
taxonomies:
  - ranges
revisions: false
sort_dir: asc
date_behavior:
  past: public
  future: private
preview_targets:
  -
    label: Entry
    url: '{permalink}'
    refresh: true
structure:
  root: false
  max_depth: 4
  slugs: true

Parent

---
id: 6ecce8f4-0681-4638-baaf-9a480fd8bf78
blueprint: area
title: External
updated_by: 1
updated_at: 1708567891
---

Child

---
id: 79d22b0b-320c-4c90-9c03-60e9c2cc4a35
blueprint: area
title: Bricks
parent: 6ecce8f4-0681-4638-baaf-9a480fd8bf78
updated_by: 1
updated_at: 1708568062
---

Is this a bug or is there something I'm missing here?

How to reproduce

When I look at the entries in the tree view there is no parent child relationship, when I edit the child entry there is no parent visible and when I try to add one and save it doesn't work ie there is no relationship visible on the list page and when I return to the edit page it is unset again (even though the parent field is still present in the MD). I drag the child into position press save and then everything seems to work from there on (note that there is no change to the parent field in the md).

If I manually remove the parent field from the md and then use the admin ui to link the entry to it's parent it creates markdown exactly the same as the above.

Logs

No response

Environment

Laravel Version: 10.45.1
PHP Version: 8.1.17
Composer Version: 2.7.1
Environment: local
Debug Mode: ENABLED
Maintenance Mode: OFF

Cache
Config: NOT CACHED
Events: NOT CACHED
Routes: NOT CACHED
Views: CACHED

Drivers
Broadcasting: pusher
Cache: redis
Database: mysql
Logs: stack / single
Mail: smtp
Queue: redis
Session: file

Statamic
Addons: 6
Antlers: runtime
Sites: 1
Stache Watcher: Disabled
Static Caching: Disabled
Version: 4.49.0 PRO

Statamic Addons
aniket-magadum/insta-feeds: 1.0.1
mitydigital/iconamic: 1.1.6
ncla/statamic-focal-point-fieldtype: 3.0.0
rias/statamic-redirect: 3.6.2
statamic/collaboration: 0.8.1
tresdstudioweb/dynamic-select: 1.0.1

Installation

Fresh statamic/statamic site via CLI

Antlers Parser

None

Additional details

No response

ryanmitchell commented 7 months ago

I assume when you are saying you add it programmatically you mean you are using parent in the data() ?

If so, thats not the way - see https://github.com/statamic/cms/issues/5457 for context.

You need to append it to the collection structure, 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) {
    if (!$entryParent) {
        $collectionStructure->append($entry);
    } else {
        $collectionStructure->appendTo($entryParent->id(), $entry->id());
    }
    $collectionStructure->save();
});
plexus77 commented 7 months ago

Thanks for the response and context… and yes I was just using the data array… I did have a look in the entry repository class but wasn’t sure how to add it into the structure. Where and how is that information stored? I expected more than just a parent value in order to store the entry’s position in the structure but that’s not present in the MD. Also why should an aftersave be needed? How is this information saved via the admin?

ryanmitchell commented 7 months ago

The parent entry in the MD is a legacy thing and a mistake (as Jason mentioned in the linked issue).

The tree structure is held in the collection tree, which is seperate from entries.

plexus77 commented 7 months ago

Oh the trees directory!! Ok thanks, I think I under stand now....

ryanmitchell commented 6 months ago

Yep the trees directory