DevCraft Документации
СправочникBack-EndClasses

QueryBuilder v200.4.0

Fluent-аргументы для DataLoaderService (SELECT / INSERT / UPDATE / DELETE) на AbstractWith + #[With]/#[WithItem].

Зачем: собрать table / columns / conditions / values / order / limit / offset без сырого массива.

Namespace: DevCraft\Builders

Путь: devcraft/src/classes/Builders/QueryBuilder.php

См. также: DataLoader, DataLoaderService, SortDirection, With attributes

Свойства (#[With] / #[WithItem])

СвойствоАтрибутыМетоды
$table#[With]withTable
$columns#[With, WithItem('string')]withColumns, withColumnsItem
$conditions#[With, WithItem('string', 'mixed')]withConditions, withConditionsItem
$values#[With, WithItem('string', 'mixed')]withValues, withValuesItem
$primaryKey#[With] (по умолчанию id)withPrimaryKey
$order#[With, WithItem('string', [SortDirection::class, 'string'])]withOrder, withOrderItem
$limit / $offset#[With]withLimit, withOffset

Ручные методы

МетодЗачем
create(string $table)Старт цепочки
build()Массив для loadData (order → `'ASC'
load()Все строки через DataLoaderService::loadData
first()Первая строка или []
insert()INSERT по values → созданная строка ([] при ошибке)
update()UPDATE по conditions → все совпавшие строки
delete()DELETE по conditionsbool (без conditions — false)

Пример SELECT

use DevCraft\Builders\QueryBuilder;
use DevCraft\Core\Enums\SortDirection;

$user = QueryBuilder::create('users')
	->withColumnsItem('user_id')
	->withColumnsItem('name')
	->withConditionsItem('user_id', $userId)
	->withOrderItem('name', SortDirection::Asc)
	->withLimit(1)
	->first();

Пример INSERT / UPDATE / DELETE

use Cycle\Database\Injection\Fragment;
use DevCraft\Builders\QueryBuilder;

$row = QueryBuilder::create('pm')
	->withValues([
		'subj' => 'Тема',
		'text' => 'Текст',
		'user' => $userId,
		'folder' => 'inbox',
	])
	->insert();

QueryBuilder::create('users')
	->withValues([
		'pm_all'    => new Fragment('pm_all+1'),
		'pm_unread' => new Fragment('pm_unread+1'),
	])
	->withConditionsItem('user_id', $userId)
	->update();

$ok = QueryBuilder::create('tags')
	->withConditionsItem('news_id', $newsId)
	->withConditionsItem('tag', $tag)
	->delete();

На этой странице