Yii2显示最近添加的会员

时间:2015-03-09 07:06:59

标签: php sorting date yii2

我有一个“最新员工”面板,它应该显示我最近添加的用户。我所拥有的只是这些用户的静态显示:

<div class="box box-danger <?= !User::isBizAdmin() ? 'hidden' : '' ?>">
    <div class="box-header with-border">
      <h3 class="box-title">Latest Staff Members</h3>
      <div class="box-tools pull-right">
        <span class="label label-danger">8 New Members</span>
        <button class="btn btn-box-tool" data-widget="collapse"><i class="fa fa-minus"></i></button>
        <button class="btn btn-box-tool" data-widget="remove"><i class="fa fa-times"></i></button>
      </div>
    </div><!-- /.box-header -->
    <div class="box-body no-padding">
      <ul class="users-list clearfix">
        <li>
          <img src="https://cdn2.iconfinder.com/data/icons/avatar-2/512/frank_man-128.png" alt="User Image"/>
          <a class="users-list-name" href="#">Alexander Pierce</a>
          <span class="users-list-date">Today</span>
        </li>
        <li>
          <img src="https://cdn2.iconfinder.com/data/icons/avatar-2/512/Ben_man-128.png" alt="User Image"/>
          <a class="users-list-name" href="#">Norman</a>
          <span class="users-list-date">Yesterday</span>
        </li>
        <li>
          <img src="https://cdn2.iconfinder.com/data/icons/avatar-2/512/iri_girl_face-128.png" alt="User Image"/>
          <a class="users-list-name" href="#">Jane</a>
          <span class="users-list-date">12 Jan</span>
        </li>
        <li>
          <img src="https://cdn2.iconfinder.com/data/icons/avatar-2/512/Jay_man-128.png" alt="User Image"/>
          <a class="users-list-name" href="#">John</a>
          <span class="users-list-date">12 Jan</span>
        </li>
        <li>
          <img src="https://cdn2.iconfinder.com/data/icons/avatar-2/512/Fred_man-128.png" alt="User Image"/>
          <a class="users-list-name" href="#">Alexander</a>
          <span class="users-list-date">13 Jan</span>
        </li>
        <li>
          <img src="https://cdn2.iconfinder.com/data/icons/avatar-2/512/michela_face_young-128.png" alt="User Image"/>
          <a class="users-list-name" href="#">Sarah</a>
          <span class="users-list-date">14 Jan</span>
        </li>
        <li>
          <img src="https://cdn2.iconfinder.com/data/icons/avatar-2/512/carla_girl-128.png" alt="User Image"/>
          <a class="users-list-name" href="#">Nora</a>
          <span class="users-list-date">15 Jan</span>
        </li>
        <li>
          <img src="https://cdn2.iconfinder.com/data/icons/avatar-2/512/hena_woman_face-128.png" alt="User Image"/>
          <a class="users-list-name" href="#">Nadia</a>
          <span class="users-list-date">15 Jan</span>
        </li>
      </ul><!-- /.users-list -->
    </div><!-- /.box-body -->
    <div class="box-footer text-center">
        <a href="javascript::" class="uppercase">View All Users</a>
    </div><!-- /.box-footer -->
</div>

这样的事情。现在,在我的数据库的user表中,我添加了一个date_added列,并手动将日期放入行中。

我是否循环数据?或者使用小部件?我不知道我应该如何实现这一点。

修改

我的控制器:

public function actionIndex()
{
    if(Yii::$app->user->isGuest){
        return $this->render('../site/index');
    }
    else{  
        $latestStaffMembers = User::find()->orderBy(['date_added' => SORT_DESC])->limit(8);   
        return $this->render('dashboard', [
            'latestStaffMembers' => $latestStaffMembers,
        ]);
    }        
}

在我看来:

<?php foreach ($latestStaffMembers as $user) { ?>
  <li>
      <img src="<?php echo $user->avatar; ?>" alt="User Image"/>
      <a href="#"><?= $user->username ?></a>
      <span ><?= $user->date_added ?></span>
  </li>
<?php } ?> 

然后我收到此错误:

  

尝试获取非对象的属性

我尝试了var-dumping $user,它说null

1 个答案:

答案 0 :(得分:1)

我不确定这是不是你想要的。我希望它有所帮助。

在您的控制器操作中:

$latestStaffMembers = User::find()->orderBy(['date_added' => SORT_DESC])->limit(8)->all();
return $this->render('some-view-file', [
    'latestStaffMembers' => $latestStaffMembers,
]);

在您看来:

<?php foreach ($latestStaffMembers as $user) { ?>
    <li>
        <img src="<?= $user->imagePath ?>" alt="User Image"/>
        <a class="users-list-name" href="#"><?= $user->name ?></a>
        <span class="users-list-date"><?= Yii::$app->formatter->asDate($user->dateAdded, 'd m'); ?></span>
    </li>
<?php } ?>

顺便说一下:我在你的代码中看到了!User::isBizAdmin()。是你的吗?如果重要的是只有管理员可以看到,我不会隐藏列表。如果用户不是管理员,请不要打印。