问题是:
<?php foreach ($users as $user): ?>
<?php $user = $user[0] ?>
错误:
=> Fatal error: Cannot use object of type User\Entity\User as array in ../module/User/view/user/index/index.phtml
我的课程是:
class IndexController extends ApplicationController
{
/**
* Store the user.
* @var \User\Entity\User
*/
protected $_user;
public function indexAction()
{
$offset = 6;
$page = $this->params()->fromQuery('page');
if (!$page) {
$page = 1;
}
$dql = "SELECT
users
FROM User\Entity\User users
GROUP BY users.id
ORDER BY " . $this->getOrderDql();
$query = $this->objectManager()
->createQuery($dql)
->setFirstResult(($page - 1) * $offset)
->setMaxResults($offset);
$users = new Paginator($query, $fetchJoinCollection = true);
$quantity = (int) ceil($users->count() / $offset);
return new ViewModel(
array(
'users' => $users,
'offset' => $offset,
'page' => $page,
'quantity' => $quantity,
'order' => $this->getOrderValue(),
)
);
}
}
感谢您的回答!
(文字使我的帖子不是代码)
答案 0 :(得分:0)
因为错误说你cant
将对象用作数组,
您需要按照以下方式访问该对象,
$user->'keyname';
或$user->'filedname'
代替$user[0]
;
其中keyname或field name是您要检索的字段的名称。