1. Go to this page and download the library: Download martynbiz/php-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/ */
use MartynBiz\Mongo;
class User extends Mongo
{
// define on the fields that can be saved
protected static $whitelist = array(
'name',
'email',
'username',
'password',
);
}
Connection::getInstance('conn1')->init(array(
...
));
// also, checking if an instance exists
if (! Connection::hasInstance('conn2')) {
Connection::getInstance('conn2')->init(array(
...
));
}
use MartynBiz\Mongo;
class User extends mongo
{
// optional - if using multiple connections/databases
protected static $conn = 'conn1';
.
.
.
}
// setting
// on instantiation -- will be filtered against $whitelist
$article = new Article(array(
'title' => 'My title',
));
$author = User::findOne(array(
//...
));
// single value properties -- no param filtering
$article->status = 2;
$article->author = $author;
// AND/OR set() method, with Mongo instance -- suited for unit testing, no param filtering
$user = User::findOne(array(
//...
));
$article->set('author', $author);
// set value as query result (will be stored as an array of dbrefs)
$tags = Tag::find(array(
//...
));
$article->tags = $tags;
// lastly, params can be passed with save() -- will be filtered against $whitelist
$article->save(array(
'content' => $text,
))
// getting
$article = Article::findOne(array(
//...
));
// single value properties -- no param filtering
echo $article->status;
echo $article->author->name;
echo $article->tags[0]->name;
echo $article->get('author');
$user->name = 'Jim';
$user->save();
$user->save(array(
'name' => 'Jim',
));
// statically
$user = User::create(array(
'name' => 'Jim',
));
// dynamically (e.g. service locator pattern, or DI)
$col = new User();
$user = $col->create(array(
'name' => 'Jim',
));
$user->toArray(3); // convert nested 3 deep to array (optional)
class User extends mongo
{
.
.
.
public function validate()
{
$this->resetErrors();
if (empty($this->data['name'])) {
$this->setError('Name is missing.');
}
return empty( $this->getErrors() ); // true if none
}
}
use MartynBiz\Validator;
class User extends mongo
{
.
.
.
public function validate()
{
$this->resetErrors();
$validator = new Validator($this->data);
$validator->check('name')
->isNotEmpty('Name is missing');
$validator->check('email')
->isNotEmpty('Email address is missing')
->isEmail('Invalid email address');
$message = 'Password must contain upper and lower case characters, and have more than 8 characters';
$validator->check('password')
->isNotEmpty($message)
->hasLowerCase($message)
->hasUpperCase($message)
->hasNumber($message)
->isMinimumLength($message, 8);
// update the model's errors with the validators
$this->setError( $validator->getErrors() );
return empty($this->getErrors());
}
}
use MartynBiz\Mongo;
class User extends Mongo
{
.
.
.
public function getCreatedAt($value)
{
return date('Y-M-d h:i:s', $this->data['created_at']->sec);
}
public function setPassword($value)
{
return password_hash($value, PASSWORD_BCRYPT);
}
}
$ composer
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.