Which is the most efficient method for gathering related data?
Lets say we have multiple projects that are associated to one category 1:M.
Each project holds the ID value of the Category it is a part of. I have an array object of all my projects, I now want to add to that array object its category name rather than its ID.
So for example instead of:
project_id: 1 project_title: first project project_desc: bla bla project_category_id: 1
I want it to be:
project_id: 1 project_title: first project project_desc: bla bla project_category: first category
I'm not having trouble achieving this, I'm curious what the most efficient way to achieve it is.
Option 1
$projects = $this->projects_model->find_all();
$categories = $this->category_model->find_all();
foreach($projects as $project){
foreach($categories as $category){
if($project->category_id == $category->id){
$project->category = $category;
}
}
}
Option 2
$projects = $this->projects_model->find_all();
foreach($projects as $project){
$project->category = $this->category_model->find_by('id', $project->category_id);
}
It's likely there are many other methods for achieving my desired result and potentially in a more efficient manor, if that's the case I'd love to learn.