Follow us on Twitter X-Cart on Facebook Wiki
Shopping cart software Solutions for online shops and malls
 

Dealing with Complex Schema API

 
Reply
   X-Cart forums > X-Cart 5 > General questions (X-Cart 5)
 
Thread Tools Search this Thread
  #1  
Old 05-11-2018, 07:15 AM
 
Soptareanu @Alex Soptareanu @Alex is offline
 

Advanced Member
  
Join Date: Apr 2018
Posts: 39
 

Default 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.
__________________
Soptareanu Alex
Reply With Quote
  #2  
Old 05-15-2018, 04:58 AM
  tony_sologubov's Avatar 
tony_sologubov tony_sologubov is offline
 

X-Cart team
  
Join Date: Jan 2009
Posts: 2,431
 

Default 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.
__________________
Found a bug in X-Cart? Post it to our bug tracker!
Know how to make X-Cart better? Suggest an idea!
Reply With Quote

The following user thanks tony_sologubov for this useful post:
Soptareanu @Alex (05-15-2018)
  #3  
Old 05-17-2018, 12:22 AM
 
Soptareanu @Alex Soptareanu @Alex is offline
 

Advanced Member
  
Join Date: Apr 2018
Posts: 39
 

Default 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.
__________________
Soptareanu Alex
Reply With Quote
  #4  
Old 05-17-2018, 06:29 AM
  tony_sologubov's Avatar 
tony_sologubov tony_sologubov is offline
 

X-Cart team
  
Join Date: Jan 2009
Posts: 2,431
 

Default 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.
__________________
Found a bug in X-Cart? Post it to our bug tracker!
Know how to make X-Cart better? Suggest an idea!
Reply With Quote
  #5  
Old 05-18-2018, 01:53 AM
 
Soptareanu @Alex Soptareanu @Alex is offline
 

Advanced Member
  
Join Date: Apr 2018
Posts: 39
 

Default 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.
__________________
Soptareanu Alex
Reply With Quote
  #6  
Old 05-18-2018, 02:06 AM
  tony_sologubov's Avatar 
tony_sologubov tony_sologubov is offline
 

X-Cart team
  
Join Date: Jan 2009
Posts: 2,431
 

Default 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.
__________________
Found a bug in X-Cart? Post it to our bug tracker!
Know how to make X-Cart better? Suggest an idea!
Reply With Quote

The following user thanks tony_sologubov for this useful post:
Soptareanu @Alex (05-18-2018)
Reply
   X-Cart forums > X-Cart 5 > General questions (X-Cart 5)



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump


All times are GMT -8. The time now is 03:10 PM.

   

 
X-Cart forums © 2001-2020