pedrof12 / datalayer
一个简单的 MYSQL 数据层!
dev-master
2020-04-04 00:18 UTC
Requires
- php: ^7.2
- ext-pdo: *
This package is auto-updated.
Last update: 2024-09-04 23:48:43 UTC
README
一个为您的数据库而生的简单数据层。
首先,让我们开始配置连接数据!
<?php use PedroF12\Datalayer\Connect; // Chamando a class. $connect = new Connect(); // Criando a conexão. $connect->setConnection("127.0.0.1", 3306, "root", "password")->setDatabase("test"); // Se houver erro, mostre-o na tela. /* * Se você quiser pegar a instancia da conexão para poder executar sql sem passar pelo datalayer é só: */ $conn = Connect::getInstance(); // Retorna a váriavel de conexão PDO. if (Connect::getError()) { print_r(Connect::getError()); } ?>
现在我们来组织我们的数据层,为此创建一个任意命名的文件夹,我创建了 models
,在其中创建一个以所需名称命名的文件 为了保持组织,使用表名。 我创建了 Test.php
。在这个文件中包含以下代码
<?php namespace PedroF12\Datalayer; class Teste extends DataLayer { public function __construct() { parent::__construct("test");// Aqui vai o nome da sua tabela. } }
现在只需调用您的 Test
类,它将包含多个方法。
<?php use PedroF12\Datalayer\Teste; $teste = new Teste(); $teste->get(); $teste->get()->fetch()->foreach(); $teste->get()->fetch()->limit(2)->foreach(); $teste->get()->fetch()->findBy("first_name", 'Teste')->foreach(); $teste->get()->fetch()->findBy("first_name", 'Teste')->limit(2)->foreach(); //Count - retorna a mesma coisa só que em int, ou seja a quantidade de rows com as informações. $teste->get()->fetch()->count(); $teste->get()->fetch()->limit(2)->count(); $teste->get()->fetch()->findBy("first_name", 'Teste')->count(); $teste->get()->fetch()->findBy("first_name", 'Teste')->limit(2)->count();
$teste->get();
- 返回表中的所有数据。$teste->get()->fetch()->foreach();
- 返回表中的所有数据,准备进行 foreach 或 while 循环。$teste->get()->fetch()->limit(2)->foreach();
- 返回 2 条表数据,准备进行 foreach 或 while 循环。$teste->get()->fetch()->findBy("first_name", 'Teste')->foreach();
- 返回first_name
等于Teste
的表中的所有数据。$teste->get()->fetch()->findBy("first_name", 'Teste')->limit(2)->foreach();
- 返回first_name
等于Teste
的 2 条表数据。
我们目前处于版本 0.0.1。
可能存在错误或问题,如果发现任何问题,请通过我的 discord PedroF.#7734
联系我。