PHP code example of denchikby / phalcon-mongodb-odm
1. Go to this page and download the library: Download denchikby/phalcon-mongodb-odm 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/ */
denchikby / phalcon-mongodb-odm example snippets
$di->set('config', function () {
return new \Phalcon\Config([
'mongodb' => [
'host' => 'localhost',
'port' => 27017,
'database' => 'auto'
]
]);
}, true);
$di->set('mongo', function () use ($di) {
$config = $di->get('config')->mongodb;
$manager = new \MongoDB\Driver\Manager('mongodb://' . $config->host . ':' . $config->port);
return $manager;
}, true);
use DenchikBY\MongoDB\Model;
class User extends Model {}
use DenchikBY\MongoDB\Model;
class User extends Model
{
public static function getSource()
{
return 'users';
}
}
/**
* @method $this active()
*/
class BaseModel extends Model
{
public scopeActive($builder)
{
return $builder->where('active', 1);
}
}
$users = User::active()->get();
$ads = Ads::active()->get();
class Ads extends Model
{
public static $globalScopes = ['notDeleted'];
public function notDeleted($builder)
{
return $builder->where('deleted', 0);
}
}
class User extends Model
{
public function getName($value)
{
return ucfirst($value);
}
public function setPassword($value)
{
return Di::getDefault()->get('security')->hash($value);
}
}
class User extends Model
{
public function afterCreate()
{
Email::send($this->email, 'emails.succeddfull_registration', ['user' => $this]);
}
}
$users = User::query()->get();
$users = User::get();
$builder = User::query();
//allowed operators in where =, !=, >, <, >=, <=
$builder->where('name', '=', 'DenchikBY');
//similar
$builder->where('name', 'DenchikBY');
$builder->orWhere('name', 'Denis');
$builder->betweenWhere('age', 20, 30);
$builder->notBetweenWhere('age', 20, 30);
$builder->inWhere('name', ['DenchikBY', 'Denis']);
$builder->notInWhere('name', ['DenchikBY', 'Denis']);
$builder->orderBy('created_at', 'desc');
$builder->limit(2, 1);
//Closing methods:
$users = $builder->get(); // return collection of models
$user = $builder->first(); // return first model without collection
$count = $builder->count(); // run count command, which return int of counted documents
$count = $builder->increment('coins', 10); // increase field in founded documents, return count of them
$count = $builder->decrement('coins', 10);
$count = $builder->update(['banned' => 1]); // update founded documents with specified fields, return count of them
$count = $builder->delete(); // delete founded documents, return count of them
$age = $builder->max('age');
$age = $builder->min('age');
$age = $builder->avg('age');
$total = $builder->sum('age');
$builder->unsetField('counters.views');