Symfony RestBundle注释视图

时间:2016-07-04 11:54:58

标签: php symfony fosrestbundle

在我早期的行动中,我有像数组一样的响应,但随后响应变化就像对象一样,我不能理解为什么。函数findBitByProject没有改变 - 返回

$results = $query->getResult();

我的行动

    $bits = $this->getDoctrine()->getManager()
        ->getRepository('ArtelProfileBundle:Bit')
        ->findBitByProject($request);  

    //$bits return array<Bit>

    return View::create()
        ->setStatusCode(200)
        ->setData($bits)
        ->setSerializationContext(
            SerializationContext::create()
                ->setGroups(
                    array('for_project_bit')
                )
        );
提前回应

[
  {}
  {}
]

现在

{
0: {}
1: {}
}

我的出价等级

/**
 * Bit.
*
* @ORM\Table(name="bit")
* @ORM\HasLifecycleCallbacks
* @ORM\Entity(repositoryClass="Artel\ProfileBundle\Entity\Repository\BitRepository")
* @Gedmo\SoftDeleteable(fieldName="deletedAt")
* @Annotation\ExclusionPolicy("all")
*/
class Bit
{
const NOT_APPROPRIATE       = 'not_appropriate';
const WAITING_FOR_DECISION  = 'waiting_for_decision';
const CONFIRM               = 'confirmed';
const STATUS_SEND_CLIENT    = 'send_client';
const WAITING_FOR_FEEDBACK  = 'waiting_for_feedback';
const STATUS_SENT_CV        = 'sent_cv';
const STATUS_UNRESOLVED     = 'unresolved';

use Timestampable;

/**
 * @var int
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 * @Annotation\Expose()
 * @Annotation\Groups({
 *      "get_client_by_id", "get_bid_id", "for_project_bit", "for_project_bit_admin", "for_all_projects",
 *      "get_entity_bits", "get_all_projects", "for_projects", "get_all_workorders", "get_workorder"
 * })
 */
private $id;

/**
 * @var Developer
 *
 * @ORM\ManyToOne(targetEntity="Developer", inversedBy="bit", cascade={"persist"})
 * @ORM\JoinColumn(name="developer_id", nullable = true, referencedColumnName="id")
 * @Annotation\Expose()
 * @Annotation\Groups({
 *      "get_client_by_id", "get_bid_id", "for_project_bit", "for_project_bit_admin", "for_all_projects", "get_entity_bits",
 *      "for_projects", "get_all_projects"
 * })
 */
protected $developerId;

/**
 * @var Users
 *
 * @ORM\ManyToOne(targetEntity="Users", inversedBy="bid", cascade={"persist"})
 * @ORM\JoinColumn(name="author_id", nullable = true, referencedColumnName="id")
 * @Annotation\Expose()
 * @Annotation\Groups({
 *      "get_client_by_id", "get_bid_id", "for_project_bit", "for_project_bit_admin", "for_all_projects", "get_entity_bits",
 *      "for_projects", "get_all_projects"
 * })
 */
protected $author;

/**
 * @var Project
 *
 * @ORM\ManyToOne(targetEntity="Project", inversedBy="bit")
 * @ORM\JoinColumn(name="project_id", referencedColumnName="id", onDelete="CASCADE")
 * @Annotation\Expose()
 * @Annotation\Groups({"for_all_projects", "for_project_bit", "for_project_bit_admin", "get_entity_bits"})
 */
private $projectId;

/**
 * @var float
 *
 * @Assert\NotBlank(groups={"post_bid"})
 * @ORM\Column(name="rate", type="float")
 * @Annotation\Expose()
 * @Annotation\Groups({
 *      "get_client_by_id", "get_bid_id", "for_projects", "for_project_bit", "for_project_bit_admin",
 *      "for_all_projects", "get_entity_bits"
 * })
 */
private $rate;

当我想要开发者关系的序列化器组时,每个人都可以

为什么这会发生在我身上以及如何解决? :)

1 个答案:

答案 0 :(得分:0)

尝试以下方法:

$view = View::create()
    ->setStatusCode(200)
    ->setData($bits)
    ->setSerializationContext(
        SerializationContext::create()
            ->setGroups(
                array('for_project_bit')
            )
    );

return $this->handleView($view);