汤混合/基础

此包已被弃用且不再维护。未建议替代包。

简单的数据库抽象层适配器集合,用于处理 CRUD 操作。

0.8 2017-07-23 15:17 UTC

This package is auto-updated.

Last update: 2021-06-26 15:24:37 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

使用 PHP 编写的简单数据库抽象层适配器集合,用于处理 CRUD 操作。此库不提供任何 ORM 或 ODM。

适配器

  • MongoDB:存在
  • Elasticsearch:存在
  • Couchbase:计划中
  • Doctrine DBAL:存在 ** MySQL ** PostgreSQL ** Microsoft SQL ** Oracle ** SQLite

安装

此库只包含接口和抽象类。要使用数据库,您必须安装针对该数据库编写的包。例如:用于 MongoDB 的 soupmix/mongodb;

建议您使用 Composer 安装 Soupmix。

$ composer require soupmix/base "~0.7"

这将安装 Soupmix 及其所有依赖项。Soupmix 需要 PHP 5.6.0 或更高版本。

文档

API 文档:查看数据库适配器函数的详细信息

使用

// Connect to MongoDB Service
$adapter_config = [];
$adapter_config['db_name'] ='db_name';
$adapter_config['connection_string']="mongodb://127.0.0.1";
$adapter_config['options'] =[];
$config['db_name'] = $adapter_config['db_name];
$client = new \MongoDB\Client($adapter_config['connection_string'], $adapter_config['options']);
$m=new Soupmix\MongoDB($config, $client);

// Connect to Elasticsearch Service
$adapter_config             = [];
$adapter_config['db_name']  = 'indexname';
$adapter_config['hosts']    = ["127.0.0.1:9200"];
$adapter_config['options']  = [];
$config['db_name'] = $adapter_config['db_name];
$client = \Elasticsearch\ClientBuilder::create()->setHosts($adapter_config['hosts'])->build();
$e=new Soupmix\ElasticSearch($config, $client);

// Connect SQL Service 

$config = [
    'dbname'    => 'test',
    'user'      => 'root',
    'password'  => '',
    'host'      => '127.0.0.1',
    'port'      => 3306,
    'charset'   => 'utf8',
    'driver'    => 'pdo_mysql',
];
$client = \Doctrine\DBAL\DriverManager::getConnection($config);
$sql = new \Soupmix\SQL(['db_name'=>$config['dbname']], $client);


$docs = [];
$docs[] = [
	"full_name" => "John Doe",
      "age" => 33,
      "email"	=> "johndoe@domain.com",
      "siblings"=> [
        "male"=> [
          "count"=> 1,
          "names"=> ["Jack"]
        ],
        "female"=> [
          "count" => 1,
          "names" =>["Jane"]
		]      
      ]
];
$docs[] = [
	"full_name" => "Jack Doe",
      "age" => 38,
      "email"	=> "jackdoe@domain.com",
      "siblings"=> [
        "male"=> [
          "count"=> 1,
          "names"=> ["John"]
        ],
        "female"=> [
          "count" => 1,
          "names" =>["Jane"]
		]      
      ]
];

$docs[] = [
	"full_name" => "Jane Doe",
      "age" => 29,
      "email"	=> "janedoe@domain.com",
      "siblings"=> [
        "male"=> [
          "count"=> 2,
          "names"=> ["Jack","John"]
        ],
        "female"=> [
          "count" => 0,
          "names" =>[]
		]      
      ]
];

foreach($docs as $doc){
	// insert user into database
	$mongo_user_id = $m->insert("users",$doc);
	$es_user_id = $e->insert("users",$doc);

}
// get user data using id
$es_user_data = $e->get('users', "AVPHZO1DY8UxeHDGBhPT");


$filter = ['age_gte'=>0];
// update users' data that has criteria encoded in $filter
$set = ['is_active'=>1,'is_deleted'=>0];

$e->update("users",$)

$filter = ["siblings.male.count__gte"=>2];

//delete users that has criteria encoded in $filter
$e->delete('users', $filter);



// user's age lower_than_and_equal to 34 or greater_than_and_equal 36 but not 38
$filter=[[['age__lte'=>34],['age__gte'=>36]],"age__not"=>38];

//find users that has criteria encoded in $filter
$docs = $e->find("users", $filter);


贡献

  • 如果发现错误,请提出问题或发送拉取请求。
  • 如果您有任何问题,请随时提出。