givewp icon indicating copy to clipboard operation
givewp copied to clipboard

Models should have a meta accessor

Open jonwaldstein opened this issue 3 years ago • 3 comments

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

jonwaldstein avatar Feb 01 '22 15:02 jonwaldstein

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.

github-actions[bot] avatar Dec 02 '22 02:12 github-actions[bot]

This is still something I would like to think through more.

jonwaldstein avatar Dec 02 '22 21:12 jonwaldstein

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.

JasonTheAdams avatar Dec 02 '22 23:12 JasonTheAdams