Skip to content

Instantly share code, notes, and snippets.

@nenad-mitic-bg
Last active August 3, 2019 10:06
Show Gist options
  • Save nenad-mitic-bg/c204255122b21879c50eff454fdae001 to your computer and use it in GitHub Desktop.
Save nenad-mitic-bg/c204255122b21879c50eff454fdae001 to your computer and use it in GitHub Desktop.
For Medium article "Symfony EasyAdmin: complex forms"
<?php
/*
* A lot of stuff is omitted so that we can focus on the point of the article.
* The whole working project can be seen here: https://github.com/shonezlo/easy-admin-demo-complex-forms
*/
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use App\Model\Shipment;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Table(name="purchase")
* @ORM\Entity
*/
class Purchase
{
/**
* The customer billing address.
*
* @var StreetAddress
* @ORM\Embedded(class="StreetAddress")
*/
protected $billingAddress;
/**
* Constructor of the Purchase class.
* (Initialize some fields).
*/
public function __construct()
{
$this->billingAddress = new StreetAddress();
}
/**
* Set the address where the customer want its billing.
*
* @param StreetAddress $billingAddress
*/
public function setBillingAddress($billingAddress)
{
$this->billingAddress = $billingAddress;
}
/**
* @return StreetAddress
*/
public function getBillingAddress()
{
return $this->billingAddress;
}
// Omitted the rest of getters and setter for brevity
}
<?php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Embeddable
*/
class StreetAddress
{
/**
* @var string
* @ORM\Column
*/
private $line1;
/**
* @var string
* @ORM\Column(nullable=true)
*/
private $line2;
/**
* @var string
* @ORM\Column
*/
private $postCode;
/**
* @var string
* @ORM\Column
*/
private $city;
/**
* @var string
* @ORM\Column(length=2)
*/
private $country;
/**
* EasyAdmin uses __toString by default when showing stuff on list page.
*
* @return string
*/
public function __toString()
{
return $this->line1 . ($this->line2 ? ' ' : '') . $this->line2
. ' ' . $this->postCode . ' ' . $this->city
. ', ' . $this->country;
}
// Getters and setters omitted
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment