20250808-1
This commit is contained in:
parent
caf65933d1
commit
1577747e6f
@ -30,9 +30,11 @@ namespace App\Models;
|
|||||||
|
|
||||||
use goodboyalex\php_db_components_pack\attributes\AutoIncrement;
|
use goodboyalex\php_db_components_pack\attributes\AutoIncrement;
|
||||||
use goodboyalex\php_db_components_pack\attributes\FieldName;
|
use goodboyalex\php_db_components_pack\attributes\FieldName;
|
||||||
|
use goodboyalex\php_db_components_pack\attributes\IgnoredInDB;
|
||||||
use goodboyalex\php_db_components_pack\attributes\NotNull;
|
use goodboyalex\php_db_components_pack\attributes\NotNull;
|
||||||
use goodboyalex\php_db_components_pack\attributes\PrimaryKey;
|
use goodboyalex\php_db_components_pack\attributes\PrimaryKey;
|
||||||
use goodboyalex\php_db_components_pack\attributes\Unique;
|
use goodboyalex\php_db_components_pack\attributes\Unique;
|
||||||
|
use goodboyalex\php_db_components_pack\enums\DBOperation;
|
||||||
use goodboyalex\php_db_components_pack\interfaces\IDBItem;
|
use goodboyalex\php_db_components_pack\interfaces\IDBItem;
|
||||||
|
|
||||||
class User implements IDBItem
|
class User implements IDBItem
|
||||||
@ -40,7 +42,7 @@ class User implements IDBItem
|
|||||||
/**
|
/**
|
||||||
* @var int $Id Идентификатор пользователя.
|
* @var int $Id Идентификатор пользователя.
|
||||||
*/
|
*/
|
||||||
#[PrimaryKey, NotNull, AutoIncrement, FieldName('id'), Unique]
|
#[PrimaryKey, NotNull, AutoIncrement, FieldName('id'), Unique, IgnoredInDB(DBOperation::Insert)]
|
||||||
public int $Id = 0;
|
public int $Id = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -54,6 +56,19 @@ class User implements IDBItem
|
|||||||
*/
|
*/
|
||||||
#[NotNull, FieldName('user_mail'), Unique]
|
#[NotNull, FieldName('user_mail'), Unique]
|
||||||
public string $Email = '';
|
public string $Email = '';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Конструктор.
|
||||||
|
*
|
||||||
|
* @param int $id Идентификатор пользователя
|
||||||
|
* @param string $name Имя пользователя
|
||||||
|
* @param string $email Адрес EMAIL
|
||||||
|
*/
|
||||||
|
public function __construct(int $id = 0, string $name = "", string $email = "") {
|
||||||
|
$this->Id = $id;
|
||||||
|
$this->Name = $name;
|
||||||
|
$this->Email = $email;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -4,60 +4,62 @@
|
|||||||
научимся добавлять данные в неё.
|
научимся добавлять данные в неё.
|
||||||
|
|
||||||
Для добавления одной строки нужно воспользоваться
|
Для добавления одной строки нужно воспользоваться
|
||||||
методом [Insert](../class_desc/classes/Database.md).
|
методом [Insert](../class_desc/classes/Database.md#51-одинарная-вставка-insert).
|
||||||
|
|
||||||
Создадим далее макет класса, описывающего пользователя сайта.
|
Заполним далее класс `User` следующими данными:
|
||||||
|
|
||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
|
$oneUser = new User (1, "Ivan", "ivan@example.com");
|
||||||
|
```
|
||||||
|
|
||||||
namespace App\Models;
|
Давайте добавим эти данные в таблицу `users`.
|
||||||
|
|
||||||
use goodboyalex\php_db_components_pack\attributes\AutoIncrement;
|
```php
|
||||||
use goodboyalex\php_db_components_pack\attributes\FieldName;
|
$result = $db->Insert('users', $oneUser);
|
||||||
use goodboyalex\php_db_components_pack\attributes\NotNull;
|
|
||||||
use goodboyalex\php_db_components_pack\attributes\PrimaryKey;
|
|
||||||
use goodboyalex\php_db_components_pack\attributes\Unique;
|
|
||||||
use goodboyalex\php_db_components_pack\interfaces\IDBItem;
|
|
||||||
|
|
||||||
class User implements IDBItem
|
if ($result === false)
|
||||||
{
|
echo "Произошла ошибка!";
|
||||||
/**
|
else
|
||||||
* @var int $Id Идентификатор пользователя.
|
echo "Запись добавлена! Идентификатор записи: $result";
|
||||||
*/
|
```
|
||||||
#[PrimaryKey, NotNull, AutoIncrement, FieldName('id'), Unique]
|
|
||||||
public int $Id = 0;
|
После этого, таблица в БД примет вид:
|
||||||
|
|
||||||
/**
|
| id | user_name | user_mail |
|
||||||
* @var string $Name Имя пользователя.
|
|:--:|:---------:|:----------------:|
|
||||||
*/
|
| 1 | Ivan | ivan@example.com |
|
||||||
#[NotNull, FieldName('user_name')]
|
|
||||||
public string $Name = '';
|
Пусть далее, у нас даны несколько записей:
|
||||||
|
|
||||||
/**
|
```php
|
||||||
* @var string $Email Почта пользователя.
|
<?php
|
||||||
*/
|
$twoUser = new User (name: "Olga", email: "olga@example.com");
|
||||||
#[NotNull, FieldName('user_mail'), Unique]
|
$threeUser = new User (name: "Oleg", email: "oleg@mail.ru");
|
||||||
public string $Email = '';
|
$fourUser = new User (name: "Nikolay", email: "nikolay@example.com");
|
||||||
|
```
|
||||||
|
|
||||||
|
Давайте добавим их в БД. Для добавления сразу стольких объектов подойдёт
|
||||||
|
метод [InsertMany](../class_desc/classes/Database.md#52-массовая-вставка-insertmany):
|
||||||
|
|
||||||
|
```php
|
||||||
|
$result = $db->InsertMany('users', $twoUser, $threeUser, $fourUser);
|
||||||
|
|
||||||
|
if ($result === false)
|
||||||
|
echo "Произошла ошибка!";
|
||||||
|
else {
|
||||||
|
$idsList = implode(', ', $result);
|
||||||
|
echo "Записи добавлена! Идентификаторы записей: $idsList";
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Итак, у нас в таблице должно быть **3 столбца**:
|
После этого, таблица в БД примет вид:
|
||||||
|
|
||||||
| id | user_name | user_mail |
|
| id | user_name | user_mail |
|
||||||
|:--:|:---------:|:---------:|
|
|:--:|:---------:|:-------------------:|
|
||||||
| - | - | - |
|
| 1 | Ivan | ivan@example.com |
|
||||||
|
| 2 | Olga | olga@example.com |
|
||||||
Значения во всех столбцах, кроме `user_name` должны быть уникальными, во всех столбцах не пустыми, а первичным ключом
|
| 3 | Oleg | oleg@mail.ru |
|
||||||
таблицы является столбец `id` с автозаполнением.
|
| 4 | Nikolay | nikolay@example.com |
|
||||||
|
|
||||||
Теперь давайте создадим таблицу `user`:
|
|
||||||
|
|
||||||
```php
|
|
||||||
if (!$db->CreateTable('user', "\\App\\Models\\User"))
|
|
||||||
throw new PDOException("Ошибка создания таблицы: users");
|
|
||||||
```
|
|
||||||
|
|
||||||
Таблица создана.
|
|
||||||
|
|
||||||
[Предыдущий пункт](create_table.md) | [На главную](../index.md) | [Следующий пункт](../index.md)
|
[Предыдущий пункт](create_table.md) | [На главную](../index.md) | [Следующий пункт](../index.md)
|
Loading…
x
Reference in New Issue
Block a user