Jahangir Alam Jahangir Alam - 3 months ago 32
PHP Question

PrestaShop: Can't access newly added resource through webservice

I have created users table in Prestashop database in phpMyAdmin, the schema of that table is :

CREATE TABLE users (
id int NOT NULL AUTO_INCREMENT,
Token int NOT NULL,
PRIMARY KEY (id),
UNIQUE KEY (Token)
);


I want to add this table in my webservice resource list as like other resources and access that resource from another application.

To do this I have done so far the following tasks:


  1. I have created
    /prestashop/mymodule/override/classes/webservice/ WebserviceRequest.php
    :

    class WebserviceRequest extends WebserviceRequestCore {
    public static function getResources(){
    $resources = parent::getResources();
    $resources['users'] = array('description' => 'Device registration', 'class' => 'Users');
    ksort($resources);
    return $resources;
    }
    }

  2. and
    /prestashop/mymodule/override/classes/Users.php
    :

    class Users extends ObjectModel {
    public static $definition = array(
    'table' => 'users',
    'primary' => 'id',
    'fields' => array(
    'Token' => array('type' => self::TYPE_INT)
    )
    );
    protected $webserviceParameters = array();
    }



As a result I found the table in webservice resource list. But when I hit the URL:
example.com/api/users
no XML data is returned.

I have googled a lot for the problem but didn't find any effective solution for: How to add a new database table
users
in webservice resources and access the resource using
example.com/api/users
URL.

Answer

All correct but, only small corrections:

table must have the prefix choosed during installation (default is ps_) :

CREATE TABLE ps_users (
 id int NOT NULL AUTO_INCREMENT,
 Token int NOT NULL,
 PRIMARY KEY (id),
 UNIQUE KEY (Token)
);

Point 1 is ok ;)

Point 2, don't forget to add the table field(s) as public var otherwise the XML will not show the informations stored in the db:

class UsersAPI extends ObjectModel {
    public $Token;

    public static $definition = array(
        'table' => 'users',
        'primary' => 'id',
        'fields' => array(
            'Token' => array('type' => self::TYPE_INT)
        )
    );
    protected $webserviceParameters = array();
}

EDIT: If you have made an override don't forget to delete the prestashop/cache/class_index.php.