X-Cart: shopping cart software

X-Cart forums (https://forum.x-cart.com/index.php)
-   General questions (X-Cart 5) (https://forum.x-cart.com/forumdisplay.php?f=66)
-   -   Dealing with Complex Schema API (https://forum.x-cart.com/showthread.php?t=76237)

Soptareanu @Alex 05-11-2018 07:15 AM

Dealing with Complex Schema API
 
I want to create a complex schema for ProductVariant entity. I follow the implementation of Tony-Restapicomplex module but i got an exception such as :
There is no model schema for that class.
This is my schema :
PHP Code:

<?php

namespace XLite\Module\Tony\RestApiComplexSchema\Core\Schema\Complex;

/**
 * Product schema
 */
class ProductVariant implements \XLite\Module\XC\RESTAPI\Core\Schema\Complex\IModel
{
    
/**
     * Convert model
     *
     * @param \XLite\Model\AEntity $model            Entity
     * @param boolean              $withAssociations Convert with associations
     *
     * @return array
     */
    
public function convertModel(\XLite\Model\AEntity $model$withAssociations)
    {
        return [
            
'id'        => 'My custom field on complex schema !',
        ];
    }

    public function 
prepareInput(array $data)
    {
        return [ 
true$data ];
    }    

    
/**
     * Preload data
     *
     * @param \XLite\Model\AEntity $entity Product
     * @param array                $data   Data
     *
     * @return void
     */
    
public function preloadData(\XLite\Model\AEntity $entity, array $data)
    {
    }
}


And this is the Complex.php file that decorate \XLite\Module\XC\RESTAPI\Core\Schema\Complex :
PHP Code:

<?php

namespace XLite\Module\Tony\RestApiComplexSchema\Core\Schema;

class 
Complex extends \XLite\Module\XC\RESTAPI\Core\Schema\Complex implements \XLite\Base\IDecorator
{
    protected function 
getAllowedEntityClasses()
    {
        
$return parent::getAllowedEntityClasses();
        
$return['XLite\Module\Tony\RestApiComplexSchema\Model\DemoEntity']     = 'XLite\Module\Tony\RestApiComplexSchema\Core\Schema\Complex\DemoEntity';
        
$return['XLite\Module\Tony\RestApiComplexSchema\Model\ProductVariant'] = 'XLite\Module\ProductVariant\Model\ProductVariant';
                

        return 
$return;
    }
}


What is wrong ?! The demo-entity works very well. But ProductVariant not.

tony_sologubov 05-15-2018 04:58 AM

Re: Dealing with Complex Schema API
 
@Alex,
You should try to implement getAllowedEntityClasses() method as follows:

Code:

        protected function getAllowedEntityClasses()
        {
                $return = parent::getAllowedEntityClasses();
                $return['XLite\Module\Tony\RestApiComplexSchema\Model\DemoEntity']    = 'XLite\Module\Tony\RestApiComplexSchema\Core\Schema\Complex\DemoEntity';
                $return['XLite\Module\ProductVariant\Model\ProductVariant'] = 'XLite\Module\Tony\RestApiComplexSchema\Core\Schema\Complex\ProductVariant';
                               

                return $return;
        }


In other words, the key of return array must be name of model class and the value must be name of the class that implements the complex schema.

Please, let me know if it helps.

Quote:

Originally Posted by Soptareanu @Alex
I want to create a complex schema for ProductVariant entity. I follow the implementation of Tony-Restapicomplex module but i got an exception such as :
There is no model schema for that class.
This is my schema :
PHP Code:

<?php

namespace XLite\Module\Tony\RestApiComplexSchema\Core\Schema\Complex;

/**
 * Product schema
 */
class ProductVariant implements \XLite\Module\XC\RESTAPI\Core\Schema\Complex\IModel
{
    
/**
     * Convert model
     *
     * @param \XLite\Model\AEntity $model            Entity
     * @param boolean              $withAssociations Convert with associations
     *
     * @return array
     */
    
public function convertModel(\XLite\Model\AEntity $model$withAssociations)
    {
        return [
            
'id'        => 'My custom field on complex schema !',
        ];
    }

    public function 
prepareInput(array $data)
    {
        return [ 
true$data ];
    }    

    
/**
     * Preload data
     *
     * @param \XLite\Model\AEntity $entity Product
     * @param array                $data   Data
     *
     * @return void
     */
    
public function preloadData(\XLite\Model\AEntity $entity, array $data)
    {
    }
}


And this is the Complex.php file that decorate \XLite\Module\XC\RESTAPI\Core\Schema\Complex :
PHP Code:

<?php

namespace XLite\Module\Tony\RestApiComplexSchema\Core\Schema;

class 
Complex extends \XLite\Module\XC\RESTAPI\Core\Schema\Complex implements \XLite\Base\IDecorator
{
    protected function 
getAllowedEntityClasses()
    {
        
$return parent::getAllowedEntityClasses();
        
$return['XLite\Module\Tony\RestApiComplexSchema\Model\DemoEntity']     = 'XLite\Module\Tony\RestApiComplexSchema\Core\Schema\Complex\DemoEntity';
        
$return['XLite\Module\Tony\RestApiComplexSchema\Model\ProductVariant'] = 'XLite\Module\ProductVariant\Model\ProductVariant';
                

        return 
$return;
    }
}


What is wrong ?! The demo-entity works very well. But ProductVariant not.


Soptareanu @Alex 05-17-2018 12:22 AM

Re: Dealing with Complex Schema API
 
Quote:

Originally Posted by tony_sologubov
@Alex,
You should try to implement getAllowedEntityClasses() method as follows:

Code:

        protected function getAllowedEntityClasses()
        {
                $return = parent::getAllowedEntityClasses();
                $return['XLite\Module\Tony\RestApiComplexSchema\Model\DemoEntity']    = 'XLite\Module\Tony\RestApiComplexSchema\Core\Schema\Complex\DemoEntity';
                $return['XLite\Module\ProductVariant\Model\ProductVariant'] = 'XLite\Module\Tony\RestApiComplexSchema\Core\Schema\Complex\ProductVariant';
                               

                return $return;
        }


In other words, the key of return array must be name of model class and the value must be name of the class that implements the complex schema.

Please, let me know if it helps.


I tested and it works very well. Now I have another issue. Because I have very much products and productvariants, sometimes I get an 500 error because the server takes much time to respond. I want to create a tool that pull products from API in batches but for that I need instead reciving all products, get only number of rows. Is there a posibility to do that ? By default convertModel() function fetching result for each rows of entity.

tony_sologubov 05-17-2018 06:29 AM

Re: Dealing with Complex Schema API
 
Quote:

Originally Posted by Soptareanu @Alex
I tested and it works very well. Now I have another issue. Because I have very much products and productvariants, sometimes I get an 500 error because the server takes much time to respond. I want to create a tool that pull products from API in batches but for that I need instead reciving all products, get only number of rows. Is there a posibility to do that ? By default convertModel() function fetching result for each rows of entity.


Did you try using _cnd[limit] in your request as explained here:
https://devs.x-cart.com/rest-api/#limiting-the-output-pagination

Just add something like _cnd[limit][0]=0&_cnd[limit][1]=100 to your request and it will pull batches of entities instead of all.

Soptareanu @Alex 05-18-2018 01:53 AM

Re: Dealing with Complex Schema API
 
Quote:

Originally Posted by tony_sologubov
Did you try using _cnd[limit] in your request as explained here:
https://devs.x-cart.com/rest-api/#limiting-the-output-pagination

Just add something like _cnd[limit][0]=0&_cnd[limit][1]=100 to your request and it will pull batches of entities instead of all.

Yes, this is what i want to do, but for that i need to create a script that do this task automatically and for that would help me if I know from the beggining the total number of the rows that i need to pull in order to set my script when to stop the request.

tony_sologubov 05-18-2018 02:06 AM

Re: Dealing with Complex Schema API
 
Quote:

Originally Posted by Soptareanu @Alex
Yes, this is what i want to do, but for that i need to create a script that do this task automatically and for that would help me if I know from the beggining the total number of the rows that i need to pull in order to set my script when to stop the request.


Knowing total number of rows is not necessary as you should stop sending requests once you receive first empty result.


All times are GMT -8. The time now is 06:48 PM.

Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.