Skip to content

Instantly share code, notes, and snippets.

@taiebb
Last active March 10, 2016 09:39
Show Gist options
  • Save taiebb/dcd9d86115ec08ed7eec to your computer and use it in GitHub Desktop.
Save taiebb/dcd9d86115ec08ed7eec to your computer and use it in GitHub Desktop.
The required option "em" is missing.
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Address
*
* @ORM\Table(name="address")
* @ORM\Entity(repositoryClass="AppBundle\Repository\AddressRepository")
* @ORM\HasLifecycleCallbacks
*/
class Address {
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="country", type="string", length=255, nullable=true)
*/
private $country;
/**
* @var string
*
* @ORM\Column(name="state", type="string", length=255, nullable=true)
*/
private $state;
/**
* @var string
*
* @ORM\Column(name="city", type="string", length=255, nullable=true)
*/
private $city;
/**
* @var string
*
* @ORM\Column(name="street", type="text")
*/
private $street;
/**
* @var string
*
* @ORM\Column(name="token", type="string", length=255, unique=true, nullable=true)
*/
private $token;
/**
* @var \DateTime
*
* @ORM\Column(name="created_at", type="datetime")
*/
private $createdAt;
/**
* @var \DateTime
*
* @ORM\Column(name="updated_at", type="datetime", nullable=true)
*/
private $updatedAt;
/**
* Get id
*
* @return integer
*/
public function getId() {
return $this->id;
}
/**
* Set country
*
* @param string $country
* @return Address
*/
public function setCountry($country) {
$this->country = $country;
return $this;
}
/**
* Get country
*
* @return string
*/
public function getCountry() {
return $this->country;
}
/**
* Set street
*
* @param string $street
* @return Address
*/
public function setStreet($street) {
$this->street = $street;
return $this;
}
/**
* Get street
*
* @return string
*/
public function getStreet() {
return $this->street;
}
/**
* Set state
*
* @param string $state
* @return Address
*/
public function setState($state) {
$this->state = $state;
return $this;
}
/**
* Get state
*
* @return string
*/
public function getState() {
return $this->state;
}
/**
* Set city
*
* @param string $city
* @return Address
*/
public function setCity($city) {
$this->city = $city;
return $this;
}
/**
* Get city
*
* @return string
*/
public function getCity() {
return $this->city;
}
/**
* Set token
*
* @param string $token
* @return Address
*/
public function setToken($token) {
$this->token = $token;
return $this;
}
/**
* Get token
*
* @return string
*/
public function getToken() {
return $this->token;
}
/**
* Set createdAt
*
* @param \DateTime $createdAt
* @return Address
*/
public function setCreatedAt($createdAt) {
$this->createdAt = $createdAt;
return $this;
}
/**
* Get createdAt
*
* @return \DateTime
*/
public function getCreatedAt() {
return $this->createdAt;
}
/**
* Set updatedAt
*
* @param \DateTime $updatedAt
* @return Address
*/
public function setUpdatedAt($updatedAt) {
$this->updatedAt = $updatedAt;
return $this;
}
/**
* Get updatedAt
*
* @return \DateTime
*/
public function getUpdatedAt() {
return $this->updatedAt;
}
}
<?php
class AddressType extends AbstractType {
private $em;
public function __construct($em) {
$this->em = $em;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
// cela suppose que le gestionnaire d'entité a été passé en option
$entityManager = $options['em'];
$propertyPathToCity = 'city';
$builder
->add('street')
;
$builder
->addEventSubscriber(new AddCountryFieldSubscriber($propertyPathToCity, $this->em, $builder->getFormFactory()))
->addEventSubscriber(new AddStateFieldSubscriber($propertyPathToCity, $this->em, $builder->getFormFactory()))
->addEventSubscriber(new AddCityFieldSubscriber($propertyPathToCity, $this->em, $builder->getFormFactory()))
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Address'
));
$resolver->setRequired(array(
'em',
));
$resolver->setAllowedTypes('em', 'Doctrine\Common\Persistence\ObjectManager');
}
}
<?php
class CandidateType extends AbstractType {
protected $em;
public function __construct($em) {
$this->em = $em;
}
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options) {
$builder
->add('firstName')
->add('lastName')
->add('address', new AddressType($this->em))
;
}
/**
* @param OptionsResolver $resolver
*/
public function configureOptions(OptionsResolver $resolver) {
$resolver->setDefaults(array(
'data_class' => 'AppBundle\Entity\Candidate'
));
$resolver->setRequired(array(
'em',
));
$resolver->setAllowedTypes('em', 'Doctrine\Common\Persistence\ObjectManager');
}
}
<?php
class JobSeekerController extends Controller {
public function editProfileAction(Request $request, Account $account) {
$candidate = $account->getCandidate();
$em = $this->getDoctrine()->getManager();
$editCandidate = $this->createForm(new \AppBundle\Form\CandidateType($em), $candidate, array(
'em' => $this->getDoctrine()->getManager()
));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment