Download the PHP package jpgerber/chaos-crud without Composer
On this page you can find all versions of the php package jpgerber/chaos-crud. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package chaos-crud
<!DOCTYPE html>
Chaos CRUD Readme
Create - Read - Update - Delete
Instantiation
This may vary a little depending on how you autoload your classes (if at all). Asuming you use Composer and have the proper namespaces autoloaded, you can instantiate by doing the following:
$db = new Chaos\Network\CRUD();
Alternatively, if you are using my classDeclarations.php file, you do not
need to instantiate it; however, it will only run one instance of the
class when using db()->
It should be noted that connection info can be grabbed 1 of 3 ways:
- During Instantiation: db('localhost','db_name','db_username','db_password')
- Using ENV: $_ENV['DB_HOST'], $_ENV['DB_NAME'], $_ENV['DB_USER'], $_ENV['DB_PASS']
- Using SESSIONs: $_SESSION['DB_HOST'] (same as ENV keys)
Note
For this next section, I'm going to assume you are using my classDeclarations.php method. If you are not, then just replace db() with whatever your instantiated class is
db()->table()
vs
$db = new Chaos\Network\CRUD();
$db->table();
Method Chaining
The Class is built with method chaining in mind in order to make a smoother process.
Example:
db()->table('users')
->create(['username'=>'jdoe',
'password'=>'test'])
->query();
Alterative Example without Method Chaining
db()->table('users');
db()->create(['username'=>'jdoe',
'password'=>'test']);
db()->query();
Both function the same.
Methods
Let's break these down based on the usage in each part of the CRUD
All Methods
- table('users') // This line is required prior to ->query() being invoked EVERY TIME .
- query(); // This is the trigger for all the methods. It MUST be the last piece of the chain, or after all non-chained methods are completed.
Create
-
getLastID(bool) // This let's the query know if you want returned the
last row of inserted data's primary key id. Be sure that if you are
chaining, you start with a variable assignment for the lastID to be
placed into.
- $lastID = db()->table('users')->getLastID(true)->insert(['username','bob'])->query();
- create(['column' => 'value']) // You can list as many as you need, but they should always be in an associatiave array format of which column and the value to be assigned to that column.
Create is one of the simplier ones. There's no WHERE, LIMIT, etc.
Read
Used to SELECT data from the table
- addColumn() // This is where you can add the columns you want to filter by. You can use a string or array of strings (not an associated array) such as: (['username','password']). If you do not want to filter it, leave it empty. The system will default it to *
- addCondition($column, $value, $operand) // This needs 3 pieces for the WHERE statement, the column you're filtering by, the value(s) you want to include, and the operand you'll be using. ('username', 'jdoe', '=') || ('username', 'jd%', 'LIKE') - Notice the % on the one with 'LIKE' - You need to include the wildcard symbol `%` when necessary. Also, '=' is the default, so if only 2 properties are sent, it will assume '='
- addOrder($string) // Accepts a single string argument, but you can use it multiple times if you want multiple ORDER BY.
- addDir('DESC') // it assumes 'ASC', but if you prefer descending order, you can set it here.
- addLimit(2) // Limits the number of returned results to the integer provided. Must be greater than 0 and an integer (not float, string, or other type).
- read() // Notice the change from create to read - this is how the class knows which type of query you want to do.
Update
- addCondition()
- setSafe(bool) // This is a protection method to help prevent you from accidently updating EVERY row. It defaults to true (limiting your amount of changes to 1 row unless limit() is declared)
- limit()
- update(['column'=>'new value']) // As with create, you can put as many rows of an associative array as you need in here. One for each column.
- update
Delete
- addCondition()
- setSafe()
- addLimit()
- delete()
Empty
The only thing you need for emptying a table and resetting the auto-increment to 1 is the table name:
// db()->table('tableName')->empty()->query();
Notes
- setSafe(bool) - If you have an environmental value for $_ENV['SAFETY_LIMIT'], and no limit set with addLimit(), it will default the limit to your SAFETY_LIMIT value. Otherwise, it defaults to 1.
- addLimit(int)
- $_ENV['SAFETY_LIMIT']
- 1