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.
php
class MemberLog { public function __construct($user) { // find record by user $this->record = findByUser($user); } public function check() { $this->record->doSomething(); }}