Last active
October 20, 2015 11:06
-
-
Save anonymous/8b2e576e69b1588e4f21 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Test\advFormBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
/** | |
* ProductSub | |
* | |
* @ORM\Table() | |
* @ORM\Entity | |
*/ | |
class ProductSub | |
{ | |
/** | |
* @var integer | |
* | |
* @ORM\Column(name="id", type="integer") | |
* @ORM\Id | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
private $id; | |
/** | |
* @ORM\ManyToOne(targetEntity="ProductGrp", inversedBy = "ProdcutSub") | |
* | |
*/ | |
private $productgrp; | |
/** | |
* @var string | |
* | |
* @ORM\Column(name="productsub", type="string", length=64) | |
*/ | |
private $productsub; | |
/** | |
* Get id | |
* | |
* @return integer | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* Get productsub | |
* | |
* @return string | |
*/ | |
public function getProductsub() | |
{ | |
return $this->productsub; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getProductgrp() | |
{ | |
return $this->productgrp; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Test\advFormBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
/** | |
* Product | |
* | |
* @ORM\Table() | |
* @ORM\Entity(repositoryClass="Test\advFormBundle\Entity\ProductRepository") | |
*/ | |
class Product | |
{ | |
/** | |
* @var integer | |
* | |
* @ORM\Column(name="id", type="integer") | |
* @ORM\Id | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
private $id; | |
/** | |
* @ORM\ManyToOne(targetEntity="productsub", inversedBy= "Product") | |
*/ | |
private $productsub; | |
/** | |
* @var string | |
* | |
* @ORM\Column(name="product", type="string", length=64) | |
*/ | |
private $product; | |
/** | |
* Get id | |
* | |
* @return integer | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* Get product | |
* | |
* @return string | |
*/ | |
public function getProduct() | |
{ | |
return $this->product; | |
} | |
/** | |
* @return mixed | |
*/ | |
public function getProductsub() | |
{ | |
return $this->productsub; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Test\advFormBundle\Entity; | |
use Doctrine\ORM\Mapping as ORM; | |
/** | |
* ProductGrp | |
* | |
* @ORM\Table() | |
* @ORM\Entity | |
*/ | |
class ProductGrp | |
{ | |
/** | |
* @var integer | |
* | |
* @ORM\Column(name="id", type="integer") | |
* @ORM\Id | |
* @ORM\GeneratedValue(strategy="AUTO") | |
*/ | |
private $id; | |
/** | |
* @var string | |
* | |
* @ORM\Column(name="productgrp", type="string", length=64, unique = true) | |
*/ | |
private $productgrp; | |
/** | |
* Get id | |
* | |
* @return integer | |
*/ | |
public function getId() | |
{ | |
return $this->id; | |
} | |
/** | |
* Set productgrp | |
* | |
* @param string $productgrp | |
* | |
* @return ProductGrp | |
*/ | |
public function setProductgrp($productgrp) | |
{ | |
$this->productgrp = $productgrp; | |
return $this; | |
} | |
/** | |
* Get productgrp | |
* | |
* @return string | |
*/ | |
public function getProductgrp() | |
{ | |
return $this->productgrp; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Test\advFormBundle\Form; | |
use Doctrine\ORM\EntityManager; | |
use Doctrine\ORM\EntityRepository; | |
use Symfony\Component\Form\AbstractType; | |
use Symfony\Component\Form\FormBuilderInterface; | |
use Symfony\Component\Form\FormEvent; | |
use Symfony\Component\Form\FormEvents; | |
use Symfony\Component\Form\FormInterface; | |
use Test\advFormBundle\Entity\ProductSub; | |
use Test\advFormBundle\Entity\Product; | |
use Symfony\Component\OptionsResolver\OptionsResolver; | |
class productype extends AbstractType{ | |
protected $em; | |
public function __construct(EntityManager $em){ | |
$this->em = $em; | |
} | |
public function buildForm(FormBuilderInterface $builder, array $options){ | |
$builder->add('productgrp','entity',array( | |
'class'=>'TestadvForm:ProductGrp', | |
'choice_label'=>'productgrp', | |
'query_builder'=>function(EntityRepository $er){ | |
return $er->createQueryBuilder('pgrp') | |
->orderBy('pgrp.productgrp','ASC'); | |
} | |
)); | |
$builder->add('search','submit'); | |
$builder->addEventListener(FormEvents::PRE_SET_DATA, array($this,'onPreSetData')); | |
$builder->addEventListener(FormEvents::PRE_SUBMIT, array($this, 'onPreSubmit')); | |
} | |
protected function addElements(FormInterface $form, ProductSub $productsub){ | |
$search = $form->get('search'); | |
$form->remove('search'); | |
$form->add('productsub','entity',array( | |
'data'=> $productsub, | |
'empty_value'=>'--Choose product sub category--', | |
'class'=>'TestadvForm:ProductSub', | |
'mapped'=> false | |
)); | |
} | |
public function onPreSubmit(FormEvent $event){ | |
$form = $event->getForm(); | |
$data_productsub = $event->getData(); | |
$productsub = $this->em->getRepository('TestadvFormBundle:ProductSub')->find($data_productsub['productsub']); | |
$this->addElements($form, $productsub); | |
} | |
public function onPreSetData(FormEvent $event){ | |
$product = $event->getData(); | |
$form = $event->getForm(); | |
$productsub = $product->getProductsub() ? $product->getProductsub()->getProductsub() : null; | |
$this->addElements($form,$productsub); | |
} | |
public function configureOptions(OptionsResolver $resolver) { | |
$resolver->setDefaults(array( | |
'data_class' => 'Test\advFormBundle\Entity\Product' | |
)); | |
} | |
public function getName() | |
{ | |
return "product_type"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment