Adding new table in rest api of prestashop webservice

梦想的初衷 提交于 2019-12-24 00:57:03

问题


I've been successful in creating an extra table in the prestashop products table throught rest api of webservice , however the api link http://127.0.0.1/prestashop/api/wb3d/1 wb3d is the new table which I have created in webservice . Which holds a path to an images directory somewhere on the web . This link when opened shows the data which has been saved in the database as seen in the following image below

model is directory name on the web, so this api(wb3d) has been associated with the product table in the webservice the link:http://127.0.0.1/prestashop/api/products/1 when I open this link . The entry of the association is shown but the data is not shown **refer the below image **

The highlighted area shows the wb3d table associated with the product table throught rest api of webservice . I'm unable to associate the wb3d tale data with product table data. So I can use it in other devices through webservice

This is what I have tried so far

    <?php
class ProductMergeCore extends ObjectModel

{  
    public $product_id;
    public $id_wb3d;
    public $directory_name;
    public static $definition = array(
    'table' => 'wb3d',
    'primary' => 'id_wb3d',
    'fields' => array(
    'id_wb3d' => array('type' => self::TYPE_INT,  'required' => true),
    'product_id' => array('type' => self::TYPE_INT, 'required' => true),
    'directory_name' => array('type' => self::TYPE_STRING,  'required' =>false, 'size' => 64),
     ),
     );
    protected $webserviceParameters = array();
    }
    ?>

productmerge.php is responsible for creating an associate table entry in product table.

 <?php 

  Class Product extends ProductCore

  {

   public $extrafield;



public function __construct($id_product = null, $full = false, $id_lang = null, $id_shop = null, Context $context = null)

 {


    $this->webserviceParameters['associations']['wb3d'] = array('resource'    => 'wb3d','fields' => array('directory_name' => array('required' => true)));

    parent::__construct($id_product, $full, $id_lang, $id_shop, $context);

 }

}
?>

In this product.php which is for overriding the product class for passing the extra parameters through webserviceparameters() and then calling the parent constructor of the product class

    <?php
    class WebserviceRequest extends WebserviceRequestCore
    {
    public static function getResources()
    {
    $resources=parent::getResources();
    $resources['wb3d'] = array('description' => 'images path', 'class' =>   'ProductMerge');
    ksort($resources);
    return $resources;
    }
    }
    ?>

WebserviceRequest.php class is a override class for the WebserviceRequest class which shows the description of the table entry in the webservice

These are the files which are required to get the things done. What I'm trying to achieve is the associated table (wb3d) data should be available within the products table through webservice rest api call.


回答1:


if u want to add other webservice table as your association to your product , you can take a look at how associations is done in category.php located in prestashop/classes.

'associations' => array(
     'categories' => array('getter' => 'getChildrenWs', 'resource' => 'category', )         
)

as you can see there is a parameter named getter which gets the value from getChildrenWs which is a method in category.php which fetches the data from database.

so in your case: in Product.php

$this->webserviceParameters['associations']['wb3d'] = array('resource' => 'wb3d',
                                                            'getter' => 'yourMethodName',
                                                            'fields' => array('directory_name' => array('required' => true));

and create a method named 'yourMethodName' in Product.php

public function yourMethodName()
{
  //copy the getChildrenWs method which is in Category.php  and alter it to ur needs
}


来源:https://stackoverflow.com/questions/29971409/adding-new-table-in-rest-api-of-prestashop-webservice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!