Extending query from spryker/vendor to pyz

bhupendra.doniwal
bhupendra.doniwal Posts: 53 🧑🏻‍🚀 - Cadet

Hi Team,

I am trying to add an extra column in the spyOmsOrderItemStateHistory table while changing the state from the backoffice. I discovered that entries for this history table are internally generated from the spySalesOrderItem. The issue seems to stem from a call generated by ORM, specifically invoking the method located at:
vendor/spryker/sales/src/Spryker/Zed/Sales/Persistence/Propel/AbstractSpySalesOrderItem.php::postSave.

For testing, I successfully set and retrieved the new value. However, when I try to extend this functionality into the Pyz namespace, it continues to reference the vendor path instead. Is there a solution for ensuring my extended code is executed rather than the vendor path?

Tagged:

Answers

  • Hidran Arias
    Hidran Arias Senior Technical Trainer Sprykee Posts: 86 🏛 - Council (mod)
    edited October 10

    Did you update the DTO to mirror this new column?

    what part did you override from the core?
    Take a look at the replacement rules if that can help:
    https://docs.spryker.com/docs/dg/dev/backend-development/extend-spryker/spryker-os-module-customisation/extend-the-spryker-core-functionality.html#folder-organization

  • bhupendra.doniwal
    bhupendra.doniwal Posts: 53 🧑🏻‍🚀 - Cadet

    Hi Hidran,

    Thanks for your response. I'm familiar with the DTO update process, but my case is slightly different. Here's an example of how the functionality is used:

    public function postSave(?ConnectionInterface $con = null): void


    {


    if ($this->statusChanged)

    {
    /** @var \Orm\Zed\Sales\Persistence\SpySalesOrderItem $salesOrderItemEntity */
    $salesOrderItemEntity = $this;

        // Creating history entry
    $omsOrderItemStateHistoryEntity = $this->createOmsOrderItemStateHistoryEntity();
    $omsOrderItemStateHistoryEntity->setOrderItem($salesOrderItemEntity);
    $omsOrderItemStateHistoryEntity->setState($this->getState());

    // Custom additions for user tracking (currently commented)
    // $omsOrderItemStateHistoryEntity->setFkUserId($salesOrderItemEntity->getFkUserId());
    // $omsOrderItemStateHistoryEntity->setUsername($this->getSpyUser()->getUsername());

    $omsOrderItemStateHistoryEntity->save();
    }
    $this->statusChanged = false;

    }

    In my scenario, the challenge is extending this core functionality where postSave() is automatically invoked. Even after extending the class into Pyz, it's still referencing the vendor file. Would appreciate any advice on how to properly redirect this behavior to my custom logic.

  • Hidran Arias
    Hidran Arias Senior Technical Trainer Sprykee Posts: 86 🏛 - Council (mod)

    That class is generated by Propel and you can extend the abstract class.
    To use use your class, you would have to extend the SalesPersistenceFactory class and project level and override the method which provides the SpySalesOrderItemQuery with your extended class.

    For SpySalesOrderItem, you already have the class which extends from the Abstract core class

    Those classes are already found in src/Orm/Zed/Sales:
    /** * Skeleton subclass for representing a row from the 'spy_sales_order_item' table. * * * * You should add additional methods to this class to meet the * application requirements. This class will only be generated as * long as it does not already exist in the output directory. */

    class SpySalesOrderItem extends BaseSpySalesOrderItem{}

    So, if you override the postSave method in this class, it would be used in place of the core Abstract postSave

  • bhupendra.doniwal
    bhupendra.doniwal Posts: 53 🧑🏻‍🚀 - Cadet

    Hi Hidran,

    Thanks for the insights! I’m trying to understand how to implement this in my scenario. Specifically, how do I override the createSalesOrderItemQuery function in my project to make it return my extended class?

    Currently, the function looks like this:

    /**
    * @return \Orm\Zed\Sales\Persistence\SpySalesOrderItemQuery
    */
    public function createSalesOrderItemQuery()
    {
    return SpySalesOrderItemQuery::create();
    }

    The challenge is that this method is in Spryker\Zed\Sales\Persistence\Propel\AbstractSpySalesOrderItem and it extends \Orm\Zed\Sales\Persistence\Base\SpySalesOrderItem. I need guidance on how to override this method to return my custom class. Could you elaborate on this or give an example of how to structure the return so that it references my project-level class?

    Thanks!

  • Hidran Arias
    Hidran Arias Senior Technical Trainer Sprykee Posts: 86 🏛 - Council (mod)

    Hi,

    What I posted for SpySalesOrderItem, is valid also for SpySalesOrderItemQuery

    Spryker uses the one that gets publish into ORM/Zed when propel:install in executed