PHP Classes

File: src/Tools/ModuleGeneratorEnhanced/docs/CRUD-IMPLEMENTATION.md

Recommend this page to a friend!
  Packages of Adrian M   upMVC   src/Tools/ModuleGeneratorEnhanced/docs/CRUD-IMPLEMENTATION.md   Download  
File: src/Tools/ModuleGeneratorEnhanced/docs/CRUD-IMPLEMENTATION.md
Role: Auxiliary data
Content type: text/markdown
Description: Auxiliary data
Class: upMVC
Pure PHP web development without other frameworks
Author: By
Last change:
Date: 3 months ago
Size: 13,741 bytes
 

Contents

Class file image Download

Full CRUD & Dashboard Implementation

Date: 2024-11-15

Version: PSR-4 Compliant with Database Integration

Overview

The ModuleGeneratorEnhanced now provides complete, production-ready module types with: - ? Automatic database table creation - ? Sample data generation (5 intelligent records per table) - ? Dynamic demo data based on configured fields - ? Graceful database fallback (works with or without DB) - ? Full CRUD operations (Create, Read, Update, Delete) - ? Field-aware forms (dynamic based on user-defined fields)

Module Types - Feature Matrix

| Feature | Basic | CRUD | API | Auth | Dashboard | Submodule | |---------|-------|------|-----|------|-----------|-----------| | Auto-Discovery | ? | ? | ? | ? | ? | ? | | Database Table | ? | ? Auto | ? | ? | ? Optional | ? | | Sample Data | ? | ? 5 rows | ? | ? | ? If DB | ? | | Demo Fallback | ? | ? Dynamic | ? | ? | ? Dynamic | ? | | Field Definition | ? | ? Required | ? | ? | ? Optional | ? | | Forms | ? | ? Dynamic | ? | ? | ? | ? | | List View | ? | ? With Actions | ? | ? | ? Stats | ? | | Flash Messages | ? | ? | ? | ? | ? | ? | | Status | ? Complete | ? Complete | ?? Stub | ?? Stub | ? Complete | ? Complete |

CRUD Module - Complete Implementation

What You Get

When you generate a CRUD module with fields like:

$config = [
    'name' => 'Product',
    'type' => 'crud',
    'fields' => [
        ['name' => 'name', 'sql_type' => 'VARCHAR(255)', 'html_type' => 'text'],
        ['name' => 'description', 'sql_type' => 'TEXT', 'html_type' => 'textarea'],
        ['name' => 'price', 'sql_type' => 'DECIMAL(10,2)', 'html_type' => 'number'],
        ['name' => 'status', 'sql_type' => 'ENUM("active","inactive")', 'html_type' => 'select'],
    ],
    'create_table' => true
];

Generated Files

src/Modules/Product/
??? Controller.php        # Full CRUD methods
??? Model.php             # Database operations + demo fallback
??? View.php              # Flash messages support
??? Routes/
?   ??? Routes.php       # All CRUD routes registered
??? views/
?   ??? index.php        # List with edit/delete buttons
?   ??? form.php         # Dynamic form (create/edit)
?   ??? layouts/
?       ??? header.php
?       ??? footer.php
??? assets/
?   ??? css/style.css
?   ??? js/script.js
??? etc/
    ??? config.php
    ??? api-docs.md

Controller Methods

class Controller extends BaseController
{
    // Action dispatcher - handles ?action=create, ?action=edit, etc.
    public function display($reqRoute, $reqMet): void
    
    // Form displays
    public function create($reqRoute, $reqMet): void  // Show create form
    public function edit($reqRoute, $reqMet): void    // Show edit form
    
    // Database operations
    public function store($reqRoute, $reqMet): void   // CREATE
    public function update($reqRoute, $reqMet): void  // UPDATE
    public function delete($reqRoute, $reqMet): void  // DELETE
    
    // Helper
    private function getPostData(): array             // Extract field data from POST
}

Model Methods

class Model extends BaseModel
{
    // Database operations
    public function getAll(): array           // Read all records
    public function getById(int $id): ?array  // Read single record
    public function create(array $data): bool // Insert new record
    public function update(int $id, array $data): bool
    public function delete(int $id): bool
    
    // Graceful fallback
    private function checkConnection(): bool  // Check if DB available
    private function getDemoData(): array     // Dynamic demo data based on fields
}

Routes Available

GET  /products              ? List all products
GET  /products?action=create ? Show create form
POST /products?action=store  ? Save new product
GET  /products?action=edit&id=1 ? Show edit form
POST /products?action=update ? Update product
GET  /products?action=delete&id=1 ? Delete product

Dynamic Form Generation

The views/form.php template automatically adapts to your fields:

Text Input:

<input type="text" name="name" value="..." required>

Textarea:

<textarea name="description" required>...</textarea>

Number Input:

<input type="number" name="price" value="..." required>

Select Dropdown:

<select name="status" required>
    <option value="">Select...</option>
    <option value="active">Active</option>
    <option value="inactive">Inactive</option>
</select>

Database Integration

Automatic Table Creation

When create_table is enabled:

  1. Connects to database using `.env` credentials:
    DB_HOST=127.0.0.1
    DB_NAME=upmvc
    DB_USER=root
    DB_PASS=
    
  2. Generates SQL with all fields:
    CREATE TABLE IF NOT EXISTS `products` (
      id INT AUTO_INCREMENT PRIMARY KEY,
      `name` VARCHAR(255) NOT NULL,
      `description` TEXT NOT NULL,
      `price` DECIMAL(10,2) NOT NULL,
      `status` ENUM("active","inactive") NOT NULL,
      created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
      updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
    
  3. Inserts 5 sample records with intelligent data: - Names/Titles: "Sample Product 1", "Sample Product 2", etc. - Descriptions: Contextual messages - Prices: Random realistic values (10.00-999.99) - Status: Mix of active/inactive - Dates: Varied timestamps

Sample Data Intelligence

The generator creates contextually appropriate sample data:

// For field: 'name' VARCHAR(255)
"Sample Product 1", "Sample Product 2", ...

// For field: 'description' TEXT
"This is a sample description for Product item 1. You can edit or delete this record."

// For field: 'price' DECIMAL(10,2)
"49.99", "129.50", "299.00", ...

// For field: 'email' VARCHAR(255)
"sample1@example.com", "sample2@example.com", ...

// For field: 'status' ENUM('active','inactive')
"active", "active", "inactive", ...

Graceful Fallback

If database is NOT available:

  1. ? Module still works
  2. ? Shows dynamic demo data based on configured fields
  3. ? Displays warning: "Demo mode: Database not connected"
  4. ? Forms work but don't persist
  5. ? Lists show 3 sample records

Demo Data Example (no database):

[
    ['id' => 1, 'name' => 'Demo name 1', 'description' => '...', 'price' => '45.32', 'status' => 'active'],
    ['id' => 2, 'name' => 'Demo name 2', 'description' => '...', 'price' => '128.99', 'status' => 'active'],
    ['id' => 3, 'name' => 'Demo name 3', 'description' => '...', 'price' => '89.50', 'status' => 'inactive']
]

Dashboard Module - Complete Implementation

What You Get

Dashboard modules now support field tracking:

$config = [
    'name' => 'Analytics',
    'type' => 'dashboard',
    'fields' => [  // Optional - defaults provided
        ['name' => 'metric_name', 'sql_type' => 'VARCHAR(255)', 'html_type' => 'text'],
        ['name' => 'value', 'sql_type' => 'INT', 'html_type' => 'number'],
    ]
];

Dashboard Features

Statistics Cards: - Total Items - Active Items - Recent Activity - Pending Items

Recent Items Table: - Lists last 5 items - Shows ID, Title, Status, Date - Action buttons

Demo Mode: - Works without database - Shows realistic stats (127 total, 98 active, 23 recent, 6 pending) - Displays 5 sample dashboard items

Dashboard View

<!-- 4 Statistics Cards -->
<div class="row">
    <div class="col-md-3">
        <div class="card bg-primary">
            <h2>127</h2> <!-- Total Items -->
        </div>
    </div>
    <!-- ... 3 more cards ... -->
</div>

<!-- Recent Items Table -->
<table class="table">
    <thead>
        <tr><th>ID</th><th>Title</th><th>Status</th><th>Date</th></tr>
    </thead>
    <!-- Dynamic rows from database or demo data -->
</table>

CLI Usage

CRUD Module

php generate-module.php

# Prompts:
? Enter module name: Product
? Select module type: crud

# Field definition:
? Define fields for your CRUD module:
? Field (name:sql_type:html_type): name:VARCHAR(255):text
   ? Added field: name (VARCHAR(255)) -> text
? Field (name:sql_type:html_type): description:TEXT:textarea
   ? Added field: description (TEXT) -> textarea
? Field (name:sql_type:html_type): price:DECIMAL(10,2):number
   ? Added field: price (DECIMAL(10,2)) -> number
? Field (name:sql_type:html_type): [Enter to finish]

??  Create database table automatically? (y/n): y
? Enable middleware integration? (y/n): n

# Result:
? Database table 'products' created successfully
? Inserted 5 sample records for testing

Dashboard Module

php generate-module.php

# Prompts:
? Enter module name: Analytics
? Select module type: dashboard

? Dashboard modules can track specific data types.
Would you like to define fields for dashboard tracking? (y/n): n
# (Uses default fields: title, status)

? Enable middleware integration? (y/n): y

# Result:
? Dashboard module created with demo data support

Testing

Test Without Database

Modules work immediately - demo data is shown:

# Browser
http://localhost/upMVC/public/products

# Shows:
- 3 demo products
- "Demo mode: Database not connected" warning
- Fully functional interface

Test With Database

Configure .env and the generator creates everything:

DB_HOST=127.0.0.1
DB_NAME=upmvc
DB_USER=root
DB_PASS=yourpassword

php generate-module.php
# ... answer prompts with create_table=yes

# Result:
? Database table 'products' created successfully
? Inserted 5 sample records for testing

Programmatic Usage

Quick CRUD

use App\Tools\ModuleGeneratorEnhanced\ModuleGeneratorEnhanced;

$config = [
    'name' => 'Product',
    'type' => 'crud',
    'fields' => [
        ['name' => 'name', 'sql_type' => 'VARCHAR(255)', 'html_type' => 'text'],
        ['name' => 'price', 'sql_type' => 'DECIMAL(10,2)', 'html_type' => 'number'],
    ],
    'create_table' => true
];

$generator = new ModuleGeneratorEnhanced($config);
$generator->generate();

Quick Dashboard

$config = [
    'name' => 'Analytics',
    'type' => 'dashboard',
    'use_middleware' => true
];

$generator = new ModuleGeneratorEnhanced($config);
$generator->generate();

Best Practices

Field Naming

Use descriptive names that match your data: - ? product_name, customer_email, order_total - ? field1, data, temp

SQL Types

Choose appropriate types: - Text: VARCHAR(255) or TEXT - Numbers: INT, DECIMAL(10,2), FLOAT - Dates: DATE, DATETIME, TIMESTAMP - Options: ENUM('value1','value2') - Boolean: TINYINT(1) or BOOLEAN

HTML Input Types

Match SQL type with appropriate input: - VARCHAR ? text - TEXT ? textarea - INT/DECIMAL ? number - DATE ? date - ENUM ? select - VARCHAR (email) ? email

Sample Data

The generator creates 5 sample records automatically. This is ideal for: - Testing forms - Verifying list views - Demonstrating features - Development workflow

Troubleshooting

Database Connection Failed

Problem: SQLSTATE[HY000] [1049] Unknown database 'upmvc'

Solution: 1. Create database: CREATE DATABASE upmvc; 2. Or update .env with existing database name 3. Or use without database (demo mode works)

Demo Data Not Showing

Problem: Empty lists even in demo mode

Solution: Check Model's getDemoData() method has configuredFields defined

Form Not Saving

Problem: Form submits but nothing happens

Solution: 1. Check database connection 2. Look for flash messages (warnings shown in demo mode) 3. Verify create_table was enabled during generation

Changelog

2024-11-15 - Full Implementation

Added: - ? Automatic database table creation - ? Intelligent sample data generation (5 records) - ? Dynamic demo data based on configured fields - ? Dashboard field support - ? Graceful database fallback - ? Flash message system - ? Action-based routing for CRUD - ? Dynamic form generation

Fixed: - ? Dashboard template not found error - ? Database connection null errors - ? Environment::current() bugs - ? All module types now PSR-4 compliant

Enhanced: - ? CRUD Model with connection checking - ? Demo data intelligence (field-aware) - ? Sample data with realistic values - ? CLI asks for dashboard fields

Future Enhancements

Planned Features

  1. API Module - Full RESTful implementation
  2. Auth Module - Complete authentication system
  3. Validation - Field-level validation rules
  4. Relationships - Foreign keys and joins
  5. Pagination - Large dataset handling
  6. Search/Filter - Advanced querying
  7. Image Upload - File handling fields
  8. Soft Delete - Trash/restore functionality

Conclusion

Status: ? PRODUCTION READY

The CRUD and Dashboard module types are now fully implemented with: - Complete database integration - Intelligent sample data - Graceful fallbacks - Dynamic form generation - Field-aware demo data

Generate a module in under 30 seconds that works with or without a database!