简体   繁体   中英

Symfony 2 embed form for doctrine one to many relation using sonata admin generator

I am trying to implemet CRUD using sonata admin generator.

I have two table and Vendor and vendor contacts. My entity tables are like this

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
/**
* @ORM\Entity
* @ORM\Table(name="vendor")
*/
class Vendor{
   /**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;
/**
 * @ORM\OneToMany(targetEntity="VendorContact", mappedBy="vendor_contact")
 */
public $contact;
/**
 * @ORM\Column(type="string", length=100)
 */
protected $name;
/**
 * @ORM\Column(type="string", length=100)
 */
protected $userName;
/**
 * @ORM\Column(type="string", length=100)
 */
protected $password;
/**
 * @ORM\Column(type="integer")
 */
private $status;
/**
 * @ORM\Column(type="date")
 */
protected $contractBeginDate;

/**
 * @ORM\Column(type="date")
 */
protected $contractEndDate;
/**
 * @ORM\Column(type="datetime", nullable="true")
 */
protected $createdAt;

/**
 * @ORM\Column(type="datetime", nullable="true")
 */
protected $updatedAt;

public function __construct()
{
     $this->contact = new ArrayCollection();
}

And my vendor contact entity class is like this

       use Doctrine\ORM\Mapping as ORM;
       use Gedmo\Mapping\Annotation as Gedmo;

/**
* @ORM\Entity
* @ORM\Table(name="vendor_contact")
*/
class VendorContact{

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;
/**
 * @ORM\ManyToOne(targetEntity="Vendor", inversedBy="contact")
 * @ORM\JoinColumn(name ="vendor_id", referencedColumnName="id")
 */
protected $vendorContact;

/**
 * @ORM\Column(type="string", length=1000)
 */
protected $street;
/**
 * @ORM\Column(type="string", length=100)
 */
protected $city;
/**
 * @ORM\Column(type="string", length=100)
 */
protected $state;
/**
 * @ORM\Column(type="string", length=100)
 */
protected $country;
/**
 * @ORM\Column(type="string", length=50)
 */
protected $zip;
/**
 * @ORM\Column(type="string", length=50)
 */
protected $contact_numb;
/**
 * @ORM\Column(type="string", length=100)
 */
protected $email;
/**
 * @ORM\Column(type="integer")
 */
protected $contact_type;
/**
 * @ORM\Column(type="datetime", nullable="true")
 */
protected $createdAt;

/**
 * @ORM\Column(type="datetime", nullable="true")
 */
protected $updatedAt;

My sonata Admin class is like:

class VendorAdmin extends Admin
{

protected function configureFormFields(FormMapper $formMapper)
{
    $formMapper
    ->with('General')
    ->add('name')
    ->add('user_name','text')
    ->add('password','text')
    ->add('status')
    ->add('contract_begin_date','date')
    ->add('contract_end_date','date');
    $formMapper->add('contact', 'collection', array('type' => new VendorContactType()));
//  ->end();
    ;
}

protected function configureListFields(ListMapper $listMapper)
{
    $listMapper
    ->addIdentifier('id')
    ->add('name')
    ->add('userName')
    ->add('contractBeginDate')
    ->add('contractEndDate')
    ->add('_action', array(), array(
            'actions' => array(
                'edit' => array(),
    ),
    ))
    ;
}

}

And vendor contact form builder is like this:

  class VendorContactType extends AbstractType
  {
      public function buildForm(FormBuilder $builder, array $options)
      {
    $builder
    ->add('street')
    ->add('city')
    ->add('state')
    ->add('country')
    ->add('zip')
    ->add('contact_numb')
    ->add('email')
    ->add('contact_type')
    ;
}

public function getName()
{
    return 'vendor_contact';
}

public function getDefaultOptions(array $options){
    return array('data_class' => 'JiniVod\StoreBundle\Entity\VendorContact');
}
}

But when i am executing this add vendor using to sonata admin crud function i am not getting form field for vendor contact. I am onlty getting label contact

Can any one please help me.

Thanks in advance.

而不是使用“收集”类型,您应该“使用sonata_type_model”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM