Skip to content

Update getting-started.md - Examples with PHP 8.0 #1244

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 15, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions core/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,37 +44,39 @@ use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;

/**
* @ApiResource
* @ORM\Entity
*/
#[ApiResource]
class Product // The class name will be used to name exposed resources
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;
private ?int $id = null;

/**
* @var string $name A name property - this description will be available in the API documentation too.
* A name property - this description will be available in the API documentation too.
*
* @ORM\Column
* @Assert\NotBlank
*/
public $name;
#[Assert\NotBlank]
public string $name = '';

// Notice the "cascade" option below, this is mandatory if you want Doctrine to automatically persist the related entity
/**
* @var Offer[]|ArrayCollection
*
* @ORM\OneToMany(targetEntity="Offer", mappedBy="product", cascade={"persist"})
*/
public $offers;
public iterable $offers;

public function __construct()
{
$this->offers = new ArrayCollection(); // Initialize $offers as a Doctrine collection
}

public function getId(): ?int
{
return $this->id;
Expand All @@ -93,7 +95,7 @@ class Product // The class name will be used to name exposed resources
$offer->product = null;
$this->offers->removeElement($offer);
}

// ...
}
```
Expand All @@ -111,35 +113,33 @@ use Symfony\Component\Validator\Constraints as Assert;
/**
* An offer from my shop - this description will be automatically extracted from the PHPDoc to document the API.
*
* @ApiResource(iri="http://schema.org/Offer")
* @ORM\Entity
*/
#[ApiResource(iri: 'http://schema.org/Offer')]
class Offer
{
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
private ?int $id = null;

/**
* @ORM\Column(type="text")
*/
public $description;
public string $description = '';

/**
* @ORM\Column(type="float")
* @Assert\NotBlank
* @Assert\Range(min=0, minMessage="The price must be superior to 0.")
* @Assert\Type(type="float")
*/
public $price;
#[Assert\Range(minMessage: 'The price must be superior to 0.', min: 0)]
public float $price = -1.0;

/**
* @ORM\ManyToOne(targetEntity="Product", inversedBy="offers")
*/
public $product;
public ?Product $product = null;

public function getId(): ?int
{
Expand Down