How to override Product model in Sylius?









up vote
1
down vote

favorite












I am working on Sylius 1.2.8. I have overridden Product model and it is working fine but when I add a new product with attributes:



<?php
...

$em = $this->container->get('sylius.manager.product');

/**
* @var SyliusComponentProductModelProductAttributeValueInterface $attribute
* @var SyliusComponentCoreModelProductInterface $product
*/

$product->addAttribute($attribute);
$em->persist($product);
$em->flush();


It throws this error:




An exception occurred while executing 'INSERT INTO
sylius_product_attribute_value (locale_code, text_value,
boolean_value, integer_value, float_value, datetime_value, date_value,
json_value, product_id, attribute_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?,
?, ?)' with params ["en_US", null, null, null, null, null, null,
"["013ea12a-1aff-4050-8107-20b53ada73ce"]", null, 28]:




SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'product_id' cannot be null


My custom Product Model looks like this:



namespace AppBundleEntity;

use DoctrineCommonCollectionsArrayCollection;
use SyliusComponentCoreModelProduct as BaseProduct;

class Product extends BaseProduct implements ProductInterface

public function __construct()

parent::__construct();




And my config file:



sylius_product:
driver: doctrine/orm
resources:
product:
classes:
repository: AppBundleDoctrineORMProductRepository
model: AppBundleEntityProduct


I have found a similar question asked :stackoverflow.com/q/22919004/6248367. But this does not solve my problem neither does it answer why application fails. This answer is also 4 years old. Can someone help me fix this?



Edit: This error is also thrown in admin whenever I create a new product and add attributes to it at the same time. Workaround is to first create product, then add attributes.










share|improve this question























  • I have tried clean install of your Sylius version and add exact same config and its working fine. Is there any other thing that you have override? Try everything from scratch - resource.yml, Product entity that extends BaseProduct, Product repository that extends BaseProductRepository and proper Product.orm.yml configuration.
    – czende
    Nov 14 at 11:14










  • it seems like you need to set the product before adding an attribute because the product_id is not set yet. so persist the product before persisting the attributes
    – Puya Sarmidani
    Nov 15 at 15:43










  • @PuyaSarmidani You're right but doctrine should automatically manage that internally. According to you If it was intended behaviour then how did admin's product creation break?
    – Aakash Tushar
    Nov 16 at 7:20










  • @czende I think you're right but I haven't got enough time to check all the configurations yet. I'll respond soon.
    – Aakash Tushar
    Nov 16 at 7:23














up vote
1
down vote

favorite












I am working on Sylius 1.2.8. I have overridden Product model and it is working fine but when I add a new product with attributes:



<?php
...

$em = $this->container->get('sylius.manager.product');

/**
* @var SyliusComponentProductModelProductAttributeValueInterface $attribute
* @var SyliusComponentCoreModelProductInterface $product
*/

$product->addAttribute($attribute);
$em->persist($product);
$em->flush();


It throws this error:




An exception occurred while executing 'INSERT INTO
sylius_product_attribute_value (locale_code, text_value,
boolean_value, integer_value, float_value, datetime_value, date_value,
json_value, product_id, attribute_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?,
?, ?)' with params ["en_US", null, null, null, null, null, null,
"["013ea12a-1aff-4050-8107-20b53ada73ce"]", null, 28]:




SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'product_id' cannot be null


My custom Product Model looks like this:



namespace AppBundleEntity;

use DoctrineCommonCollectionsArrayCollection;
use SyliusComponentCoreModelProduct as BaseProduct;

class Product extends BaseProduct implements ProductInterface

public function __construct()

parent::__construct();




And my config file:



sylius_product:
driver: doctrine/orm
resources:
product:
classes:
repository: AppBundleDoctrineORMProductRepository
model: AppBundleEntityProduct


I have found a similar question asked :stackoverflow.com/q/22919004/6248367. But this does not solve my problem neither does it answer why application fails. This answer is also 4 years old. Can someone help me fix this?



Edit: This error is also thrown in admin whenever I create a new product and add attributes to it at the same time. Workaround is to first create product, then add attributes.










share|improve this question























  • I have tried clean install of your Sylius version and add exact same config and its working fine. Is there any other thing that you have override? Try everything from scratch - resource.yml, Product entity that extends BaseProduct, Product repository that extends BaseProductRepository and proper Product.orm.yml configuration.
    – czende
    Nov 14 at 11:14










  • it seems like you need to set the product before adding an attribute because the product_id is not set yet. so persist the product before persisting the attributes
    – Puya Sarmidani
    Nov 15 at 15:43










  • @PuyaSarmidani You're right but doctrine should automatically manage that internally. According to you If it was intended behaviour then how did admin's product creation break?
    – Aakash Tushar
    Nov 16 at 7:20










  • @czende I think you're right but I haven't got enough time to check all the configurations yet. I'll respond soon.
    – Aakash Tushar
    Nov 16 at 7:23












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I am working on Sylius 1.2.8. I have overridden Product model and it is working fine but when I add a new product with attributes:



<?php
...

$em = $this->container->get('sylius.manager.product');

/**
* @var SyliusComponentProductModelProductAttributeValueInterface $attribute
* @var SyliusComponentCoreModelProductInterface $product
*/

$product->addAttribute($attribute);
$em->persist($product);
$em->flush();


It throws this error:




An exception occurred while executing 'INSERT INTO
sylius_product_attribute_value (locale_code, text_value,
boolean_value, integer_value, float_value, datetime_value, date_value,
json_value, product_id, attribute_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?,
?, ?)' with params ["en_US", null, null, null, null, null, null,
"["013ea12a-1aff-4050-8107-20b53ada73ce"]", null, 28]:




SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'product_id' cannot be null


My custom Product Model looks like this:



namespace AppBundleEntity;

use DoctrineCommonCollectionsArrayCollection;
use SyliusComponentCoreModelProduct as BaseProduct;

class Product extends BaseProduct implements ProductInterface

public function __construct()

parent::__construct();




And my config file:



sylius_product:
driver: doctrine/orm
resources:
product:
classes:
repository: AppBundleDoctrineORMProductRepository
model: AppBundleEntityProduct


I have found a similar question asked :stackoverflow.com/q/22919004/6248367. But this does not solve my problem neither does it answer why application fails. This answer is also 4 years old. Can someone help me fix this?



Edit: This error is also thrown in admin whenever I create a new product and add attributes to it at the same time. Workaround is to first create product, then add attributes.










share|improve this question















I am working on Sylius 1.2.8. I have overridden Product model and it is working fine but when I add a new product with attributes:



<?php
...

$em = $this->container->get('sylius.manager.product');

/**
* @var SyliusComponentProductModelProductAttributeValueInterface $attribute
* @var SyliusComponentCoreModelProductInterface $product
*/

$product->addAttribute($attribute);
$em->persist($product);
$em->flush();


It throws this error:




An exception occurred while executing 'INSERT INTO
sylius_product_attribute_value (locale_code, text_value,
boolean_value, integer_value, float_value, datetime_value, date_value,
json_value, product_id, attribute_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?,
?, ?)' with params ["en_US", null, null, null, null, null, null,
"["013ea12a-1aff-4050-8107-20b53ada73ce"]", null, 28]:




SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'product_id' cannot be null


My custom Product Model looks like this:



namespace AppBundleEntity;

use DoctrineCommonCollectionsArrayCollection;
use SyliusComponentCoreModelProduct as BaseProduct;

class Product extends BaseProduct implements ProductInterface

public function __construct()

parent::__construct();




And my config file:



sylius_product:
driver: doctrine/orm
resources:
product:
classes:
repository: AppBundleDoctrineORMProductRepository
model: AppBundleEntityProduct


I have found a similar question asked :stackoverflow.com/q/22919004/6248367. But this does not solve my problem neither does it answer why application fails. This answer is also 4 years old. Can someone help me fix this?



Edit: This error is also thrown in admin whenever I create a new product and add attributes to it at the same time. Workaround is to first create product, then add attributes.







symfony sylius sylius-1.2






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 19:27









common sense

2,14811525




2,14811525










asked Nov 10 at 12:03









Aakash Tushar

9412




9412











  • I have tried clean install of your Sylius version and add exact same config and its working fine. Is there any other thing that you have override? Try everything from scratch - resource.yml, Product entity that extends BaseProduct, Product repository that extends BaseProductRepository and proper Product.orm.yml configuration.
    – czende
    Nov 14 at 11:14










  • it seems like you need to set the product before adding an attribute because the product_id is not set yet. so persist the product before persisting the attributes
    – Puya Sarmidani
    Nov 15 at 15:43










  • @PuyaSarmidani You're right but doctrine should automatically manage that internally. According to you If it was intended behaviour then how did admin's product creation break?
    – Aakash Tushar
    Nov 16 at 7:20










  • @czende I think you're right but I haven't got enough time to check all the configurations yet. I'll respond soon.
    – Aakash Tushar
    Nov 16 at 7:23
















  • I have tried clean install of your Sylius version and add exact same config and its working fine. Is there any other thing that you have override? Try everything from scratch - resource.yml, Product entity that extends BaseProduct, Product repository that extends BaseProductRepository and proper Product.orm.yml configuration.
    – czende
    Nov 14 at 11:14










  • it seems like you need to set the product before adding an attribute because the product_id is not set yet. so persist the product before persisting the attributes
    – Puya Sarmidani
    Nov 15 at 15:43










  • @PuyaSarmidani You're right but doctrine should automatically manage that internally. According to you If it was intended behaviour then how did admin's product creation break?
    – Aakash Tushar
    Nov 16 at 7:20










  • @czende I think you're right but I haven't got enough time to check all the configurations yet. I'll respond soon.
    – Aakash Tushar
    Nov 16 at 7:23















I have tried clean install of your Sylius version and add exact same config and its working fine. Is there any other thing that you have override? Try everything from scratch - resource.yml, Product entity that extends BaseProduct, Product repository that extends BaseProductRepository and proper Product.orm.yml configuration.
– czende
Nov 14 at 11:14




I have tried clean install of your Sylius version and add exact same config and its working fine. Is there any other thing that you have override? Try everything from scratch - resource.yml, Product entity that extends BaseProduct, Product repository that extends BaseProductRepository and proper Product.orm.yml configuration.
– czende
Nov 14 at 11:14












it seems like you need to set the product before adding an attribute because the product_id is not set yet. so persist the product before persisting the attributes
– Puya Sarmidani
Nov 15 at 15:43




it seems like you need to set the product before adding an attribute because the product_id is not set yet. so persist the product before persisting the attributes
– Puya Sarmidani
Nov 15 at 15:43












@PuyaSarmidani You're right but doctrine should automatically manage that internally. According to you If it was intended behaviour then how did admin's product creation break?
– Aakash Tushar
Nov 16 at 7:20




@PuyaSarmidani You're right but doctrine should automatically manage that internally. According to you If it was intended behaviour then how did admin's product creation break?
– Aakash Tushar
Nov 16 at 7:20












@czende I think you're right but I haven't got enough time to check all the configurations yet. I'll respond soon.
– Aakash Tushar
Nov 16 at 7:23




@czende I think you're right but I haven't got enough time to check all the configurations yet. I'll respond soon.
– Aakash Tushar
Nov 16 at 7:23












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










I have figured out the problem. As @czende mentioned properly check the configuration.



In my case I had set the wrong class in subject while overriding the attribute:



sylius_product:
driver: doctrine/orm
resources:
product:
classes:
repository: AppBundleDoctrineORMProductRepository
model: AppBundleEntityProduct

sylius_attribute:
driver: doctrine/orm
resources:
product:
# Make sure to provide the correct Product class in the subject.
subject: AppBundleEntityProduct
attribute:
classes:
model: AppBundleEntityProductAttribute
repository: AppBundleDoctrineORMProductAttributeRepository


Also make sure to use mappedSuperclass in the mapping config instead of entity as commented by Michał Marcinkowski in issue #3997



AppBundleEntityProductAttribute:
type: mappedSuperclass
table: sylius_product_attribute


EDIT: To debug what's wrong, start by checking warnings in symfony debug bar. It can narrow down your search area for bug. I can also hint you if something is wrong in your configuration file.






share|improve this answer






















    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238749%2fhow-to-override-product-model-in-sylius%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    I have figured out the problem. As @czende mentioned properly check the configuration.



    In my case I had set the wrong class in subject while overriding the attribute:



    sylius_product:
    driver: doctrine/orm
    resources:
    product:
    classes:
    repository: AppBundleDoctrineORMProductRepository
    model: AppBundleEntityProduct

    sylius_attribute:
    driver: doctrine/orm
    resources:
    product:
    # Make sure to provide the correct Product class in the subject.
    subject: AppBundleEntityProduct
    attribute:
    classes:
    model: AppBundleEntityProductAttribute
    repository: AppBundleDoctrineORMProductAttributeRepository


    Also make sure to use mappedSuperclass in the mapping config instead of entity as commented by Michał Marcinkowski in issue #3997



    AppBundleEntityProductAttribute:
    type: mappedSuperclass
    table: sylius_product_attribute


    EDIT: To debug what's wrong, start by checking warnings in symfony debug bar. It can narrow down your search area for bug. I can also hint you if something is wrong in your configuration file.






    share|improve this answer


























      up vote
      0
      down vote



      accepted










      I have figured out the problem. As @czende mentioned properly check the configuration.



      In my case I had set the wrong class in subject while overriding the attribute:



      sylius_product:
      driver: doctrine/orm
      resources:
      product:
      classes:
      repository: AppBundleDoctrineORMProductRepository
      model: AppBundleEntityProduct

      sylius_attribute:
      driver: doctrine/orm
      resources:
      product:
      # Make sure to provide the correct Product class in the subject.
      subject: AppBundleEntityProduct
      attribute:
      classes:
      model: AppBundleEntityProductAttribute
      repository: AppBundleDoctrineORMProductAttributeRepository


      Also make sure to use mappedSuperclass in the mapping config instead of entity as commented by Michał Marcinkowski in issue #3997



      AppBundleEntityProductAttribute:
      type: mappedSuperclass
      table: sylius_product_attribute


      EDIT: To debug what's wrong, start by checking warnings in symfony debug bar. It can narrow down your search area for bug. I can also hint you if something is wrong in your configuration file.






      share|improve this answer
























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        I have figured out the problem. As @czende mentioned properly check the configuration.



        In my case I had set the wrong class in subject while overriding the attribute:



        sylius_product:
        driver: doctrine/orm
        resources:
        product:
        classes:
        repository: AppBundleDoctrineORMProductRepository
        model: AppBundleEntityProduct

        sylius_attribute:
        driver: doctrine/orm
        resources:
        product:
        # Make sure to provide the correct Product class in the subject.
        subject: AppBundleEntityProduct
        attribute:
        classes:
        model: AppBundleEntityProductAttribute
        repository: AppBundleDoctrineORMProductAttributeRepository


        Also make sure to use mappedSuperclass in the mapping config instead of entity as commented by Michał Marcinkowski in issue #3997



        AppBundleEntityProductAttribute:
        type: mappedSuperclass
        table: sylius_product_attribute


        EDIT: To debug what's wrong, start by checking warnings in symfony debug bar. It can narrow down your search area for bug. I can also hint you if something is wrong in your configuration file.






        share|improve this answer














        I have figured out the problem. As @czende mentioned properly check the configuration.



        In my case I had set the wrong class in subject while overriding the attribute:



        sylius_product:
        driver: doctrine/orm
        resources:
        product:
        classes:
        repository: AppBundleDoctrineORMProductRepository
        model: AppBundleEntityProduct

        sylius_attribute:
        driver: doctrine/orm
        resources:
        product:
        # Make sure to provide the correct Product class in the subject.
        subject: AppBundleEntityProduct
        attribute:
        classes:
        model: AppBundleEntityProductAttribute
        repository: AppBundleDoctrineORMProductAttributeRepository


        Also make sure to use mappedSuperclass in the mapping config instead of entity as commented by Michał Marcinkowski in issue #3997



        AppBundleEntityProductAttribute:
        type: mappedSuperclass
        table: sylius_product_attribute


        EDIT: To debug what's wrong, start by checking warnings in symfony debug bar. It can narrow down your search area for bug. I can also hint you if something is wrong in your configuration file.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 30 at 5:32

























        answered Nov 29 at 10:30









        Aakash Tushar

        9412




        9412



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238749%2fhow-to-override-product-model-in-sylius%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            How to how show current date and time by default on contact form 7 in WordPress without taking input from user in datetimepicker

            Syphilis

            Darth Vader #20