Model/Database Relationship? |
How can you link up the database relationships,
Such as $post = $post_model->find(123); $comments = $post->comments()->where(['active'=>true]); Something along these lines, changing models together to create relationships, without the need of calling each model separately. Post and Comments in separate tables of course. Ive tried to replicate this by creating a new object within one model to chain another, and failed to get it relating to what im looking for. Any ideas to accomplish this without the need of loading each model by themselves? UPDATE: `find()` method doesn't allow chaining. So what this means is relationships within the database have to be called individually. Which means, I'd have to put the ID into each one separately. The result objects wouldn't be allowed to access relationship content which is a bit of a downfall to the query system. I'd really like to avoid having to use find() a bunch of times just to get the content I'm looking for on 1 item. being able to call it dynamically is a huge benefit.
The only thing I can think about right now for this setup is something along the lines of this (creating a new method in the main item model, and linking all the other models together manually) really replacing the use of find() elsewhere.
public function getItem($id) { $post = $this->find($id); $files = new \App\Models\PostFiles(); $post->file = $files->where(['post_id'=>$id,'primary'=>'Y'])->first(); $images = new \App\Models\PostImages(); $post->image = $images->where(['post_id'=>$id,'primary'=>'Y'])->first(); $desc = new \App\Models\PostDescriptions(); $post->description = $desc->where(['post_id'=>$id,'primary'=>'Y'])->first(); return $post; } This way I can just call "$post->getItem(id)" and use the other tables for my relationship "$post->image->location" etc
You may want to consider getting this information in a single query using a join - it'll be a lot faster than running four separate queries to get the information, especially at scale.
(07-22-2016, 06:25 PM)JayAdra Wrote: You may want to consider getting this information in a single query using a join - it'll be a lot faster than running four separate queries to get the information, especially at scale. Yes of course. but that's why I was hoping CI4 had method chaining, like Laravel. Where it uses the query builder to automatically create the joins. (07-23-2016, 05:56 AM)idealcastle Wrote:(07-22-2016, 06:25 PM)JayAdra Wrote: You may want to consider getting this information in a single query using a join - it'll be a lot faster than running four separate queries to get the information, especially at scale. You modify the find() before you call it, not after. Code: public function getItem($id) or something like that.
(07-24-2016, 09:52 PM)kilishan Wrote:(07-23-2016, 05:56 AM)idealcastle Wrote:(07-22-2016, 06:25 PM)JayAdra Wrote: You may want to consider getting this information in a single query using a join - it'll be a lot faster than running four separate queries to get the information, especially at scale. Ok thanks kilishan, is that in the v4 documentation yet? |
Welcome Guest, Not a member yet? Register Sign In |