Welcome Guest, Not a member yet? Register   Sign In
Use query result of one model in another model
#1

[eluser]bertcarremans[/eluser]
Hi,

I would like to use the result of a query in model A in another model B.
So what I have in my controller is this:

Code:
$data['show'] = $this->shows_model->get_show($id);

In the result of this query I get a photo_album_id. I first want to check whether there is
a photo_album_id available in the query result. If that is the case I want to extract rows from another table with that photo_album_id.

Does anyone know how I can do this?

Thanks!
#2

[eluser]CroNiX[/eluser]
something like
Code:
$this->load->model('shows_model');
$this->load->model('other_model');

$show = $this->shows_model->get_show($id);
if($show['id'] !== null) //some sort of check for id returned
{
  //we have an id, pass it to the other model and get the data
  $other_data = $this->other_model->other_method($show['id']);
}
#3

[eluser]bertcarremans[/eluser]
Hi CroNiX,

thanks for the quick answer. Unfortunately, I can't get it to work.

My controller contains this code:
Code:
$show = $this->shows_model->get_show($id);
        
        if ($show['photo_album_id'] !== null) {
            $data['photos'] = $this->media_model->get_photo_album($show['photo_album_id']);
        }

In the show_model I have:
Code:
function get_show($id) {
        $query = $this->db->query('SELECT * FROM shows WHERE id = '.$id);
        return $query->row();
    }

and in the media_model:
Code:
function get_photo_album($id) {
        $query = $this->db->query('SELECT * FROM photos WHERE album_id = '.$id);
        return $query->result_array();
    }

When I load the page I get the error: Fatal error: Cannot use object of type stdClass as array in C:\xampp\htdocs\ambassadors\application\controllers\site.php on line 200

So I cannot use
Code:
$show['id']
as an array. Does this have something to do with the fact that I return the query result as $query->row()?

Thanks!
#4

[eluser]CroNiX[/eluser]
Yeah. Probably just change
Code:
if ($show['photo_album_id'] !== null) {
to
Code:
if ($show->photo_album_id !== null) {
since you are returning the result as an object instead of an array in your model, and change it in the other place too.




Theme © iAndrew 2016 - Forum software by © MyBB