This topic contains 5 replies, has 2 voices, and was last updated by Alexandr Smaga 10 years, 2 months ago.
- Topic
Hello, I’m newbie in Symfony and also new with Oro. From what I see, this system is great but still too complex for me to comprehend all. I need to add a little bit customization.
I want to add Order-Product like subsystem (I use sample code).I have problem with entity relation. When using ManyToMany relation, it seems all fine
MANY-TO-MANY CODE (this is fine, no problem, just comparison)
1234567891011121314//MyBundle\Entity\Order.php => using ManyToManyclass Order {/*** Products storage** @var ArrayCollection $products** @ORM\ManyToMany(targetEntity="MyBundle\Entity\Product", inversedBy="products")* @ORM\JoinTable(name="mybundle_order_to_product")*/protected $products;}MANY-TO-MANY CODE (this is fine, no problem, just comparison)
12345678910111213141516171819202122//MyBundle\Form\Type\OrderType.php => using ManyToManypublic function buildForm(FormBuilderInterface $builder, array $options) {$builder->add('default_product','oro_entity_identifier',array('class' => 'MyBundle:Product','multiple' => false));$builder->add('products','oro_multiple_entity',array('class' => 'MyBundle:Product','required' => false,'default_element' => 'default_product',)); // will add/remove product using grid widget}Then I have a need to use ManyToOne-OneToMany relation instead of using ManyToMany relation. Here is my code (ideally like this, but practically I got error):
1234567891011121314151617181920212223242526272829303132//MyBundle\Entity\Order.phpclass Order {/*** @ORM\OneToMany(targetEntity="MyBundle\Entity\Orderproduct", mappedBy="order")*/protected $orderproducts;}//MyBundle\Entity\Product.phpclass Product {/*** @ORM\OneToMany(targetEntity="MyBundle\Entity\Orderproduct", mappedBy="product")**/protected $orderproducts;}//MyBundle\Entity\Orderproduct.phpclass Orderproduct {/*** @ORM\ManyToOne(targetEntity="MyBundle\Entity\Order", inversedBy="orderproducts")* @ORM\JoinColumn(name="order_id", referencedColumnName="id")*/protected $order;/*** @ORM\ManyToOne(targetEntity="MyBundle\Entity\Product", inversedBy="orderproducts")* @ORM\JoinColumn(name="product_id", referencedColumnName="id")*/protected $product;}12345678910111213141516171819202122//MyBundle\Form\Type\OrderType.phppublic function buildForm(FormBuilderInterface $builder, array $options) {$builder->add('default_orderproduct','oro_entity_identifier',array('class' => 'MyBundle:Orderproduct','multiple' => false));$builder->add('orderproducts','oro_multiple_entity',array('class' => 'MyBundle:Orderproduct','required' => false,'default_element' => 'default_orderproduct',)); // will add/remove product using grid widget}From above code, I can’t get orderproducts-added or orderproducts-removed because added orderproduct not found from db (of course because this is actually collection of product id)
123456789//MyBundle\Form\Handler\OrderHandler.phpclass OrderHandler {protected function handleOrderproducts($entity){$appendOrder_products = $this->form->get('orderproducts')->get('added')->getData(); // will result null}}Then after I try changed a bit, the class changed to be Product, but held by default_orderproduct & orderproducts builder, would result problem too
12345678910111213141516171819202122//MyBundle\Form\Type\OrderType.phppublic function buildForm(FormBuilderInterface $builder, array $options) {$builder->add('default_orderproduct','oro_entity_identifier',array('class' => 'MyBundle:Product','multiple' => false));$builder->add('orderproducts','oro_multiple_entity',array('class' => 'MyBundle:Product','required' => false,'default_element' => 'default_orderproduct',));}Of course would cause problem, my manipulation just for testing, just trying to find a way. When get data from db to be put into form data, would result error incompatible class Product with Orderproduct.
My Question, Can OroCrm using ManyToOne-OneToMany relation as alternative to ManyToMany relation?
Thanks
The forum ‘OroPlatform – Programming Questions’ is closed to new topics and replies.