【问题标题】:check field weather expired or not while loading a page cakephp在加载页面 cakephp 时检查字段天气是否过期
【发布时间】:2014-07-07 11:43:54
【问题描述】:

以下函数通过将横幅表中的状态字段设置为 0 来检查横幅是否足够老以使其处于非活动状态。

/** * * 检查天气横幅是否足够老,可以转回非高级 * 并将状态更改为 0 ,以防非高级 * * @return 无效 */

   protected function check_status(){
     $non_premium_expire = 5*24*60*60;
     $premium_expire = 14*24*60*60;
     $data = $this->Banner->find('all');
     foreach ($data as $Banners) {
            if ($Banners['Banner']['is_premium'] == 0) {
                if ((time() - strtotime($Banners['Banner']['created'])) > $premium_expire) {
                     $this->Banner->id = $Banners['Banner']['id'];
                     $this->Banner->saveField('status',0);
                }
            else if ($Banners['Banner']['is_premium'] == 1) {
                if ((time() - strtotime($Banners['Banner']['created'])) > $non_) {
                    $this->Banner->id = $Banners['Banner']['id'];
                    $this->Banner->saveField('status',0);
                }
            }
     }
   }
}

来自 mybanners() 函数。即用户打开时应调用 check_status() 函数 /站点名称/我的横幅

我已经从下面显示的 mybanners() 函数中调用了 check_status() 函数

public function mybanners()
    {


        $this->check_status();
        $this->layout = 'index';
        $this->loadModel('Banner'); 
        $loggedUserId   = $this->Auth->user('id');

我的问题是,即使尝试了 set() 和 updateAll() 函数,数据库中的一个字段状态也没有得到更新。有什么解决办法吗?我是 cakephp 的新手

【问题讨论】:

    标签: database save cakephp-2.0


    【解决方案1】:

    我想通了。工作代码如下。

    ** 
    *
    * Checks weather the banners are old enough to be turned back to non-premium
    * and change the status to 0 , in case of non-premium
    * 
    * @return void 
    */
    
       protected function check_status(){
         $non_premium_expire = 5*24*60*60;
         $premium_expire = 14*24*60*60;
         $data = $this->Banner->find('all');
         //pr ($Banners);
         //pr ((time() - strtotime($Banners['Banner']['modified'])) > 5*24*60*60);
         //pr ((time() - strtotime($Banners['Banner']['modified'])) > 14*24*60*60);
         //die();
         foreach ($data as $Banners) {
                if ($Banners['Banner']['is_premium'] == 0) {
                    if ((time() - strtotime($Banners['Banner']['modified'])) > $non_premium_expire) {
                         $id = $Banners['Banner']['id'];
                         //pr ("in non premium loop");
                         //pr ($id);
                         //die();
                         $data_one = array('id' => $id , 'status'=> 0, 'modified' => false);
                         $this->Banner->save($data_one);
                         //$this->Banner->clear();
                    }
                }
                if ($Banners['Banner']['is_premium'] == 1) {
                    if ((time() - strtotime($Banners['Banner']['modified'])) > $premium_expire) {
                         $id = $Banners['Banner']['id'];
                         //pr ("in premium loop");
                         //pr ($id);
                         //die();
                         $data_one = array('id' =>$id , 'status'=> 0, 'modified' => false);
                        //pr ($data_one);
                        //die();
                         $this->Banner->save($data_one);
                         //$this->Banner->clear();
                    }
                }
         }
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-08
      • 1970-01-01
      相关资源
      最近更新 更多