1. Go to this page and download the library: Download serebro/reach-mongo 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/ */
// Create model
$client = new Client();
$client->name = 'Google';
// or
$client = new Client(['name' => 'Yandex']);
// or
$client = new Client();
$client->setName('Google')->setDescription('Good Corporation');
// Save
$client->save();
// Count the number of active clients
echo Client::count(['status' => Client::STATUS_ACTIVE]); // 1000
// Get customers as an array
$array = Client::find()->limit(3)->asArray();
// Get a list of the top 10 active clients sorted in alphabetical order
$clients = Client::find(['status' => Client::STATUS_ACTIVE])->sort(['name' => 1])->limit(10);
echo $clients->count(); // 10
echo count($clients); // 10
// Get an array of "_id" all clients
$array_of_mongo_ids = $clients->pluck('_id');
// Convert a list of customers in the array and delete the two attribute
$array_document = $clients->toArray(['exclude' => ['balance', 'partners']]);
// Merge the results of queries
$clients_inactive = Client::find(['status' => Client::STATUS_INACTIVE])->limit(2);
$clients = $clients->mergeWith($clients_inactive);
echo $clients->count(); // 12
// Iterate through the list of clients
foreach($clients as $client) {
echo $client->name;
}
// or
$clients->map(function($client){
echo $client->name;
});
// Additional information about the query used indexes, etc.
$clients->explain();
// Get one customer from the database
$client = Client::findOne(['status' => Client::STATUS_ACTIVE]);
$client = Client::findOne($mongoId);
// Deleting
$client->delete();
Client::deleteAll(['status' => Client::STATUS_INACTIVE]);
class Client extends \Reach\Mongo\Document
{
const STATUS_INACTIVE = 0;
const STATUS_ACTIVE = 1;
// Attributes
/** @var \MongoId */
public $_id;
/** @var int */
public $sequence_id;
public $status = self::STATUS_INACTIVE;
public $name = '';
public $balance = 0;
/** @var \Reach\Mongo\Behavior\LangText */
public $description;
/** @var \City */
public $city;
/** @var \Partners[] */
public $partners = [];
/** @var \MongoDate */
public $created_at;
/** @var \MongoDate */
public $updated_at;
// Behaviors and relations
public function behaviors()
{
return [
// Create an auto-incrementing sequence field
'sequence_id' => ['class' => '\Reach\Mongo\Behavior\Generator\SequenceId'],
'city' => ['class' => Relation::REF_PK, 'ref' => '\City', 'key' => 'city_id'],
'partners' => ['class' => Relation::REF_MANY, 'ref' => '\Partner'],
'description' => ['class' => '\Reach\Mongo\Behavior\LangText'],
];
}
// Methods
public static function getCollectionName()
{
return 'client';
}
public function beforeInsert()
{
$this->created_at = new \MongoDate();
return parent::beforeInsert();
}
public function beforeUpdate()
{
$this->updated_at = new \MongoDate();
return parent::beforeUpdate();
}
public function toArray(array $params = [])
{
$array = parent::toArray($params);
$array['created_at'] = self::date($this->created_at->sec);
$array['updated_at'] = self::date($this->updated_at->sec);
return $array;
}
}
$criteria1 = new \Reach\Mongo\Criteria();
$criteria1->addOr(['status' => Client::STATUS_INACTIVE]);
$criteria1->addOr('created', '<', new \MongoDate());
$criteria2 = new \Reach\Mongo\Criteria();
$criteria2->orderBy(['title' => -1]);
$criteria2->add($criteria1);
$query = TestSchema::query($criteria2);
$resultSet = $query->all()->limit(2);
class ClientQuery extends \Reach\Mongo\Query
{
public function active($status = Client::STATUS_ACTIVE)
{
$this->add(['status' => $status]);
return $this; // it is important
}
}
class Client extends \Reach\Mongo\Document
{
public static function query(CriteriaInterface $criteria = null)
{
return new ClientQuery($criteria, get_called_class());
}
}