Estimados,
estoy hace un tiempo con Silex, y bueno ahora he agregado Doctrine ORM por medio de dflydev
https://github.com/dflydev/dflydev-d...rvice-provider
todo funciona bien, pero lo que no he podido hacer funcionar es que el FormBuilder detecte las relaciones (Many To One, One To Many, etc) y las muestre en el formulario.
Lo que necesito es que el formulario me muestre el listado de empresas al momento de agregar un contacto.
Lo que tengo en el controllers.php
Código PHP:
Ver original$app->match('/add-contact', function(Request $request) use ($app) {
$form = $app['form.factory']->create(new ContactType());
if('POST' == $request->getMethod()) {
$form->bind($request);
if($form->isValid()) {
$data = $form->getData();
$app['db']->insert('contacts', $data);
$app['session']->getFlashBag()->add('success', 'Haz añadido un contacto. Amalo! :)');
return $app->redirect($app['url_generator']->generate('add-contact'));
}
}
return $app['twig']->render('backend/add-contact.html.twig', array('form' => $form->createView())); })
->bind('add-contact')
->method('GET|POST')
;
y en ContactType tengo lo siguiente:
Código PHP:
Ver original<?php
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Validator\Constraints
as Assert;
class ContactType extends AbstractType
{
public function buildForm
(FormBuilderInterface
$builder, array $options) {
$builder
->add('name')
->add('phone')
->add('email')
->add('company')
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array( 'data_class' => 'Entity\Contact'
));
}
public function getName()
{
return 'contact';
}
}
y el la Entidad Contact, tengo lo siguiente:
Código PHP:
Ver original<?php
namespace Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* Contact
*
* @ORM\Table(name="contacts")
* @ORM\Entity
*/
class Contact
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer", precision=0, scale=0, nullable=false, unique=false)
* @ORM\Id
* @ORM\GeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=45, precision=0, scale=0, nullable=false, unique=false)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="phone", type="string", length=20, precision=0, scale=0, nullable=false, unique=false)
*/
private $phone;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=150, precision=0, scale=0, nullable=false, unique=false)
*/
private $email;
/**
* @var \Doctrine\Common\Collections\Collection
*
* @ORM\OneToMany(targetEntity="Entity\Job", mappedBy="contact")
*/
private $jobs;
/**
* @var \Entity\Company
*
* @ORM\ManyToOne(targetEntity="Entity\Company", inversedBy="contact", cascade={"remove"})
* @ORM\JoinColumns({
* @ORM\JoinColumn(name="company_id", referencedColumnName="id", nullable=true)
* })
*/
private $company;
/**
* Constructor
*/
public function __construct()
{
$this->jobs = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Contact
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set phone
*
* @param string $phone
* @return Contact
*/
public function setPhone($phone)
{
$this->phone = $phone;
return $this;
}
/**
* Get phone
*
* @return string
*/
public function getPhone()
{
return $this->phone;
}
/**
* Set email
*
* @param string $email
* @return Contact
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Add jobs
*
* @param \Entity\Job $jobs
* @return Contact
*/
public function addJob(\Entity\Job $jobs)
{
$this->jobs[] = $jobs;
return $this;
}
/**
* Remove jobs
*
* @param \Entity\Job $jobs
*/
public function removeJob(\Entity\Job $jobs)
{
$this->jobs->removeElement($jobs);
}
/**
* Get jobs
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getJobs()
{
return $this->jobs;
}
/**
* Set company
*
* @param \Entity\Company $company
* @return Contact
*/
public function setCompany(\Entity\Company $company = null)
{
$this->company = $company;
return $this;
}
/**
* Get company
*
* @return \Entity\Company
*/
public function getCompany()
{
return $this->company;
}
public function __toString()
{
return $this->name;
}
}
Con todo este codigo no obtengo un Select en la vista... como deberia pasar.
espero que me puedan ayudar.
Gracias :)