CodeIgniter Forums
How to troubleshoot code in a model? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: How to troubleshoot code in a model? (/showthread.php?tid=57092)



How to troubleshoot code in a model? - El Forum - 02-14-2013

[eluser]35mm[/eluser]
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?


How to troubleshoot code in a model? - El Forum - 02-14-2013

[eluser]Harold Villacorte[/eluser]
This will help you out with queries.
Code:
$this->db->last_query();
Also CI has the Unit Testing class.


How to troubleshoot code in a model? - El Forum - 02-14-2013

[eluser]Otemu[/eluser]
Hi,

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.


How to troubleshoot code in a model? - El Forum - 02-14-2013

[eluser]35mm[/eluser]
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!




How to troubleshoot code in a model? - El Forum - 02-14-2013

[eluser]Otemu[/eluser]
I assume your returning values from your Model, is your Echo after the return?



How to troubleshoot code in a model? - El Forum - 02-14-2013

[eluser]35mm[/eluser]
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.


How to troubleshoot code in a model? - El Forum - 02-14-2013

[eluser]Otemu[/eluser]
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.


How to troubleshoot code in a model? - El Forum - 02-14-2013

[eluser]35mm[/eluser]
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!