The yii2model class in the first section.

Source: Internet
Author: User
Tags findone
The first section of yii2 code is used to check whether the user has created the log of the current day. If not, create it immediately and save it. To avoid a large number of queries to the database, it is saved to the session (maybe not good ?). Please refer to the advantages and disadvantages (correct logic): {code...} {... the first section of yii2 code, which is used to check whether the user has created the log of the current day. If not, create it immediately and save it.
To avoid a large number of queries to the database, it is saved to the session (maybe not good ?).
Please confirm the advantages and disadvantages (whether the logic is correct ):

#/models/log/DayLog.php#/models/log/DayLogInterface.php#/models/log/SystemLog.php#/models/log/MemberLog.php#/models/log/VistorLog.php
php#/Models/log/DayLog. phpnamespace app \ models \ log; use yii \ db \ ActiveRecord; class DayLog extends ActiveRecord {const SESSION_LOG_ID = 'x _ day_id '; const SESSION_LOG_DATE = 'x _ day_date '; public $ allowFields = []; /*** @ description: updates the statistics log ** @ param $ id * @ param $ field * @ param int $ num ** @ return bool */public function updateLog ($ id, $ field, $ num = 1) {if (in_array ($ field, $ this-> allowFields) {$ s = self: findOne ($ id ); if ($ s) {return $ s-> updateCounters ([$ field => $ num]) ;}} return false;} public function getLogId () {return \ Yii :: $ app-> session-> get ($ this: SESSION_LOG_ID);} public function getLogDate () {return \ Yii: $ app-> session-> get ($ this:: SESSION_LOG_DATE);} public function setLogId ($ id) {\ Yii: $ app-> session-> set ($ this: SESSION_LOG_ID, $ id );} public function setLogDate ($ date) {\ Yii: $ app-> session-> set ($ this: SESSION_LOG_DATE, $ date );} // when exiting, you must delete all public function clean () {\ Yii: $ app-> session-> remove ($ this: SESSION_LOG_ID); \ Yii :: $ app-> session-> remove ($ this: SESSION_LOG_DATE );}}
php#/models/log/DayLogInterface.phpnamespace app\models\log;interface DayLogInterface{    public function check();}
php#/Models/log/MemberLog. phpnamespace app \ models \ log; use yii \ base \ Event; use yii \ db \ ActiveRecord; use yii \ web \ User; class MemberLog extends DayLog implements DayLogInterface {const SESSION_LOG_ID = 'U _ day_id '; // log storage ID const SESSION_LOG_DATE = 'U _ day_date '; // Date saved in the log: YYYY-MM-dd public $ allowFields = []; public static function tableName () {return '{{% daylog_member }}';} public function check () {// If If (! \ Yii: $ app-> user-> isGuest) {$ logId = $ this-> getLogId (); $ logDate = $ this-> getLogDate (); $ today = date ('Y-m-d'); if ($ logId & $ logDate = $ today) {// log created on the current day} else {$ row = self: find ()-> where (['uid' => \ Yii :: $ app-> user-> getId (), 'date' => $ today])-> one (); if ($ row) {// The database has a daily record $ this-> setLogId ($ row-> id); $ this-> setLogDate ($ row-> date );} else {$ log = new self (); $ log-> date = $ today; $ log-> uid = \ Yii: $ app-> user-> getId (); if ($ log-> save () & $ log-> id) {$ this-> setLogId ($ log-> id ); $ this-> setLogDate ($ log-> date) ;}}// clear the original log Event: on (User: className (), User :: EVENT_BEFORE_LOGIN, [ActiveRecord: className (), 'clean']) ;}}

Reply content:

The first section of yii2 code is used to check whether the user has created the log of the current day. If not, create it immediately and save it.
To avoid a large number of queries to the database, it is saved to the session (maybe not good ?).
Please confirm the advantages and disadvantages (whether the logic is correct ):

#/models/log/DayLog.php#/models/log/DayLogInterface.php#/models/log/SystemLog.php#/models/log/MemberLog.php#/models/log/VistorLog.php
php#/Models/log/DayLog. phpnamespace app \ models \ log; use yii \ db \ ActiveRecord; class DayLog extends ActiveRecord {const SESSION_LOG_ID = 'x _ day_id '; const SESSION_LOG_DATE = 'x _ day_date '; public $ allowFields = []; /*** @ description: updates the statistics log ** @ param $ id * @ param $ field * @ param int $ num ** @ return bool */public function updateLog ($ id, $ field, $ num = 1) {if (in_array ($ field, $ this-> allowFields) {$ s = self: findOne ($ id ); if ($ s) {return $ s-> updateCounters ([$ field => $ num]) ;}} return false;} public function getLogId () {return \ Yii :: $ app-> session-> get ($ this: SESSION_LOG_ID);} public function getLogDate () {return \ Yii: $ app-> session-> get ($ this:: SESSION_LOG_DATE);} public function setLogId ($ id) {\ Yii: $ app-> session-> set ($ this: SESSION_LOG_ID, $ id );} public function setLogDate ($ date) {\ Yii: $ app-> session-> set ($ this: SESSION_LOG_DATE, $ date );} // when exiting, you must delete all public function clean () {\ Yii: $ app-> session-> remove ($ this: SESSION_LOG_ID); \ Yii :: $ app-> session-> remove ($ this: SESSION_LOG_DATE );}}
php#/models/log/DayLogInterface.phpnamespace app\models\log;interface DayLogInterface{    public function check();}
php#/Models/log/MemberLog. phpnamespace app \ models \ log; use yii \ base \ Event; use yii \ db \ ActiveRecord; use yii \ web \ User; class MemberLog extends DayLog implements DayLogInterface {const SESSION_LOG_ID = 'U _ day_id '; // log storage ID const SESSION_LOG_DATE = 'U _ day_date '; // Date saved in the log: YYYY-MM-dd public $ allowFields = []; public static function tableName () {return '{{% daylog_member }}';} public function check () {// If If (! \ Yii: $ app-> user-> isGuest) {$ logId = $ this-> getLogId (); $ logDate = $ this-> getLogDate (); $ today = date ('Y-m-d'); if ($ logId & $ logDate = $ today) {// log created on the current day} else {$ row = self: find ()-> where (['uid' => \ Yii :: $ app-> user-> getId (), 'date' => $ today])-> one (); if ($ row) {// The database has a daily record $ this-> setLogId ($ row-> id); $ this-> setLogDate ($ row-> date );} else {$ log = new self (); $ log-> date = $ today; $ log-> uid = \ Yii: $ app-> user-> getId (); if ($ log-> save () & $ log-> id) {$ this-> setLogId ($ log-> id ); $ this-> setLogDate ($ log-> date) ;}}// clear the original log Event: on (User: className (), User :: EVENT_BEFORE_LOGIN, [ActiveRecord: className (), 'clean']) ;}}

Disclaimer: I have never written about yii. I can only talk about it in the direction of OOP.

I think this code has a wrong dependency. Logically, it should be $ app-> user $ app-> session dependent on record, but now record depends on user and session.

So if I encapsulate it, this is probably the case.

phpclass MemberLog {    public function __construct($user) {        // find record by user        $this->record = findByUser($user);    }    public function check() {        $this->record->doSomething();    }}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.