There is no license information available for the latest version (v1.11.0) of this package.

CRUD Library for CI4

Installs: 76

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 1

Forks: 0

Open Issues: 0

pkg:composer/tomkirsch/crud

v1.11.0 2024-10-17 21:33 UTC

README

A focused set of base classes and helpers for building structured CRUD applications in CodeIgniter 4.
Designed to reduce repetition in models, entities, joins, form handling, and timezone-aware date workflows.

Contents

  • BaseModel.php
  • BaseEntity.php
  • date_helper.php
  • form_helper.php

BaseModel

Extends CodeIgniter\Model and adds higher-level utilities for relational CRUD workflows.

Key Features

  • Automatic prefixing of primary/created/updated/deleted fields
  • Option to derive $allowedFields from $validationRules
  • columns() helper based on validation rules
  • nextAutoIncrement() (MySQL information_schema lookup)
  • Smart save() override (prevents empty PK from forcing update)
  • syncOneToMany() for maintaining lookup tables
  • Join utilities with automatic unique field aliasing
  • Subquery helpers for selecting values from latest related rows

Join Workflow

joinModel() registers fields with unique aliases.
selectUnique() builds a collision-safe SELECT clause.

Example:

$rows = $userModel
    ->reset()
    ->joinModel('ProfileModel', 'profiles.user_id = users.id', 'left')
    ->selectUnique()
    ->findAll();

Supports aliasing and prefix mapping for nested joins.

Sync Lookup Tables

$model->syncOneToMany(
    'foos2bars',
    ['foo_id' => 1],
    'bar_id',
    [12, 13],
    [5, 13, 14]
);

Handles insert + delete diff automatically.

BaseEntity

Extends CodeIgniter\Entity\Entity and improves form and join handling.

Key Features

  • fillNullEmpty() --- converts empty strings to NULL before fill
  • Timezone helpers:
    • applyLocalTimezone()
    • applyServerTimezone()
  • csvMap() --- converts GROUP_CONCAT CSV fields into structured objects/entities
  • prefixMap() --- extracts prefixed join fields into sub-entities
  • stripAliasPrefix() --- removes select alias prefixes
  • getHtmlData() --- exports entity attributes as HTML data-* attributes

Example:

$image = $row->prefixMap('image_', \App\Entities\Image::class, true);

date_helper

Helpers for timezone-aware date handling using CodeIgniter\I18n\Time.

Functions:

  • localDate($dateTime, $format = null, $timezone = null)
  • serverDate($dateTime, $format = null, $timezone = null)

Supports returning either a Time object or formatted string.

Requires:

public string $localTimezone;
public string $appTimezone;

in Config/App.php.

form_helper

Improves CI form value binding for objects/entities.

Handles:

  • NULL vs empty
  • Boolean fields
  • DateTime / Time objects
  • Bracketed input names (user[email])
  • Optional local timezone conversion

Key functions:

  • set_value_from()
  • set_checkbox_from()
  • set_select_from()
  • set_radio_from()
  • set_textarea_from()

Example:

echo form_input('email', set_value_from('email', $user));

With date formatting:

echo form_input('starts_at', set_value_from('starts_at', $entity, '', true, [
    'localTimezone' => true,
    'format' => 'Y-m-d H:i:s',
]));

Installation

Drop the classes into an autoloaded namespace and register via PSR-4 in Config/Autoload.php.

Place helpers in app/Helpers/ and load with:

helper(['date', 'form']);

Notes

  • Call reset() when using the unique join workflow before building a new query.
  • Ensure syncOneToMany() receives specific WHERE conditions in $common_data.