简体   繁体   中英

Fatal error: Cannot use object of type User\Entity\User as array

The problem is:

<?php foreach ($users as $user): ?>
<?php $user = $user[0] ?>

error:

=> Fatal error: Cannot use object of type User\Entity\User as array in ../module/User/view/user/index/index.phtml

My class is:

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(),
        )
    );
}

}

Thanks for the answers!

(text to make my post not mostly code)

as the error says you cant use the object as array,

you need to access the object as following way,

$user->'keyname'; or $user->'filedname' instead of $user[0] ;

where keyname or field name is name of your field that you want to retrieve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM