Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [Symfony2][SONATA] Problem z formularzem Collection
luq92
post 26.09.2019, 21:43:20
Post #1





Grupa: Zarejestrowani
Postów: 3
Pomógł: 0
Dołączył: 2.07.2017

Ostrzeżenie: (0%)
-----


Cześć,

Potrzebuję na szybko postawić panel do zarządzania. Z racji, że znam SF2 postawiłem na Sonatę, jednakże mam problem ze stworzeniem jednego formularza.
Mamy 2 encje, Product i ProductPrice. Z tego co sprawdziłem, w ProductPriceAdmin w metodzie prePersist dostaję obiekt ProductPrice, a gdy spojrzymy sobie na parenta ($this->getParent()) dostajemy nulla.
Przy obecnym stanie kodu w momemncie zapisu dostaniemy: NotNullConstraintViolationException: An exception occurred while executing 'INSERT INTO product_price (price_netto, currency_id, distribution_channel_id, product_id) VALUES (?, ?, ?, ?)' with params [1323, 1, 1, null]:
Jak badam children w ProductAdmin to productPrice się tam znajduje.

  1. ProductBundle\Entity\Product:
  2. type: entity
  3. table: product
  4. repositoryClass: ProductBundle\Repository\ProductRepository
  5. id:
  6. id:
  7. type: integer
  8. nullable: false
  9. options:
  10. unsigned: false
  11. id: true
  12. generator:
  13. strategy: IDENTITY
  14. fields:
  15. name:
  16. type: string
  17. nullable: false
  18. length: 255
  19. options:
  20. fixed: false
  21.  
  22. oneToMany:
  23. productPrices:
  24. targetEntity: ProductBundle\Entity\ProductPrice
  25. mappedBy: product
  26. orphanRemoval: true
  27. cascade: ["persist", "remove"]
  28. joinColumn:
  29. name: id
  30. referencedColumnName: product_id
  31. onDelete: CASCADE
  32.  
  33. lifecycleCallbacks: { }


  1. ProductBundle\Entity\ProductPrice:
  2. type: entity
  3. table: product_price
  4. id:
  5. id:
  6. type: integer
  7. nullable: false
  8. options:
  9. unsigned: false
  10. id: true
  11. generator:
  12. strategy: IDENTITY
  13. fields:
  14. priceNetto:
  15. type: integer
  16. nullable: false
  17. options:
  18. unsigned: false
  19. column: price_netto
  20. manyToOne:
  21. product:
  22. targetEntity: ProductBundle\Entity\Product
  23. cascade: { }
  24. fetch: LAZY
  25. mappedBy: null
  26. inversedBy: null
  27. joinColumns:
  28. product_id:
  29. referencedColumnName: id
  30. orphanRemoval: false
  31. lifecycleCallbacks: { }

  1. <?php
  2.  
  3. namespace Application\Sonata\AdminBundle\Admin;
  4.  
  5. use CoreBundle\Money\Formatter\AmountFormatter;
  6. use Doctrine\ORM\Query\Expr\Join;
  7. use ProductBundle\Entity\ProductPrice;
  8. use Sonata\AdminBundle\Admin\AbstractAdmin;
  9. use Sonata\AdminBundle\Datagrid\DatagridMapper;
  10. use Sonata\AdminBundle\Datagrid\ListMapper;
  11. use Sonata\AdminBundle\Form\FormMapper;
  12. use Sonata\AdminBundle\Form\Type\AdminType;
  13. use Sonata\AdminBundle\Form\Type\Filter\ChoiceType;
  14. use Sonata\AdminBundle\Form\Type\Filter\NumberType;
  15. use Sonata\AdminBundle\Form\Type\ModelType;
  16. use Sonata\AdminBundle\Show\ShowMapper;
  17. use Sonata\CoreBundle\Form\Type\CollectionType;
  18. use Sonata\DoctrineORMAdminBundle\Datagrid\ProxyQuery;
  19. use SupplierBundle\Enum\SupplierEnum;
  20. use Symfony\Component\Form\Extension\Core\Type\TextType;
  21.  
  22. class ProductAdmin extends AbstractAdmin
  23. {
  24. public $supportsPreviewMode = true;
  25.  
  26. protected $translationDomain = 'ProductBundle';
  27.  
  28. private $operatorChoices = [
  29. 'choices' => [
  30. NumberType::TYPE_EQUAL => '=',
  31. NumberType::TYPE_GREATER_EQUAL => '>=',
  32. NumberType::TYPE_GREATER_THAN => '>',
  33. NumberType::TYPE_LESS_EQUAL => '<=',
  34. NumberType::TYPE_LESS_THAN => '<',
  35. ],
  36. ];
  37.  
  38. protected function configureFormFields(FormMapper $formMapper): void
  39. {
  40. $formMapper
  41. ->tab($this->trans('tab.basic_information'))
  42. ->add('name', TextType::class)->end()
  43. ->end()
  44. ->tab($this->trans('tab.prices'))
  45. ->add('productPrices', CollectionType::class,
  46. [
  47. 'required' => false,
  48. 'label' => 'abc',
  49. ]
  50. )->end()
  51. ->end();
  52.  
  53. }
  54. }


  1. <?php
  2.  
  3. namespace Application\Sonata\AdminBundle\Admin;
  4.  
  5. use CoreBundle\Entity\Currency;
  6. use CoreBundle\Entity\DistributionChannel;
  7. use ProductBundle\Entity\Product;
  8. use Sonata\AdminBundle\Admin\AbstractAdmin;
  9. use Sonata\AdminBundle\Form\FormMapper;
  10. use Sonata\AdminBundle\Form\Type\ModelHiddenType;
  11. use Symfony\Bridge\Doctrine\Form\Type\EntityType;
  12. use Symfony\Component\Form\Extension\Core\Type\NumberType;
  13.  
  14. class ProductPriceAdmin extends AbstractAdmin
  15. {
  16. protected $parentAssociationMapping = 'product';
  17.  
  18. protected function configureFormFields(FormMapper $formMapper)
  19. {
  20. $formMapper->add('product', ModelHiddenType::class);
  21. $formMapper
  22. ->add('priceNetto', NumberType::class, [
  23. 'required' => true,
  24. ])
  25. ->add('distributionChannel', EntityType::class, [
  26. 'required' => true,
  27. 'class' => DistributionChannel::class,
  28. 'property' => 'name'
  29. ])
  30. ->add('currency', EntityType::class, [
  31. 'required' => true,
  32. 'class' => Currency::class,
  33. 'property' => 'name'
  34. ])
  35. ;
  36. }
  37.  
  38.  
  39. public function prePersist($object)
  40. {
  41. dump($object);
  42. }
  43. public function preUpdate($object)
  44. {
  45. dump($object);
  46. }
  47. }

  1. Application\Sonata\AdminBundle\Admin\ProductPriceAdmin:
  2. class: Application\Sonata\AdminBundle\Admin\ProductPriceAdmin
  3. arguments: [~, ProductBundle\Entity\ProductPrice, ~]
  4. tags:
  5. - { name: sonata.admin, manager_type: orm, label: Product }
  6. public: true
  7.  
  8. Application\Sonata\AdminBundle\Admin\ProductAdmin:
  9. class: Application\Sonata\AdminBundle\Admin\ProductAdmin
  10. arguments: [~, ProductBundle\Entity\Product, ~]
  11. tags:
  12. - { name: sonata.admin, manager_type: orm, label: Product }
  13. calls:
  14. - method: addChild
  15. arguments: ['@Application\Sonata\AdminBundle\Admin\ProductPriceAdmin']
  16. public: true


Czy ma ktoś jakiś pomysł jak to rozwiązać? Jak zrobić aby encja Product była dostępna w ProductPriceAdmin?

Z góry dzięki za pomoc
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 29.03.2024 - 00:10