A
A
akula222016-01-13 21:03:48
Yii
akula22, 2016-01-13 21:03:48

Logging from action controllers?

I'm just learning Yii and OOP, before that I programmed with ordinary functions, so I wanted to ask about the correctness of such an implementation.
I want to log various actions of users on the site, for example, when adding a comment, write to the log.
Created model Log
* @property integer $id
* @property integer $created_at
* @property string $category
* @property integer $user_id
* @property string $username
* @property string $event
and method in it

public static function addLog($model)
    {
        if(Yii::$app->user->isGuest)
            return false;

        $log = new Log();
        $log->created_at = time();
        $log->user_id = Yii::$app->user->id;
        $log->username = Yii::$app->user->identity->username;
        $log->category = $model['category'];
        $log->event = $model['event'];
        $log->save();
    }

in the necessary controller actions I call like this
if ($model->load(Yii::$app->request->post()))
        { 
            if($model->save())
            {
                //  Отправляем в лог
                \app\modules\log\models\log::addLog(
                    [
                        'category' => 'comments',
                        'event' => 'Добавлен новый комментарий: ' . $model->post
                    ]
                );

Or does it have to be done differently?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
S
sunrails, 2016-01-13
@sunrails

Consider www.yiiframework.com/doc-2.0/guide-concept-events.html as a good option

A
Alexey Pavlov, 2014-07-15
@lexxpavlov

if (!mysql_query("SELECT ...") {
the mysql_query function does not return a result, but a resource - a pointer to the result to be passed to one of the mysql_fetch_* or mysql_result functions. The resource evaluates to true and the condition is always false.
In general, do not use the mysql_* functions anymore - they are deprecated and will soon be removed from php. Use PDO or mysqli. Read this for detailed explanations.

T
TiGR, 2014-07-15
@TiGR

1. The script will not output anything if any error occurs in the first request. Try adding error handling by adding an else clause and using the mysql_error and mysql_errno functions.
2. Field names are escaped with a backtick (`) rather than an apostrophe.
3. If you are writing new code, do not use obsolete parts of php. In particular, the mysql module has been replaced by mysqli and is not supported in new versions of php.

A
Alexander, 2014-07-15
@SashaSkot

First, you can simplify the check for an existing value to SELECT 1 FROM table where field = value.
Second, what is the error?

D
Dmitry Skogorev, 2014-07-15
@EnterSandman

read tulvit.net/post/162

C
Carry, 2014-07-16
@carryx

mysql_query - deprecated a long time ago

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question