getkirby / getkirby.com

Source code and content for the Kirby website
https://getkirby.com
134 stars 271 forks source link

Hidden model methods: how to know that they are reserved names? #2363

Open 3tlam opened 3 months ago

3tlam commented 3 months ago

Description

Hi all!

I think I ran into a bug with a field named inventory and accessing it in a page model. The output is:

array(3) {
  ["children"]=>
  array(0) {
  }
  ["files"]=>
  array(0) {
  }
  ["template"]=>
  string(4) "home"
}

Expected behavior
Access to the field inventory, instead I think I get the result of $users->inventory()^, so a list of files

Additional context
Maybe this is expected behaviour, but as far as I could find in the docs, it should only be part of Kirby\Cms\User?

Thank you for all the hard work and the wonderful CMS!

To reproduce

# /site/blueprints/pages/default.yml
title: Default Page
type: page

fields:
  inventory:
    type: textarea
# /site/models/default.php
<?php

use Kirby\Cms\Page;

class DefaultPage extends Page
{
  public function myCustomMethod()
  {
    var_dump($this->inventory());
  }
}
# /site/templates/default.php
<?php $page->myCustomMethod() ?>

Your setup

K 4.3.0 Plainkit

distantnative commented 3 months ago

There is also Page::inventory() as core method which is why you cannot use it as field name (or if you need to access it via $page->content()->get('inventory').

The problem is that the method is marked as internal (which is right as it shouldn't be used programatically by non-expert user), but that's also why it's hidden in the docs reference. Which then of course makes it hard for you to know that it's a reserved name.

3tlam commented 3 months ago

Heya

Thanks for the quick reply; yeah I was able to access it using content()

Given your explanation, I am now not sure if this works as expected or not - so feel free to close this :)

distantnative commented 3 months ago

Totally expected in code, but a problem in our docs, which is why I moved this to our website repo so we find a solution for our docs.