bnomei / kirby3-autoid

Automatic unique ID for Pages, Files and Structures including performant helpers to retrieve them. Bonus: Tiny-URL.
https://forum.getkirby.com/t/kirby3-autoid-plugin/23572
MIT License
80 stars 8 forks source link

$page->update not working with autoid #49

Open philipmarnef opened 4 years ago

philipmarnef commented 4 years ago

I have blueprint with an autoid field. If I try this:

$page = Page::create($props);
$page->update($newprops);

The updated fields are in the $page object, but the .txt file is not updated.

Problem is gone when I remove the autoid field.

philipmarnef commented 4 years ago

Following up: I didn't add

'autoid' => autoid()

to the $props array. When I do, it works.

I'm guessing the autoID is added in a hook which changes the page between create & update so $page->update() doesn't resolve?

bnomei commented 4 years ago

i never tried with Page::create but used $parent->createChild().

https://github.com/bnomei/kirby3-autoid/wiki/PHP:-autoid(),-Field-Method-fromAutoID()#create-a-pagefile-programmatically-and-retrieve-autoid

it would be a problem if the autoid registered hook at create changed the object and breaks expected behaviour. the only way around that is using a custom create method add add the autoid there https://getkirby.com/kosmos/45#kirby-tip-of-the-month

i will think about adding a AutoidPage to inherit from that avoids this.

class AutoidPage extends Page
{

  public static function create(array $props): Page
  {
    $props['autoid') = autoid(); // generate new
    return parent::create($props);
  }
}