20.11.2014 Views

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

O Guia Definitivo do Yii 1.1

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Note, code blocks need to be nested properly. That is, a code block cannot intersect with<br />

another. It must be either at a parallel level or be completely enclosed by the other code<br />

block.<br />

To show profiling result, we need to install a CLogRouter application component with a<br />

CProfileLogRoute log route. This is the same as we <strong>do</strong> with normal message routing. The<br />

CProfileLogRoute route will display the performance results at the end of the current page.<br />

Profiling SQL Executions<br />

Profiling is especially useful when working with database since SQL executions are often<br />

the main performance bottleneck of an application. While we can manually insert<br />

beginProfile and endProfile statements at appropriate places to measure the time spent in<br />

each SQL execution, starting from version 1.0.6, <strong>Yii</strong> provides a more systematic approach<br />

to solve this problem.<br />

By setting CDbConnection::enableProfiling to be true in the application configuration,<br />

every SQL statement being executed will be profiled. The results can be readily displayed<br />

using the aforementionedCProfileLogRoute, which can show us how much time is spent in<br />

executing what SQL statement. We can also call CDbConnection::getStats() to retrieve the<br />

total number SQL statements executed and their total execution time.<br />

Erros<br />

<strong>Yii</strong> provides a complete error handling framework based on the PHP 5 exception<br />

mechanism. When the application is created to handle an incoming user request, it<br />

registers its handleError method to handle PHP warnings and notices; and it registers its<br />

handleException method to handle uncaught PHP exceptions. Consequently, if a PHP<br />

warning/notice or an uncaught exception occurs during the application execution, one of<br />

the error handlers will take over the control and start the necessary error handling<br />

procedure.<br />

Tip: The registration of error handlers is <strong>do</strong>ne in the application's constructor by calling<br />

PHP functions set_exception_handler and set_error_handler. If you <strong>do</strong> not want <strong>Yii</strong> to<br />

handle the errors and exceptions, you may define constant<br />

YII_ENABLE_ERROR_HANDLER and YII_ENABLE_EXCEPTION_HANDLER to be<br />

false in the entry script.<br />

By default, errorHandler (or exceptionHandler) will raise an onError event (or onException<br />

event). If the error (or exception) is not handled by any event handler, it will call for help<br />

from the errorHandler application component.<br />

Raising Exceptions<br />

Raising exceptions in <strong>Yii</strong> is not different from raising a normal PHP exception. One uses<br />

the following syntax to raise an exception when needed:<br />

throw new ExceptionClass('ExceptionMessage');

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!