1. Go to this page and download the library: Download rayful/db-manager 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/ */
namespace Job;
use rayful\DB\Mongo\DBManager;
class JobManager extends DBManager
{
/**
* 返回本对象的数据库集合名称
* @return string
*/
protected function collectionName()
{
return "job";
}
}
namespace Job;
use rayful\DB\Mongo\Data;
use rayful\DB\Mongo\DBManager;
class Job extends Data
{
/**
* 排序
* @var int
* @name 排序
*/
public $sequence;
/**
* 职位名称
* @var string
* @name 职位名称
*/
public $title;
/**
* 职位描述
* @var string
* @name 职位描述
* @input textarea
*/
public $desc;
/**
* 职位部门
* @var string
* @name 职位部门
*/
public $department;
/**
* 职位要求
* @var string
* @name 职位要求
* @input textarea
*/
public $ public function DBManager()
{
return new JobManager();
}
}
namespace Job;
use rayful\DB\Mongo\Data;
use rayful\DB\Mongo\DataSet;
class Jobs extends DataSet
{
/**
* 声明迭代器返回的对象实例
* @example return new Product(); //Product是Data的子类
* @return Data
*/
protected function iterated()
{
return new Job();
}
}
//增
$Job = new \Job\Job();
$Job->title = "运营总监";
$Job->desc = "你是人才你就过来";
$Job->save();
//查
$id = $_REQUEST['id'];
$Job = new \Job\Job($id);
echo $Job->title;
echo $Job->desc;
//也可以传递一个$query或调用findOne()方法
$Job = new \Job\Job(['title'=>'运营总监']);
//或者
$Job = new \Job\Job();
$Job->findOne(['title'=>'运营总监']); //当然这里的入参也是直接ID的,ID可以是字符串,也可以是MongoID类型,会自动转换
//改
$Job = new \Job\Job($id);
if ($Job->isExists()) {
$Job->title = "技术总监";
$Job->save();
} else {
throw new Exception("职位不存在.");
}
//快捷修改
$Job = new \Job\Job($id);
$Job->update(['$set' => ['title' => $newTitle]]);
//删
$Job = new \Job\Job($id);
$Job->delete();
//打印全部职位名称
$Jobs = new \Job\Jobs();
foreach ($Jobs as $Job) {
echo $Job . "\n";
}
//按照一个条件,次序去找
$Jobs = new \Job\Jobs();
$Jobs->find(['title' => '行政助理']); //入参为MongoDB的query参数,详情请见MongoDB的文档
foreach ($Jobs as $Job) {
echo $Job . "\n";
}
//可以在构造函数内赋值query,与上面的
//支持排序,限制每页个数,支持分页
$Jobs = new \Job\Jobs();
$Jobs->find(['used' => true])->sort(['sequence' => 1])->limit(3);
echo $Jobs->paginate();
foreach ($Jobs as $Job) {
echo $Job->title . ":" . $Job->desc . "\n\n";
}
//批量删(每个控制法)
$Jobs = new \Job\Jobs();
$Jobs->find(['used'=>false])->sort(['sequence'=>1])->limit(3);
foreach ($Jobs as $Job) {
$Job->delete(); //这样子调用还可以调用Model的各种方法,比如检查是否有权限删等
}
//批量删(粗暴法)
$Jobs = new \Job\Jobs();
$Jobs->find(['used' => false])->remove(); //注意调用remove方法前必须要先调用find(或在构造函数时传入query参数)才能执行,安全性的考虑
//找到有多少个
$Jobs = new \Job\Jobs();
$count = $Jobs->find(['used' => false])->count();
//批量改(指定改条件)
$Jobs = new \Job\Jobs();
$Jobs->find(['used' => false])->update(['$set' => ['used' => true]]);
//按照指定条件及排序只找符合条件的第一个
$Jobs = new \Job\Jobs();
$Job = $Jobs->findOne(['used' => true], ['title' => 1]); //第一个参数为query,第二个参数为sort方式
$_REQUEST = [
'used' => "1",
'title' => '技术总监'
];
class Jobs extends DataSet
{
protected function _request_used($value)
{
$this->_query['used'] = boolval($value);
}
protected function _request_title($value)
{
$this->_query['title'] = strval($value);
}
}
$Jobs = new \Job\Jobs();
$Jobs->readRequest();
$Jobs = new \Job\Jobs();
$query = [];
if (isset($_REQUEST['used']))
$query['used'] = boolval($_REQUEST['used']);
if (isset($_REQUEST['title']))
$query['title'] = strval($_REQUEST['title']);
$Jobs->find($query);