CodeIgniter Forums

Full Version: Model/Database Relationship?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.

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.

You modify the find() before you call it, not after.

Code:
public function getItem($id)
{
    $post = $this->join('post_files', 'post_files.post_id = posts.id AND post_files.primary = `Y`', 'inner')
                 ->join('post_images', 'post_images.post_id = posts.id  AND post_images.primary = `Y`', 'inner')
                 ->join('post_descriptions', 'post_descriptions.post_id = posts.id  AND post_descriptions.primary = `Y`', 'inner')
                 ->find($id);

    return $post;
}

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.

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.

You modify the find() before you call it, not after.

Code:
public function getItem($id)
{
$post = $this->join('post_files', 'post_files.post_id = posts.id AND post_files.primary = `Y`', 'inner')
->join('post_images', 'post_images.post_id = posts.id  AND post_images.primary = `Y`', 'inner')
->join('post_descriptions', 'post_descriptions.post_id = posts.id  AND post_descriptions.primary = `Y`', 'inner')
->find($id);

return $post;
}

or something like that.



Ok thanks kilishan, is that in the v4 documentation yet?
(08-01-2016, 07:48 AM)idealcastle Wrote: [ -> ]Ok thanks kilishan, is that in the v4 documentation yet?

Not explicitly, no. But consider all find(), get(), update(), etc type of methods to be the last method you would call in a Query Builder context, just like it's always been with CI and you'll be fine.