P
P
prozrost2017-08-13 15:50:07
symfony
prozrost, 2017-08-13 15:50:07

How to get object from fosuserbundle?

Made a simple API for User, which is taken from fosUserBundle. I created a record in the database manually (such a thing, of course)
Now an error pops up:

{"error":{"code":500,"message":"Internal Server Error","exception":[{"message":"Notice: unserialize(): Error at offset 0 of 13 bytes","class":"Symfony\\Component\\Debug\\Exception\\ContextErrorException","trace":[{"namespace":"","short_class":"","class":"","type":"","function":"","file":"H:\\xampp\\htdocs\\rest_test\\vendor\\doctrine\\dbal\\lib\\Doctrine\\DBAL\\Types\\ArrayType.php","line":58,"args":[]},{"namespace":"Doctrine\\DBAL\\Types","short_class":"ArrayType","class":"Doctrine\\DBAL\\Types\\ArrayType","type":"->","function":"convertToPHPValue","file":"H:\\xampp\\htdocs\\rest_test\\vendor\\doctrine\\orm\\lib\\Doctrine\\ORM\\Internal\\Hydration\\SimpleObjectHydrator.php","line":131,"args":},

This is somehow connected with the fact that roles has an array type, maybe someone had this, how did you solve it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Kuznetsov, 2017-08-13
@prozrost

Does the user have connections?
I would recommend giving flat DTOs generated from the model. This DTO will contain only the data that is really needed.

flat class example
<?php

namespace AppBundle\Entity\Dto;

use AppBundle\Other\Arr;

/**
 * User
 * 
 */
class UserDTO
{

    /**
     *
     * @var int
     */
    public $id;

    /**
     *
     * @var string
     */
    public $login;

    /**
     *
     * @var int
     */
    public $dzoId;

    /**
     *
     * @var string
     */
    public $dzoName;

    /**
     * Roles
     * @var string
     */
    public $roles;

    /**
     *
     * @var string
     */
    public $fullname;

    /**
     *
     * @var string
     */
    public $email;

    public function __construct(\AppBundle\Entity\User $userInfo)
    {
        $this->id = $userInfo->getId();
        $this->login = $userInfo->getUsername();
        $dzo = $userInfo->getDzo();
        if ($dzo != null) {
            $this->dzoId = $dzo->getId();
            $this->dzoName = $dzo->getName();
        }
        $this->roles = $this->plainRoles($userInfo);
        $this->email = $userInfo->getEmail();
        $this->fullname = $userInfo->getFullname();
    }

    private function plainRoles(\AppBundle\Entity\User $user)
    {
        $roles = $user->getRoles();
        return join(',', $roles);
    }

    private function getRolesInfo($str)
    {
        $parts = explode('-::-', $str);
        $roles = [];

        foreach ($parts as $p) {
            $deserialized = \unserialize($p);
            $roles = array_merge($roles, $deserialized);
        }

        return join(',', array_unique($roles));
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question