datado / data
1.1.6
2016-02-05 23:32 UTC
README
DataDo Data是一种无需编写SQL查询即可轻松与数据库通信的方式。
如何?
DataDo Data通过仓库工作。这意味着对于某个集合(通常是一个SQL表),您会创建一个仓库来与之交互。现在,我听到了您的顾虑,您可能在想:“但如果我不需要编写SQL,我如何从数据库中获取数据?”这正是技巧所在。
入门
/**
* This is the entity we are going to use in this example.
*/
class File {
public $id;
public $filePath;
public $size;
public $fileName;
}
// First we will have to connect to out database
$pdo = new PDO('mysql:host=localhost;dbname=demo', 'username', 'secret_password');
/* Then we create the repository we mentioned earlier. To be able to do this we need
* some information:
*
* - The class of the entity we want to store or get from this repository
* - The PDO connection to the database
* - The name of the property that identifies this entity. This will be used when
* updating and inserting objects
*/
$repository = new Repository(File::class, $pdo, 'id');
使用分析工具
当然,现在这不会起作用。我们甚至还没有在我们的数据库中创建任何表。为了帮助我们做到这一点,仓库提供了一个方便的工具。运行方式如下
$repository->checkDatabase();
如果您在浏览器中查看此输出,您应该会看到您的类和数据库之间连接的概览。大多数的属性行都是读取的,因为您还没有创建表或列。您可以使用此概览作为参考。
插入数据
因此,我们现在已经创建了数据库和表,我们准备插入一些数据。
// Let's create a file object for this current script $file = new File(); $file->filePath = __FILE__; $file->size = filesize(__FILE__); $file->fileName = basename(__FILE__); // Now we can use the repository we created earlier to save this file to the database. $repository->save($file); // Now note that this file has been assigned an id echo $file->id;
获取数据
从数据库中获取数据稍微有些复杂。然而,这也是仓库优势所在的地方。
EQL
DataDo使用最简单的查询语言,这是一种为此项目开发的简单查询机制。它通过在仓库上调用具有特定名称的方法来实现。
// Here's a simple example $myFile = $repository->getByFilePath(__FILE__); // Let's print the result var_dump($myFile);
如果您在上面的代码与第插入数据部分相同的文件中运行了代码,那么您应该已经从数据库中收到了您刚刚创建的对象。这种方式是您可以调用仓库上的非现有方法,只要您遵守特定的语法:(get,find或delete),可选:(fields),可选:By(fields)。
让我们看看一些更多的示例。
// The 'find' query will return a list of all entities that match your query $files = $repository->findByFileName('index.php'); /* You can also request only specific fields. You will then still get a File * object but only the requested fields will be filled out. */ $paths = $repository->findFilePathByFileName(); /* The 'get' query if very much like the 'find' query except this query will * only return the first match. */ $myFile = $repository->getFileByFileSizeAndFileNameLike(1032, '%.php'); /* Then finally there is the 'delete' query. This query does not accept the * selection fields. (The bit before the By keyword). It will delete all * matches of the query. * The return value of this query is the number of affected rows. */ // Delete all files echo $repository->delete(); // Or $repository->deleteAll(); // Delete specific files $repository->deleteById(235);
关键字
- delete - 创建一个将删除所有匹配项的查询
- find - 创建一个将返回匹配项列表的查询
- get - 创建一个将返回单个匹配项的查询
- Distinct - 仅返回不同的值。示例:findDistinctUsername();
- By - 标记过滤器部分的开始
- 过滤器: And - 表示两个过滤器之间的布尔和运算符
- 过滤器: Or - 表示两个过滤器之间的布尔或运算符
- 过滤器: Like - 表示SQL中的LIKE运算符。它接受一个参数。
属性投影
要获取某些属性,您可以在查询模式(delete,find或get)后面跟您想要的属性,这些属性通过And关键字分隔。
请注意,您必须将所需字段的第一个字母大写。
$repository->getIdAndFilePathAndFileSize();
许可证
本项目采用MIT许可证
Copyright (c) 2015 Thomas Biesaart
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.