0

Let's say in my model, I have a function that queries two separate tables. I need to pass the results back to my controller to display in my view.

I'm using MongoDB but it should be the same for any other DB. Normally this would work.

$files = $grid->find(array("username" => $profile_id, 
                     "thumbnail" => array('$ne' => true)) );

$return files;

But, I need to go a step further and check to see if the user has a default photo selected.

$getCount = $grid->count(array("username" => $profile_id, 
                     "default" => array('$ne' => true)) );

If I recall correctly, I would normally do ...

$return array($files, $getCount);

Doesn't work though.

3 Answers 3

2

Figured it out ... in my function, I did the following.

$files['data'] = $grid->find(array("username" => $profile_id, 
                 "thumbnail" => array('$ne' => true)) );

$files['count'] = $grid->count(array("username" => $profile_id, 
                  "default" => array('$ne' => true)) );

$return files;

In my view, I would manipulate my data accordingly.

echo $files['count'];

foreach( $files['data'] as $obj) {
    ...
}
Sign up to request clarification or add additional context in comments.

Comments

2

To do it the way you wanted to in your original post it'd be something like:

$return array('files' => $files, 'getCount' => $getCount);

1 Comment

Did you know, you can insert 4 spaces before some code to make it a code block. You can also achieve that by selecting a big block of code and clicking on the Code Sample button. ;)
0

I'd split it into two separate model functions. I think its cleaner than fetching and returning the two separate pieces of data from the one function.

1 Comment

Though I ve implemented the solution proposed by the op this one looks much clearer and superior from a design point of view. Cause having in your views something like : <?php echo $employees['emp_count']; ?> causes extra mess....

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.