qeti/yii2-podata-adapter

Adapter for using POData with Yii2

v0.9 2015-09-26 04:57 UTC

This package is auto-updated.

Last update: 2024-03-21 01:35:05 UTC


README

What is this?

Adapter for using OData in Yii2 with library POData.

Who is using it?

Installation

PHP 5.4 or higher is required to use it. Yii2 framework is required to use it.

Installation is recommended to be done via composer. Add the following to the require and repositories sections in composer.json of Yii2 project:

    "require": {
       "qeti/simple-podata": ">=0.9.1"
    }

Run composer update afterwards.

Usage

In your basic Yii2 project

Add the following to the configuration file web.php:

    'components' => [
        'urlManager' => [
            'enablePrettyUrl' => true,
            'showScriptName' => false,
            'rules' => [
                ['class' => 'qeti\Yii2PODataAdapter\ODataUrlRule'],
            ],
        ],
    ],
    'controllerMap' => [
        'o-data' => 'qeti\Yii2PODataAdapter\implementation\ODataController',
    ],

Set up class map for MetaProvider:

yii::$classMap['qeti\Yii2PODataAdapter\implementation\MetadataProvider'] = '@app/models/OData/MetadataProvider.php';

Create classes, what describe entities, for example:

namespace app\models\OData\Entities;

use yii\db\ActiveRecord;
use qeti\SimplePOData\EntityTrait;

class Request extends ActiveRecord {

    use EntityTrait;

    public $id;
    public $type_code;
    public $status_code;
}

Create MetaProvider class, what describe all entities for POData, for example:

namespace qeti\Yii2PODataAdapter\implementation;

use POData\Providers\Metadata\Type\EdmPrimitiveType;
use POData\Providers\Metadata\SimpleMetadataProvider;

class MetadataProvider
{
    const MetaNamespace = "Data";

    /**
     * @return IMetadataProvider
     */
    public static function create()
    {
        $metadata = new SimpleMetadataProvider('Data', self::MetaNamespace);

        $requestEntityType = self::createRequestEntityType($metadata);
        $requestResourceSet = $metadata->addResourceSet('Requests', $requestEntityType);

        return $metadata;
    }

    private static function createRequestEntityType(SimpleMetadataProvider $metadata)
    {
        $et = $metadata->addEntityType(new \ReflectionClass('app\models\OData\Entities\Request'), 'Requests', self::MetaNamespace);
        $metadata->addKeyProperty($et, 'id', EdmPrimitiveType::INT64); 
        $metadata->addPrimitiveProperty($et, 'type_code', EdmPrimitiveType::STRING);
        $metadata->addPrimitiveProperty($et, 'status_code', EdmPrimitiveType::STRING);
        return $et;
    }

}

Try to open links:

  • http:///odata.svc/$metadata

  • http:///odata.svc/Requests?$format=json&$skip=1&$top=14&$inlinecount=allpages&$filter=status_code eq 'held'

  • http:///odata.svc/Requests/$count?&$filter=status_code eq 'held'

  • http:///odata.svc/Requests(2465)

For more details about URL format, see OData documentation.

Am I free to use this?

This library is open source and licensed under the MIT License. This means that you can do whatever you want with it as long as you mention my name and include the license file. Check the license for details.

Contact

Feel free to contact me using email.