user3174311
user3174311

Reputation: 1983

Symfony api-platform: user should retrieve his own entities

I have this entity

/**
 * @ApiResource()
 * @ORM\Entity(repositoryClass="App\Repository\FeedRepository")
*/
class Feed implements AuthoredEntityInterface
{
/**
 * @ORM\Id()
 * @ORM\GeneratedValue()
 * @ORM\Column(type="integer")
 */
private $id;

/**
 * @ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="myFeeds")
 * @ORM\JoinColumn(nullable=false)
 */
private $user;

/**
 * @ORM\Column(type="string", length=255)
 */
private $name;

/**
 * @ORM\Column(type="string", length=2083, unique=true)
 */
private $url;

// various getters and setters

}

using the AuthoredEntityInterface I made I can automatically set the user to the logged user.

I'd need to know how to set the collectionOperations so when I am logged in as the user with id = 1, when I call /api/feeds I will only retrieve items with user = 1. If this is possible I would like to do this with an annotation, otherwise any other method is ok.

thanks.

Upvotes: 1

Views: 1528

Answers (1)

Florian RADUREAU
Florian RADUREAU

Reputation: 111

If it is just for connected user, what you need is a current user extension (doctrine extension). Else, you need to create a "subresource' link.

Link to Extension, and to Subresource.

Enjoy :) (and thank you timisorean for the review)

Upvotes: 4

Related Questions