Skip to content

Conversation

@jasonvarga
Copy link
Member

@jasonvarga jasonvarga commented Feb 16, 2022

With all the recent updates to working with items in Blade to make it seem more like eloquent, we thought why just make a wrapper? Let's make the entry/term/etc classes have magic properties and methods like eloquent models.

Overview

Magic properties

Calling $entry->something will get the evaluated augmented value. ie. It will not be wrapped in a Value class.

$entry->id; // 123
$entry->title; // "The title"
$entry->toggle; // true / false
$entry->related_posts; // EntryCollection<Entry>

Magic methods

Calling $entry->something() will get a query builder if the something field if a fieldtype that augments to a query builder (e.g. an entries fieldtype). This will allow you to continue chaining a query, just like an Eloquent Model relationship.

$entry->related_posts()->where('featured', true)->get();
$entry->toggle_field();  // BadMethodCallException

Breaking Changes

There are two breaking changes, both pretty minor.

First is the ContainsData trait. If you were using that, the __get() method has been removed.
If you were also using the HasAugmentedData or HasAugmentedInstance traits, it was moved in there, so no change is necessary. Otherwise, you can add this to your class:

    public function __get($key)
     {
         return $this->get($key);
     }

Next change is that previously calling a property would get the raw data on that entry, without any fallback logic.

Now, it'll consider the entry itself, any origin entries, the collection cascade, etc.

It's only really going to affect you in two situations:

  1. If you were using property access on a field that has a different end result if you were to augment it.

    For example, an entries field.

    $entry->related_posts;
    // before: ['id1', 'id2']
    // after: EntryCollection([Entry, Entry])

    For basic fields, like text, toggle, etc, there's no change:

    $entry->textfield; // before and after: 'the string'
  2. If you were intentionally wanting the value on the entry, without factoring in fallbacks to origin entries or collection cascade:

    # collection/blog.yaml
    title: Blog
    inject:
      hello: world
    # origin.md
    id: 1
    foo: bar
    # no "hello" key.
    # entry.md
    id: 2
    origin: 1
    # no "foo" or "hello" key
    // before
    $entry->foo; // null
    $entry->hello; // null
    
    // after
    $entry->foo; // 'bar'
    $entry->hello; // 'world'

To get the previous behavior, you can do $entry->get('fieldname')

Todo:

  • Apply to augmentable items:
    • Entry
    • Page
    • Structure
    • LocalizedTerm
    • Asset
    • Global Variables
    • Taxonomy
    • Collection
    • Site
    • Blueprint
    • User
    • User Role
    • User Group
  • Revert Add toValuesCollection macro #5286 since it'll be rendered pointless.

@ryanmitchell
Copy link
Contributor

You are making it too easy! :) These changes are brilliant.

@jasonvarga jasonvarga marked this pull request as ready for review February 18, 2022 15:02
@jasonvarga jasonvarga changed the title Entries have magic properties and methods Items have magic properties and methods Feb 18, 2022
jasonvarga added a commit that referenced this pull request Feb 21, 2022
- Remove the has/get/set/data methods so they delegate to ContainsData which does the same thing.
- The data method will always return a collection.
- The data method used to filter out fields that weren't in the blueprint. This was a leftover thing from v2 before we moved to v3 style validation. Now it won't do any filtering.

This goes along with #5297 but moving into its own PR to avoid noise there.
@jasonvarga jasonvarga merged commit e5c03aa into master Feb 22, 2022
@jasonvarga jasonvarga deleted the feature/model-ish-accessors branch February 22, 2022 17:45
@jasonvarga jasonvarga mentioned this pull request Feb 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants