PHP code example of haiflive / orientdb-yii2-connector

1. Go to this page and download the library: Download haiflive/orientdb-yii2-connector library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

haiflive / orientdb-yii2-connector example snippets


...
'db' => => 




return [
    'class' => 'OrientDBYii2Connector\Connection',
    'hostname' => 'localhost',
    'port' => 2424,
    'dbname' => 'OpenBeer',
    'username' => 'root',
    'password' => 'password',
];

use PhpOrient\PhpOrient;
use PhpOrient\Protocols\Binary\Data\Record;
use OrientDBYii2Connector\Query;

    $client = Yii::$app->dborient->createCommand();
    
    // create new record
    $just_inserted = $client->insert('beer', [
          'name' => 'test2',
          'descript' => $longText,
    ])->execute();
    
    // select just created record by RID
    $data = (new Query())->from('beer')
      ->where(['@rid'=>$just_inserted['@rid']])
      ->one();
      
    var_dump($data);
    
    // get list of records:
    $data_list = (new Query())
      ->from('beer')
      ->fetch_plan(['out_HasCategory.in:0', 'out_HasBrewery.in:0', 'out_HasStyle.in:0'])
      // ->select('name', 'descript', 'out_HasCategory.in.exclude(\'in_HasCategory\')')
      // ->where(['name' => 'Hocus Pocus'])
      // ->limit(10)
      // support all Yii methods, without join (no join in OrientDB)
      ->all();
    
    foreach($data as $key => $record) {
        var_dump($record);
        // do something
    }
    
    // remove record
    $sql = $client->delete('beer', [
        'name' => 'test2',
    ])->execute();

    $good_insert = $client->insert('Goods', [
        'GoodsDescription' => 'test Query GoodsDescription',
        'GoodsQuantity' => '11',
    ])->execute();

    // get by RID
    $good_select = (new Query())->from('Goods')
      ->where(['@rid'=>$good_insert['@rid']])
      ->one();
      
    // or query All
    $test_query_all = (new Query())
      ->from('Goods')
      ->all();

// -- embedded relation
    $expense_insert = $client->insert('Expense', [
       'Name' => 'test Query Expense related',
       'Price' => '1021',
       // 'executor' => // linlk
       'prices' => [[ // embedded list (has_many)
         // "@type" => "d",
         // "@version" => 0,
         "@class" => "Price", //! '
    ])->execute();

    // create `Expense` with link relation `executor`
    $expense_insert = $client->insert('Expense', [
        'Name' => 'test Query Expense related',
        'Price' => '1021',
        'executor' => $organization_insert['@rid'] // link
    ])->execute();

    // get by RID and load with link relation
    $expense_select = (new Query())->from('Expense')
      ->where(['@rid'=>$expense_insert['@rid']])
      ->fetch_plan('executor:0') // `:0` - is level (read more in OrientDB documentation about fetch_plan)
      ->one();



namespace data;

use Yii;

class oDeal extends \OrientDBYii2Connector\ActiveRecord
{
    public static function tableName()
    {
        return 'Deal';
    }

    public function getSender()
    {
        return $this->embeddedOne(oOrganization::className(), 'sender');
    }

    public function getGoods()
    {
        return $this->embeddedMany(oGoods::className(), 'goods');
    }
    
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['@class', '@rid', 'CurrencyCode', 'Name', 'Note', 'Number'], 'string'],
            [['@version'], 'integer'],
            [['Date'], 'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            '@class' => '@class',
            '@rid' => '@rid',
            '@version' => '@version',
            'CurrencyCode' => 'Currency Code',
            'Date' => 'Date',
            'Name' => 'Name',
            'Note' => 'Note',
            'Number' => 'Number',
            'addressFrom' => 'Address From',
            'addressTo' => 'Address To',
            'expenses' => 'Expenses',
            'goods' => 'Goods',
            'reciver' => 'Reciver',
            'sender' => 'Sender',
        ];
    }
}



$dataProvider = new ActiveDataProvider([
    'query' => Deal::find(),
    'pagination' => ['pageSize' => 10],
]);

echo yii\grid\GridView::widget([
    'dataProvider' => $dataProvider,
]);

// also use pager:
echo \yii\widgets\LinkPager::widget([
    'pagination'=>$dataProvider->pagination,
]);

//simulate form input
$post = [
  'oDeal' => [
    'CurrencyCode' => 'USD',
    'Date' => date('Y-m-d'),
    'Name' => 'unit_test_deal',
    'Note' => 'testing relations',
    'Number' => '0001',
    // embedded relations data:
    'addressTo' => [
      '@class'=>'Address', //!
      'PostalCode' => '692500',
      'CountryCode' => 'RU',
      'Region' => 'Primorsky kray',
      'City' => 'Ussuriisk',
      'StreetHouse' => 'Nekrasova street, 25',
      'LanguageCode' => 'EN'
    ],
    'addressFrom' => [
      '@class'=>'Address', //!
      'PostalCode' => '692500',
      'CountryCode' => 'RU',
      'Region' => 'Primorsky kray',
      'City' => 'Ussuriisk',
      'StreetHouse' => 'Nekrasova street, 25',
      'LanguageCode' => 'EN'
    ],
    // embedded  list relations:
    'goods' => [[
      '@class'=>'Goods', //!
      'GoodsShortDescription' => 'Test Goods Short Description',
      'GoodsDescription' => 'Full text Goods Description'
    ],[
      '@class'=>'Goods', //!
      'GoodsShortDescription' => 'Test Goods Short Description',
      'GoodsDescription' => 'Full text Goods Description'
    ]],
    // embedded list relations:
    'expenses' => [[
      '@class'=>'Expense', //!
      'Name' => 'Test Expense',
      'Price' => '100.00',
      'CurrencyCode' => 'USD',
      'Margin' => '0',
      'Cost' => '100.00',
      'prices' => [[ // has_many
        '@class'=>'Price', //!
        'Price' => '110.00',
        'Cost' => '100.00',
        'Discount' => '0',
        'QuantityMeasure' => '796',
        'Quantity' => '1',
        'transport' => [
          '@class'=>'Transport', //!
          'TransportIdentifier' => 'AB0202AM23',
          'NameMrkCar' => 'KAMAZ'
          'driver' => [
             '@class'=>'Resident', //!
             'role' => '2',
             'PersonSurname' => 'Ivanov',
             'PersonName' => 'Ivan'
          ]
        ],
        'goods' => [[
           '@class'=>'Goods', //!
           'GoodsShortDescription' => 'Test Goods Short Description',
           'GoodsQuantity' => '100',
           'MeasureUnitQualifierCode' => '796'
        ],[
           '@class'=>'Goods', //!
           'GoodsShortDescription' => 'Test Goods Short Description',
           'GoodsQuantity' => '100',
           'MeasureUnitQualifierCode' => '796'
        ]]
      ],[
        '@class'=>'Price', //!
        'Price' => '110.00',
        'Cost' => '100.00',
        'Discount' => '0',
        'QuantityMeasure' => '796',
        'Quantity' => '1'
      ]]
    ],[
      '@class'=>'Expense', //!
      'Name' => 'Test Expense',
      'Price' => '100.00',
      'CurrencyCode' => 'USD',
      'Margin' => '0',
      'Cost' => '100.00'
    ]],
  ]
];

$deal = new oDeal;

// create relations
$executor = $this->testCreateExecutor(); // has services(LINKLIST)
$sender = $this->testCreateSender();
$reciver = $this->testCreateReciver();
$address1 = $this->testCreateAddress();
$address2 = $this->testCreateAddress();
$driver = $this->testCreateResident();

$deal->load($post) // Load deal POST data;

// link relation data
// link data as embedded via link() (@rid will be automatically removed because `sender` relation is embedded)
$deal->link('sender', $sender); // embedded relation (embeddedOne)

// or you can use magic methods instead link() method
unset($reciver->{'@rid'}); // embedded relation 

// SELECT * FROM `Price` WHERE `@rid` = '#13:5'
$price = oPrice::find()
            ->where(['@rid' => '#13:5'])
            ->one();

// SELECT * FROM `Address` WHERE `@rid` = '#14:10'
$delivery = $price->delivery; // link

SELECT * FROM `Price` WHERE `@rid` = '#13:5' fetchplan delivery:0
$price = oPrice::find()
            ->where(['@rid' => '#13:5'])
            ->with(['delivery']) // link
            ->one();

// no SQL executed
$delivery = $price->delivery;

$priceFind = oPrice::find()
               ->where(['@rid' => $price['@rid']])
               ->with(['transport']) // embedded
               ->one();