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
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,
]);