CodeIgniter Forums
Model/Database Relationship? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Model/Database Relationship? (/showthread.php?tid=65777)



Model/Database Relationship? - idealcastle - 07-22-2016

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.


RE: Model/Database Relationship? - idealcastle - 07-22-2016

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


RE: Model/Database Relationship? - JayAdra - 07-22-2016

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.


RE: Model/Database Relationship? - idealcastle - 07-23-2016

(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.


RE: Model/Database Relationship? - arma7x - 07-23-2016

https://bcit-ci.github.io/CodeIgniter4/database/query_builder.html?highlight=join#CodeIgniterDatabaseBaseBuilder::join


RE: Model/Database Relationship? - kilishan - 07-24-2016

(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.


RE: Model/Database Relationship? - idealcastle - 08-01-2016

(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?


RE: Model/Database Relationship? - kilishan - 08-01-2016

(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.