givewp
givewp copied to clipboard
Models should have a meta accessor
Details
As we build our concept of models, we'll need to include a way to access and persist related meta information that's not core to said model. For instance, our Donation model will include things like amount from it's meta that will be a core property. For meta that exists externally from add-ons that is not necessarily core to the model, we'll want a specific meta accessor.
Additional Context
Proposed Solution
- Create an abstract Meta class that models can extend assign as their meta property
- Have an easy way to get and set meta
Examples scaffolded from conversation with @JasonTheAdams
// Note that the meta does not save until the donation itself saves. Like the donation, the assigned meta values are
// simply stored until told to persist.
$donation = Donation::find(1);
$foo = $donation->meta->foo;
$donation->meta->bar = true;
$donation->meta->baz = 42;
$donation->save();
// retrieve value from single row
$donation->meta->get('my-value');
$donation->meta->my_value;
$donation->meta->myValue;
// save single row
$donation->meta->myValue = 123;
$donation->meta->set('my-value', 456);
$donation->meta->myArray = [1,2,3];
// retrieve from multiple rows
$donation->meta->getMany('my-values');
// save as multiple rows
$donation->meta->setMany('my-values', [1,2,3]);
Acceptance Criteria
- [ ] Models have a way to access and persist meta
This issue is stale because it has been open 45 days with no activity. Remove stale label or comment or this will be closed in 14 additional days.
This is still something I would like to think through more.
Just to quickly job down my thoughts. I think something like:
-
$donation->meta->some_meta_key;
-
$donation->meta->get('funky-key name');
-
$donation->meta->getAll('multi-row-meta');
-
$donation->meta->eagerLoad('keys', 'to', 'preload', 'all at once');
-
$donation->meta->cache(['meta key to store' => 'the value to store']);
Meta is retrieved on the fly and cached in the instance. If the value exists in the cache, then it's used instead of querying.