PHP Classes

Crudx: Store and retrieve objects in MySQL using MySQLi

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 403 All time: 6,547 This week: 455Up
Version License PHP version Categories
crudx 1.0.3MIT/X Consortium ...5.3PHP 5, Databases, Design Patterns
Description 

Author

This class can store and retrieve objects in MySQL using MySQLi.

It can create objects associated to a given table that will retrieve its record fields to a given database. It also supports conditions and joins with other tables.

The class can also insert, update and delete records from the current object variables.

Variables may be retrieved or set using getters and setters.

Picture of Nahid Bin Azhar
  Performance   Level  
Name: Nahid Bin Azhar <contact>
Classes: 24 packages by
Country: United States United States
Age: 33
All time rank: 792109 in United States United States
Week rank: 44 Up6 in United States United States Up
Innovation award
Innovation award
Nominee: 6x

Winner: 2x

Recommendations

What is the best PHP crud class?
How to create CRUD with PHP

Example

<?php
/*
*@Author: Nahid Bin Azhar
*@Author URL: http://nahid.co
*/

require_once 'src/Crudx.php';

use
Nahid\Crudx\Crudx;

//use Nahid\Crudx\Crudx;
$config = [
   
'host' => 'localhost',
   
'user' => 'root',
   
'password' => 'bolbona',
   
'database' => 'crudx_db',
   
'charset' => 'utf8',
   
'collation' => 'utf8_unicode_ci',
   
'prefix' => 'tbl_',
];

$db = new Crudx($config);

//Crudx::table('user')->where('id', '=', 5)->get()->result();

/*
$user = $db->table('userget()
$user->name = 'Arufur Naim';
$user->username = 'naim';
$user->created_at = date('Y-m-d');

if($user->save()) {
    echo 'Success';
}*/

echo '<pre>';
$query = $db->table('users')->where('username', '=', 'nahid')->first(['name'])->result();
var_dump($db->getQueryString());


Details

crudx

CRUDX is a PHP based library for mysql query builder. Its easy and simple.

How to install

To install this package go to terminal and run this command

composer require nahid/crudx

Usage

To use this package you have to include it. Include it from composer autoload

require_once 'vendor/autoload.php';

How to connect

You can connect by using this process;

use Nahid\Crudx\Crudx;

$config = [
    'host' 		=> 'localhost',
    'user' 		=> 'root',
    'password' 	=> 'your_password',
    'database' 	=> 'database_name',
    'charset' 	=> 'utf8',
    'collation'	=> 'utf8_unicode_ci',
    'prefix' 	=> 'table_prefix',
];

$crud = new Crudx($config);

Insert

Its too much important to inserting data in database table when you developing an application. Crudx make easy to inserting data in table. Crudx provide different type of insertion mechanism. First you can insert data with Laravel Eloquent style. Suppose you want to save name, username, email in users table. So what can you do?

$user=$crud->table('users');

$user->name='Nahid Bin Azhar';
$user->username='nahid';
$user->email='talk@nahid.co';

$user->save();

Its like a piece of cake.

The second procedure. Its traditional way. You may called it CodeIgniter Style. However see how can you inserting data with this way.

$data=[
	'name'=>'Nahid Bin Azhar',
	'username'=>'nahid',
	'email'=>'talk@nahid.co'
];

$crud->table('users')->save($data);

What do you think? yes its Crudx, make easy development.

You may insert multiple records at once. Yes its true, just see what happend

$data=[
	[
	'name'=>'Nahid Bin Azhar',
	'username'=>'nahid',
	'email'=>'talk@nahid.co'
	],
	[
	'name'=>'Naim',
	'username'=>'naim',
	'email'=>'naim@themebucket.net'
	]
];

$crud->table('users')->insertMany($data);

Update

Making update rocord is so easy.

$data=[
	'name'=>'Nahid Bin Azhar',
	'username'=>'nahid',
	'email'=>'talk@nahid.co'
];

$crud->where('id', '=', 1)->save($data);

Delete

Sometimes you need to delete record from table. Crudx make it easy. Its just a single command

$crud->where('id', '=', 1)->delete();

Fetching Record

Crudx provide you to differents type service to fetching record from table. Suppose you want to get all data of author role from table users

$crud->table('users')->where('role', '=', 'author')->all()->result();

//generated SQL String: SELECT * FROM users WHERE role='author'

But if you want to add multiple condition with AND operator then follow the process

$crud->table('users')->where('role', '=', 'author')->where('age','>',17)->all()->result();

//generated SQL String: SELECT * FROM users WHERE role='author' AND age>17

you can use orWhere() for OR operator and you can also use whereBetween() `orBetween()

If you have to fetch specific table column then use get() method and pass an array to specify table column

$crud->table('users')->where('role', '=', 'author')->get(['name', 'username'])->result();

//generated SQL String: SELECT name, username FROM users WHERE role='author'

Join

Crudx provide you a simple joining method.

$crud->table('posts')->join('users', 'posts.user_id','=', 'users.id')->get(['post', 'name'])->result();

//generated SQL String: SELECT post, name FROM posts INNER JOIN users on posts.user_id=users.id

Get last inserted id

$crud->table('users')->save(['name'=>'Nahid']);
echo $crud->getId();

Get last generated query string

Sometimes you have to need which query string is generated by the last method. Crudx make it easy

$crud->table('users')->where('role', '=', 'author')->where('age','>',17)->all()->result();
echo $crud->getQueryString();

  Files folder image Files (6)  
File Role Description
Files folder imagesrc (1 file, 1 directory)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file index.php Example Example script
Accessible without login Plain text file LICENSE Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (6)  /  src  
File Role Description
Files folder imageFacade (1 file)
  Plain text file Crudx.php Class Class source

  Files folder image Files (6)  /  src  /  Facade  
File Role Description
  Plain text file crudx.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:403
This week:0
All time:6,547
This week:455Up