Welcome Guest, Not a member yet? Register   Sign In
Model/Database Relationship?
#1

(This post was last modified: 07-22-2016, 03:19 PM by idealcastle.)

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.
Reply
#2

(This post was last modified: 07-22-2016, 03:31 PM by idealcastle.)

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
Reply
#3

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.
Reply
#4

(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.
Reply
#5

https://bcit-ci.github.io/CodeIgniter4/d...lder::join
Keep calm.
Reply
#6

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

(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?
Reply
#8

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




Theme © iAndrew 2016 - Forum software by © MyBB