• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Entities and pivots

Hi all- this is probably only partially CI related, but since CI4 is introducing me to Entities for the first time I figured it might others as well. I'm wondering what the best practice is for an entity handling its pivot table(s)? Consider this example:

id, firstname, lastname, email, deleted, created_at, updated_at

id, name, color, deleted, created_at, updated_at

user_id, widget_id, created_at

My simple User model return type is 'App\Entities\User', and my user Entity handles the normal stuff. Now, am iterating through $users->findAll() and want to display each user's widgets. In CodeIgniter 3 I would have a function in my user model $this->User->widgets($user->id) that returned widget_id from users_widgets, and then could use $this->Widget->get($widget_id) if I needed the whole row.
The same could be accomplished in my entity by defining getWidgets() and then I could use $user->widgets - is that how folks typically do it? Or other recommendations / example repos to check out? Do people pass around IDs or entire data rows (e.g. does getWidgets() return an int or an object/array)?
Thanks for the help!

FWIW I ended up going with the same general idea of what I did in CI3 - my Entity has a "getWidgets()" function that returns an array of the IDs from the pivot table, which can be used as-is or with the Widget model to get the full object. This makes it very convenient to reference "$user->widgets" without needing to load the widget model in my controller.

Okay I’m running into this again in a library I’m making - seriously, how do people usually handle pivots in a model? In this case I have a users table and model, a groups table and model, and a groups_users pivot table for the many-to-many... If I want to get a user’s groups what does that look like in the model or entity? Anyone have ideas on best practices, common solutions? Currently I’m doing a join but only selecting the target table, but I feel like that has neither the efficient g of just grabbing IDs nor the robustness of returning an actual entity.

I would do it similar to how you've done it in the past. I actually just wrote an article covering this.

Good read! Thanks for sharing that. Your coverage of the optimization is making me realize that the way I originally did this saved on memory but was a lot of extra calls to the database. I’ll plan to implement something more like yours in my CI4 endeavors.

Not trying to beat the dead horse, but I keep coming back to this. While I like the method you outlines generally, it still feels a little weird passing around an array of modified objects with their pivots injected. Without trying to create an ORM, I've been thinking about single-run storehouse idea ("cache lite"), where anytime an entity needed its relations it would check the storehouse first before calling the DB. That way you could still preload a batch of them and save the multiple database calls, and the relations would be available anywhere for that run.
I'm curious what you (and others) think. Right now I'm coding the storehouse as a static property of any model that needs it, but considering it as a separate library.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.