Download the PHP package dedsecteam17/soft-mapper without Composer
On this page you can find all versions of the php package dedsecteam17/soft-mapper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download dedsecteam17/soft-mapper
More information about dedsecteam17/soft-mapper
Files in dedsecteam17/soft-mapper
Package soft-mapper
Short Description A lightweight, powerful, and easy-to-use PHP ORM library for MySQL databases with advanced features including relationships, soft deletes, and automatic timestamps
License MIT
Homepage https://github.com/DedSecTeam17/Soft-Mapper
Informations about the package soft-mapper
Soft-Mapper
A lightweight, powerful, and easy-to-use PHP ORM (Object-Relational Mapping) library for MySQL databases. Soft-Mapper provides a clean and intuitive interface for database operations with built-in security features and advanced ORM capabilities.
🚀 What's New in v2.0
Version 2.0 brings 27 new methods and advanced ORM features that make Soft-Mapper comparable to Laravel's Eloquent:
- ✨ Automatic Timestamps - Auto-manage created_at/updated_at
- ✨ Soft Deletes - Mark records as deleted without removing them
- ✨ ORM Relationships - Define 1:1, 1:N, and N:N relationships (NEW!)
- ✨ Eager Loading - Load relationships efficiently to prevent N+1 queries (NEW!)
- ✨ Query Scopes - Reusable query constraints
- ✨ Batch Operations - Insert multiple records efficiently
- ✨ Transactions - Full transaction support
- ✨ Advanced Queries - whereIn, whereBetween, whereNull, JOINs
- ✨ Helper Methods - count(), exists(), first(), pluck()
- ✨ And much more! - See CHANGELOG.md for complete list
📚 Documentation:
- Quick Start Guide - Get started with new features
- ORM Relationships Guide - Complete guide to relationships (NEW!)
- Changelog - Complete list of changes
- Advanced Examples - Real-world usage examples
Table of Contents
- Features
- Requirements
- Installation
- Quick Start
- Configuration
- Usage
- Creating Models
- Basic CRUD Operations
- Advanced Queries
- Aggregate Functions
- Advanced Features
- API Reference
- Examples
- Security
- Troubleshooting
- Contributing
- License
- Author
Features
Core Features
- ✅ Simple and Intuitive API: Easy-to-understand method chaining interface
- ✅ Full CRUD Support: Create, Read, Update, and Delete operations
- ✅ Query Builder: Build complex SQL queries with simple PHP methods
- ✅ Security First: Built-in prepared statements prevent SQL injection
- ✅ Aggregate Functions: Support for COUNT, SUM, AVG, MIN, MAX, etc.
- ✅ Flexible Filtering: WHERE and HAVING clauses with multiple conditions
- ✅ Result Ordering: ORDER BY with ASC/DESC support
- ✅ Result Grouping: GROUP BY for aggregate queries
- ✅ Result Limiting: LIMIT clause for pagination
- ✅ Method Chaining: Chain multiple methods for complex queries
- ✅ PDO-Based: Uses PHP Data Objects for database abstraction
- ✅ Lightweight: Minimal dependencies, single-file implementation
Advanced Features ⭐ NEW
- ✨ Automatic Timestamps: Auto-manage created_at and updated_at columns
- ✨ Soft Deletes: Mark records as deleted without removing from database
- ✨ ORM Relationships: Define and query 1:1, 1:N, and N:N relationships
- ✨ Eager Loading: Load relationships efficiently with with()
- ✨ Relationship Methods: hasOne(), hasMany(), belongsTo(), belongsToMany()
- ✨ Pivot Table Operations: attach(), detach(), sync() for many-to-many
- ✨ Pagination with OFFSET: Full pagination support with limit and offset
- ✨ Custom Primary Keys: Support for non-'id' primary key columns
- ✨ Batch Operations: Insert multiple records efficiently with insertMany()
- ✨ Query Scopes: Define reusable query constraints
- ✨ JOIN Support: INNER JOIN, LEFT JOIN, RIGHT JOIN operations
- ✨ Advanced WHERE Clauses: whereIn, whereNotIn, whereBetween, whereNull
- ✨ Helper Methods: first(), count(), exists(), pluck()
- ✨ Transaction Support: Complete transaction management
- ✨ Raw Queries: Execute custom SQL when needed
- ✨ Chunking: Memory-efficient processing of large datasets
- ✨ Update or Create: Intelligent upsert operations
- ✨ Distinct Results: Get unique records only
Requirements
- PHP: Version 5.6 or higher (PHP 7.x or 8.x recommended)
- MySQL: Version 5.5 or higher
- PHP Extensions:
- PDO
- PDO_MySQL
Installation
Using Composer (Recommended)
Install via Composer:
Then include the autoloader in your project:
Manual Installation
-
Clone this repository or download the files:
- Include the library in your project:
Using as a Git Submodule
Then include in your code:
Quick Start
Configuration
Database Configuration
Edit env.php to configure your database connection:
Environment-Specific Configuration
For production environments, consider using environment variables:
Usage
Creating Models
Each model represents a database table. Extend the SoftMapper class:
Basic CRUD Operations
Create (Insert)
Insert new records into the database:
Read (Select)
Find by ID:
Get All Records:
Get Single Record:
Select Specific Columns:
Update
Update existing records:
Update Multiple Records:
Delete
Delete records from the database:
Delete Multiple Records:
Advanced Queries
WHERE Clauses
Simple WHERE:
Multiple Conditions with AND:
Using Different Operators:
ORDER BY
Order Descending:
Order Ascending:
LIMIT
Limit Results:
Pagination Example:
Combining Multiple Clauses
Aggregate Functions
COUNT
Count all records:
Count with GROUP BY:
SUM, AVG, MIN, MAX
HAVING Clause
Use HAVING with aggregate functions:
Advanced Features
Automatic Timestamps
Enable automatic timestamp management for created_at and updated_at columns:
Soft Deletes
Soft delete marks records as deleted without removing them from the database:
Custom Primary Keys
Override the default 'id' primary key:
Pagination with OFFSET
Full pagination support:
Batch Insert
Insert multiple records efficiently:
Advanced WHERE Clauses
WHERE IN
WHERE NOT IN
WHERE BETWEEN
WHERE NULL / WHERE NOT NULL
Query Scopes
Define reusable query constraints:
JOIN Operations
Perform table joins:
Helper Methods
first()
Get the first record:
count()
Count records:
exists()
Check if records exist:
pluck()
Get a single column's values:
Transaction Support
Manage database transactions:
Raw Queries
Execute custom SQL:
Chunking
Process large datasets efficiently:
Update or Create
Update existing record or create new one:
Distinct Results
Get unique records:
Get Last Insert ID
ORM Relationships
Soft-Mapper now supports defining and querying relationships between models, making it easy to work with related data.
Defining Relationships
Define relationships in your model classes:
Loading Relationships
Lazy Loading (load relationships as needed):
Eager Loading (load relationships efficiently for multiple records):
Many-to-Many Operations
Manage many-to-many relationships with pivot table operations:
Complete Example
📚 For complete relationship documentation, see RELATIONSHIPS.md
API Reference
Core Methods
| Method | Parameters | Returns | Description |
|---|---|---|---|
all() |
None | $this |
Selects all records from table |
select() |
array $columns, string $aggregate, string $parameter |
$this |
Select specific columns with optional aggregate |
find() |
mixed $id |
object\|null |
Find record by primary key |
insert() |
None | bool |
Insert new record using $columns array |
update() |
None | $this |
Update records (use with where()) |
delete() |
bool $force |
$this |
Delete records (supports soft delete) |
where() |
array $conditions |
$this |
Add WHERE conditions |
orderBy() |
string $column, string $direction |
$this |
Order results |
groupBy() |
string $column |
$this |
Group results |
having() |
array $conditions |
$this |
Add HAVING conditions |
limit() |
int $number |
$this |
Limit number of results |
offset() |
int $number |
$this |
Offset for pagination |
getAll() |
None | array |
Execute query and fetch all results |
get() |
None | object\|null |
Execute query and fetch single result |
execute() |
None | bool |
Execute query (for UPDATE/DELETE) |
Advanced Methods
| Method | Parameters | Returns | Description |
|---|---|---|---|
withTrashed() |
None | $this |
Include soft deleted records |
onlyTrashed() |
None | $this |
Get only soft deleted records |
restore() |
None | $this |
Restore soft deleted records |
insertMany() |
array $records |
bool |
Insert multiple records at once |
whereIn() |
string $column, array $values |
$this |
WHERE IN clause |
whereNotIn() |
string $column, array $values |
$this |
WHERE NOT IN clause |
whereBetween() |
string $column, mixed $start, mixed $end |
$this |
WHERE BETWEEN clause |
whereNull() |
string $column |
$this |
WHERE NULL clause |
whereNotNull() |
string $column |
$this |
WHERE NOT NULL clause |
first() |
None | object\|null |
Get the first record |
count() |
None | int |
Count records |
exists() |
None | bool |
Check if records exist |
pluck() |
string $column |
array |
Get single column values |
scope() |
string $name, callable $callback |
void |
Define a query scope |
applyScope() |
string $name, ...$args |
$this |
Apply a query scope |
beginTransaction() |
None | bool |
Start a database transaction |
commit() |
None | bool |
Commit a database transaction |
rollback() |
None | bool |
Rollback a database transaction |
raw() |
string $query, array $params |
mixed |
Execute raw SQL query |
lastInsertId() |
None | string |
Get last inserted ID |
chunk() |
int $size, callable $callback |
void |
Process results in chunks |
join() |
string $table, string $first, string $op, string $second, string $type |
$this |
Join tables |
leftJoin() |
string $table, string $first, string $op, string $second |
$this |
LEFT JOIN |
rightJoin() |
string $table, string $first, string $op, string $second |
$this |
RIGHT JOIN |
distinct() |
None | $this |
Get distinct records |
updateOrCreate() |
array $attributes, array $values |
bool |
Update existing or create new record |
Relationship Methods
| Method | Parameters | Returns | Description |
|---|---|---|---|
hasOne() |
string $class, string $foreign_key, string $local_key |
$this |
Define one-to-one relationship |
hasMany() |
string $class, string $foreign_key, string $local_key |
$this |
Define one-to-many relationship |
belongsTo() |
string $class, string $foreign_key, string $owner_key |
$this |
Define inverse relationship |
belongsToMany() |
string $class, string $pivot, string $foreign_key, string $related_key |
$this |
Define many-to-many relationship |
loadRelation() |
string $name, object $record |
object |
Load a relationship for a record |
with() |
array $relations |
$this |
Eager load relationships |
attach() |
mixed $id, mixed $related_id, string $relation, array $pivot_data |
bool |
Attach in many-to-many |
detach() |
mixed $id, string $relation, mixed $related_id |
bool |
Detach in many-to-many |
sync() |
mixed $id, array $related_ids, string $relation |
bool |
Sync many-to-many relationships |
Condition Array Format
For where() and having() methods:
Supported Operators: =, >, <, >=, <=, !=, <>
Logical Operators: AND, OR
Examples
See the example.php file for complete working examples. Here are some real-world scenarios:
Blog Post Management
User Management
Security
SQL Injection Protection
Soft-Mapper uses PDO prepared statements for all database queries, which automatically protects against SQL injection attacks. User input is never directly concatenated into SQL queries.
Best Practices
- Never expose database credentials: Keep
env.phpoutside your web root or use environment variables - Validate user input: Even though SQL injection is prevented, validate data types and formats
- Hash passwords: Always use
password_hash()for storing passwords - Sanitize output: Use
htmlspecialchars()when displaying data in HTML - Use HTTPS: Encrypt data in transit
- Implement access controls: Check user permissions before database operations
Example with validation:
Troubleshooting
Common Issues
1. PDO Connection Error
Solution: Check your database configuration in env.php. Ensure MySQL is running.
2. Table doesn't exist
Solution: Verify that $table_name in your model matches your actual database table name.
3. Column not found
Solution: Ensure column names in $columns array match your database schema.
4. Class 'PDO' not found
Solution: Enable the PDO and PDO_MySQL extensions in your php.ini:
Debug Mode
To see the generated SQL queries, you can temporarily add debugging to the SoftMapper class:
Getting Help
If you encounter issues:
- Check the examples section
- Review your database configuration
- Verify your table and column names
- Check PHP and MySQL versions meet requirements
- Open an issue on GitHub
Contributing
Contributions are welcome! Here's how you can help:
- Fork the repository
- Create a feature branch:
git checkout -b feature/amazing-feature - Make your changes: Implement your feature or bug fix
- Test your changes: Ensure everything works as expected
- Commit your changes:
git commit -m 'Add amazing feature' - Push to the branch:
git push origin feature/amazing-feature - Open a Pull Request
Development Guidelines
- Follow PSR coding standards
- Add comments for complex logic
- Update documentation for new features
- Test with multiple PHP versions if possible
- Keep backward compatibility in mind
License
This project is open source and available under the MIT License. Feel free to use, modify, and distribute this software.
Author
Mohammed Elamin
- Created: December 2018
- GitHub: @DedSecTeam17
Star this Repository ⭐
If you find Soft-Mapper useful, please consider giving it a star on GitHub. It helps others discover the project!
Support
For questions, issues, or feature requests, please visit:
Happy Coding! 🚀
All versions of soft-mapper with dependencies
ext-pdo Version *
ext-pdo_mysql Version *