Replies: 3 comments
-
|
I am struggling with the exact same problem for weeks now. I've summarized some of my research here, if you want to take a look: For inputs, we don't have a satisfying result right now. For Outputs, we've end up to use a mix of serializer groups, with custom source/target attributes if needed. |
Beta Was this translation helpful? Give feedback.
-
|
It's really interesting, could we add this example on https://github.com/mtarld/sfcon-apip/ ? We're trying to get a nice reference for this out there then we'll fix our docs. |
Beta Was this translation helpful? Give feedback.
-
|
@solariox // Book entity
class Book
{
#[ORM\ManyToOne(inversedBy: 'books')]
private ?Author $author = null;
}
// Author Entity
// Multiple Map attribute not works automatically :(
#[Map(target: OutputAuthorDTO::class)]
#[Map(target: AnotherOutputAuthorDTO::class)]
class Author {
#[ORM\Column(length: 255)]
private ?string $name = null;
#[ORM\OneToMany(targetEntity: Book::class, mappedBy: 'author')]
private Collection $books;
}
// DTO 1
class OutputAuthorDTO
{
public string $name;
}
// DTO 2
class AnotherOutputAuthorDTO
{
public string $books;
}
// Book resource
#[ApiResource(
stateOptions: new Options(entityClass: Book::class)
)]
#[GetCollection(
uriTemplate: '/books',
provider: BookResourceProvider::class,
)]
#[Map(source: Book::class)]
class BookResource
{
public OutputAuthorDTO $author;
#[Map(source: 'author')]
public AnotherOutputAuthorDTO $anotherAuthor;
}With this config, I gets error: If I swap these 2 #[Map] attributes, then I gets error: So, I guess the last #[Map] attribute is taken into account. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Supose I have two Entity classes:
And also I have two DTO classes:
#[ApiResource( operations: [ new Get(), new GetCollection(), ], stateOptions: new Options(entityClass: BookEntity::class) )] class BookApi { public int $id; public string $title; public AuthorApi $author; } #[ApiResource( operations: [ new Get(), new GetCollection(), ], stateOptions: new Options(entityClass: AuthorEntity::class) )] class AuthorApi { public int $id; public string $name; /** * array<BookApi> */ public array $books; }$authoras AuthorApi object (in BookApi) and$booksas collection (array) of BookApi objects (in AuthorApi)?$booksfield as collection of BookApi, but should be fetch BookEntity objects behind the scene?I don't want mix up entities with DTO classes, for example:
I tried to use "symfony/object-mapper" but no chance so far.
Thanks for Your help.
Beta Was this translation helpful? Give feedback.
All reactions