Chapters
27 Chapters
|
2:54:11
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.1.3
Subscribe to download the code!Compatible PHP versions: ^7.1.3
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Este tutorial funciona muy bien para Symfony 5 y la Plataforma API 2.5/2.6.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-ctype": "*",
"ext-iconv": "*",
"api-platform/core": "^2.1", // v2.4.3
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // 1.10.2
"doctrine/doctrine-bundle": "^1.6", // 1.11.2
"doctrine/doctrine-migrations-bundle": "^2.0", // v2.0.0
"doctrine/orm": "^2.4.5", // v2.7.2
"nelmio/cors-bundle": "^1.5", // 1.5.5
"nesbot/carbon": "^2.17", // 2.19.2
"phpdocumentor/reflection-docblock": "^3.0 || ^4.0", // 4.3.1
"symfony/asset": "4.2.*|4.3.*|4.4.*", // v4.3.11
"symfony/console": "4.2.*", // v4.2.12
"symfony/dotenv": "4.2.*", // v4.2.12
"symfony/expression-language": "4.2.*|4.3.*|4.4.*", // v4.3.11
"symfony/flex": "^1.1", // v1.21.6
"symfony/framework-bundle": "4.2.*", // v4.2.12
"symfony/security-bundle": "4.2.*|4.3.*", // v4.3.3
"symfony/twig-bundle": "4.2.*|4.3.*", // v4.2.12
"symfony/validator": "4.2.*|4.3.*", // v4.3.11
"symfony/yaml": "4.2.*" // v4.2.12
},
"require-dev": {
"symfony/maker-bundle": "^1.11", // v1.11.6
"symfony/stopwatch": "4.2.*|4.3.*", // v4.2.9
"symfony/web-profiler-bundle": "4.2.*|4.3.*" // v4.2.9
}
}
4 Comments
Hi, is there a way to disable removing in that case, without throwing any exception/validation ? kind of patching my array collection with new data, but i dont want old data be deleted, if I don't provide them inside my PUT payload ?
Hi ahmedbhs!
Apologies for the slow reply - vacation last week, and your question was left for me personally! :)
Yes, I think you CAN do this, but it's entirely up to YOUR code to do it. Behind the scenes, iirc, ApiPlatform (via the serializer I believe) looks at the current collection and the submitted collection and finds the "difference". It calls addCheeseListing() for any new items and removeCheeseListing() for any removed items. So, in theory, you could make your removeCheeseListing() do nothing ;) That feels... a bit odd and potentially dangerous to me, but I think that IS the path... I can't think of another way.
Cheers!
Hi,
I have a question about adding items to a collection property in many-to-many relations with extra property in bridge table.
Example is sports players and teams. A player can be in multiple teams and teams can have multiple players. Also team can have player(s) as captains.
Here is the table schema:
`
Players
--
id
name
teams
Teams
--
id
name
players
PlayersTeams
--
player
team
is_captain
`
And here is the shortened Entities:
`
// App/Entity/Players.php
/**
*/
private $teams
/**
*/
public function getTeams(): Collection {
return $this->teams;
}
public function addTeam(Teams $team): self {
}
public function removeTeam(Teams $team): self {
}
// App/Entity/PlayersTeams.php
/**
*/
private $id;
/**
*/
private $team;
/**
*/
private $player;
/**
*/
private $isCaptain = false;
public function getId(): ?int {
return $this->id;
}
public function getTeam(): Teams {
return $this->team;
}
public function setTeam(Teams $team): self {
$this->team = $team;
return $this;
}
public function getPlayer(): Players {
return $this->player;
}
public function setPlayer(Players $player): self {
$this->player = $player;
return $this;
}
public function getIsCaptain(): bool {
return $this->isCaptain;
}
public function setIsCaptain(bool $isCaptain): self {
$this->isCaptain = $isCaptain;
return $this;
}
`
When I call
/player/1PUT API, I get the following response: "Expected value of type \"App\Entity\PlayersTeams\" for association field \"App\Entity\Players#$teams\", got \"App\Entity\Teams\" instead."How can I add items to the embedded objects in this entity relations?
Thank you for your tips!
Hey Sung,
It sounds like you're trying to set a Teams entity instead of PlayersTeams entity to the Players::$teams property. It sounds like you have an invalid annotation mapping for this property, shouldn't it be "App\Entity\Teams" instead? Otherwise, you should set PlayersTeams entity instead of Teams.
Btw, I'd recommend you also to check your Doctrine mapping configuration with the command:
$ bin/console doctrine:schema:validate
Make sure you have a valid mapping in your application. If you do - most probably you have a logic mistake somewhere.
I hope this helps!
Cheers!
"Houston: no signs of life"
Start the conversation!