20250810 v1.0
This commit is contained in:
@@ -13,12 +13,54 @@
|
||||
методом [GetRow](../class_desc/classes/Database.md#61-получение-одного-объекта-getrow):
|
||||
|
||||
```php
|
||||
use goodboyalex\php_db_components_pack\classes\ConditionBuilder;
|
||||
|
||||
$user = $db->GetRow('users', where: new ConditionBuilder()->WhereEquals('user_name', 'Oleg'), className: "\\App\\Models\\User")
|
||||
$user = $db->GetRow('users', where: new ConditionBuilder()->WhereEquals('user_name', 'Oleg'), className: "\\App\\Models\\User");
|
||||
|
||||
echo $user->Id; // Выведет 3
|
||||
echo $user->Email; // Выведет oleg@mail.ru
|
||||
```
|
||||
|
||||
[Предыдущий пункт](insert_data.md) | [На главную](../index.md) | [Следующий пункт](../index.md)
|
||||
Давайте теперь получим пользователей, у которых почта на домене `example.com`. Для этого нам потребуется
|
||||
метод [GetRows](../class_desc/classes/Database.md#62-получение-набора-объектов-getrows):
|
||||
|
||||
```php
|
||||
$users = $db->GetRows('users', where: new ConditionBuilder()->WhereLike('user_mail', '%example.com'), className: "\\App\\Models\\User");
|
||||
|
||||
// Выведет:
|
||||
// Ivan
|
||||
// Olga
|
||||
// Nikolay
|
||||
foreach ($users as $user)
|
||||
echo $user->Name;
|
||||
```
|
||||
|
||||
Давайте теперь немного изменим предыдущую задачу. Пусть нам требуется только столбец `user_name` для всех пользователей.
|
||||
Для этого нам потребуется метод [GetCol](../class_desc/classes/Database.md#63-получение-столбца-getcol):
|
||||
|
||||
```php
|
||||
$users = $db->GetCol('users', 'user_name');
|
||||
|
||||
$users = implode(', ', $users);
|
||||
|
||||
// Выведет: Ivan, Olga, Oleg, Nikolay
|
||||
echo $users;
|
||||
```
|
||||
|
||||
Далее, представим, что вы пишете функцию получения `id` по `user_name`. С помощью
|
||||
метода [GetValue](../class_desc/classes/Database.md#64-получение-одиночного-значения-getvalue) мы это сделаем ниже:
|
||||
|
||||
```php
|
||||
function GetUserId (string $userName): int {
|
||||
// Задаём условие
|
||||
$condition = new ConditionBuilder()->WhereEquals('user_name', $userName);
|
||||
|
||||
// Получаем
|
||||
$id = $db->GetValue('users', 'id', $condition);
|
||||
|
||||
// Возвращаю 0, если ошибка и $id, если всё хорошо
|
||||
return $id == null ? 0 : $id;
|
||||
}
|
||||
```
|
||||
|
||||
Итак, мы рассмотрели все возможные способы получения данных.
|
||||
|
||||
[Предыдущий пункт](insert_data.md) | [На главную](../index.md) | [Следующий пункт](update_data.md)
|
Reference in New Issue
Block a user