Welcome Guest, Not a member yet? Register   Sign In
How to troubleshoot code in a model?

As I can't echo stuff out from a controller, how do I troubleshoot it?

I've just been building a login system. As it involves calls to the database then creation of sessions and cookies and then more calls to the database, I've put it all in one model and that model reports either true or false back to the controller. Laying it out that way seemed logical to me.

The login system isn't working. No errors, just telling me I'm entering an incorrect username/password - which of course I'm not! Usually I'd move an echo around to different places to narrow down where the issue is, but with CI I obviously can't do that. So how can I get output from the model so I can debug?

[eluser]Harold Villacorte[/eluser]
This will help you out with queries.
Also CI has the Unit Testing class.


You can echo anywhere within Codeigniter, so if you want to echo in the controller or model go ahead, after you solved the issue then you can remove the echo.

I find the Profiler handy, put this in your conroller $this->output->enable_profiler(TRUE); this will show a report at the bottom of your page which includes:

Elapsed time of Benchmark points and total execution time
CodeIgniter Config variables
The Controller class and method requested
Any GET data passed in the request
The HTTP headers for the current request
Amount of memory consumed by the current request, in bytes
POST data passed in the request
Listing of all database queries executed, including execution time
The URI of the current request
The number of queries after which the query block will default to hidden.

I haven't been able to echo anything from models? In the end I had to pass back a string to the controller and show it in the view, and move the string about until I found the fail point (which was a miss named var). Obviously being able to simply echo would be a lot easier! So how comes echoing from model doesn't work for me?

Thanks for the tips on unit testing and profiling!


I assume your returning values from your Model, is your Echo after the return?

Yes I was returning a string from the model to the controller then passing it on to the view to do the echo. I couldn't echo direct from the model though.

What I meant was in your code if you have something like this

//my model

function someModel(){
//hey run some query
return $somequery;
echo $somequery . 'success i can see my model echo';

The echo won't work as it after the return, you would need to put it before, also maybe view the source within your browser and look for the word just in case the word has been outputted but you can't see it. Apart from that not sure why else you couldn't echo through the model unless you paste your code. Try a simple test, call a model function and just have a echo statement within it and see what happens.

Ah OK I see. I wasn't putting a return infront of the echo. Now it works. That should make things easier. Thanks for your help!

Theme © iAndrew 2016 - Forum software by © MyBB