0

User entity

namespace AppBundle\Entity;

use JMS\Serializer\Annotation\Expose;
use JMS\Serializer\Annotation\Groups;
use JMS\Serializer\Annotation\VirtualProperty;
use JMS\Serializer\Annotation\ExclusionPolicy;

use Doctrine\ORM\Mapping as ORM;
use FOS\UserBundle\Model\User as BaseUser;
use FOS\UserBundle\Model\Group;

/**
 * User
 *
 * @ORM\Table(name="user")
 * @ORM\Entity(repositoryClass="AppBundle\Repository\UserRepository")
 * @ExclusionPolicy("all")
 */
class User extends BaseUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Expose
     */
    protected $id;

    /**
     * @ORM\Column(type="integer")
     */
    private $balance = 0;

    /**
     * Set balance
     *
     * @param integer $balance
     *
     * @return User
     */
    public function setBalance($balance)
    {
        $this->balance = $balance;

        return $this;
    }

    /**
     * Get balance
     *
     * @return integer
     */
    public function getBalance()
    {
        return $this->balance;
    }
}

Controller:

namespace Backend\UserBundle\Controller;

use AppBundle\Entity\User;
use FOS\RestBundle\Controller\FOSRestController;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use FOS\RestBundle\Controller\Annotations as Rest;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

/**
 * Class UsersController
 * @package Backend\UserBundle\Controller
 */
class UsersController extends FOSRestController
{
    public function allUsersAction()
    {
        $users = $this->getDoctrine()->getRepository('AppBundle:User')->findAll();

        $view = $this->view($users, 200);
        return $this->handleView($view);
    }

    /**
     * @Rest\View
     */
    public function getUserAction($id)
    {
        $user = $this->getDoctrine()->getRepository('AppBundle:User')->find($id);

        if (!$user instanceof User) {
            throw new NotFoundHttpException('User not found');
        }

        $view = $this->view($user, 200);
        return $this->handleView($view);
    }
}

И при запросе на /api/v1/users/1.json он мне пишет:

{  
   "id":1,
   "username":"admin",
   "username_canonical":"admin",
   "email":"admin",
   "email_canonical":"admin",
   "enabled":true,
   "salt":"o12yxgxp3vkk0w4sck80408w8s8o84s",
       "password":"$2y$13$o12yxgxp3vkk0w4sck804uSVjSMSB1W0qwEjunGTHomBqqoGvkW9G",
   "last_login":"2016-02-28T17:28:19+03:00",
   "locked":false,
   "expired":false,
   "roles":[  
      "ROLE_ADMIN"
   ],
   "credentials_expired":false
}
0

1 ответ 1

0

Как так не скрывает?

У вас в выводе отстуствует поле balance, как вы и задали + ваш класс наследует от BaseUser, в котором другая "политика исключения". Скорее всего вам стоит поискать в гугле что-то вроде "JSM Serializer and parent class". И в репозитории JSM Serializer'а уже есть issue по этому поводу

0

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.