简体   繁体   中英

Symfony RestBundle Annotations View

In my action early I have response like array, but then response change like object and I not undestand why. Function findBitByProject not change - return

$results = $query->getResult();

my action

    $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')
                )
        );

response early

[
  {}
  {}
]

now

{
0: {}
1: {}
}

my bid class

/**
 * 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;

when I desable serializer group for developer relation everythin ok

why this happenes with me and how to fix ? :)

Try the following:

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

return $this->handleView($view);

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