PHPackages                             mattsmithdev/pdo-crud-for-free - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Database &amp; ORM](/categories/database)
4. /
5. mattsmithdev/pdo-crud-for-free

ActiveLibrary[Database &amp; ORM](/categories/database)

mattsmithdev/pdo-crud-for-free
==============================

make it really simply to do db crud with pdo

1.0(9y ago)17081MITPHPPHP ~5.5|~7.0

Since Jul 5Pushed 9y ago1 watchersCompare

[ Source](https://github.com/dr-matt-smith/pdo-crud-for-free)[ Packagist](https://packagist.org/packages/mattsmithdev/pdo-crud-for-free)[ Docs](https://github.com/dr-matt-smith/pdo-crud-for-free)[ RSS](/packages/mattsmithdev-pdo-crud-for-free/feed)WikiDiscussions master Synced today

READMEChangelogDependencies (4)Versions (2)Used By (0)

pdo-crud-for-free
=================

[](#pdo-crud-for-free)

[![Build Status](https://camo.githubusercontent.com/0c10b7c9f047c3016b6030f1549f851839a8d09dd3fc14dfdb1ef8e919eb4d55/68747470733a2f2f7472617669732d63692e6f72672f64722d6d6174742d736d6974682f70646f2d637275642d666f722d667265652e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/dr-matt-smith/pdo-crud-for-free) [![Latest Stable Version](https://camo.githubusercontent.com/40f2a8c51863c20e305fa7954b9adca46826f114c4a9c1bbabcef4a2735ca5b1/68747470733a2f2f706f7365722e707567782e6f72672f6d617474736d6974686465762f70646f2d637275642d666f722d667265652f762f737461626c65)](https://packagist.org/packages/mattsmithdev/pdo-crud-for-free) [![Total Downloads](https://camo.githubusercontent.com/d3bf7e054c73a1a2f6e8193ea4eaba2fb62b27829840e66967a18e2694319c8a/68747470733a2f2f706f7365722e707567782e6f72672f6d617474736d6974686465762f70646f2d637275642d666f722d667265652f646f776e6c6f616473)](https://packagist.org/packages/mattsmithdev/pdo-crud-for-free) [![Latest Unstable Version](https://camo.githubusercontent.com/e4470bc8ddc0768b818b5cb43e4b9def48a61440dcf389f7bafcdaf2b947fbd1/68747470733a2f2f706f7365722e707567782e6f72672f6d617474736d6974686465762f70646f2d637275642d666f722d667265652f762f756e737461626c65)](https://packagist.org/packages/mattsmithdev/pdo-crud-for-free) [![License](https://camo.githubusercontent.com/6b19f8ef2b4f44a5796c06c1f4490df4af31f9ff04d5ac15cbb29e8363020f93/68747470733a2f2f706f7365722e707567782e6f72672f6d617474736d6974686465762f70646f2d637275642d666f722d667265652f6c6963656e7365)](https://packagist.org/packages/mattsmithdev/pdo-crud-for-free)

This package provides a few classes to try to give programmers using PDO (with MySQL) in a simple way some instance CRUD (create-read-update-delete) method, 'for free', simply by subclassing **\\Mattsmithdev\\PdoCrud\\DatabaseTable**.

All code is (intended :-) to follow PSR-1, PSR-2 coding standards. Classes are following the PSR-4 autoloading standard.

Install
-------

[](#install)

Via Composer

```
$ composer require mattsmithdev/pdo-crud-for-free
```

NOTE - until I get the hang of SemVer (and get to a 1.0.0 or later version), then you'll need to force the dev-master download as follows:

```
$ composer require mattsmithdev/pdo-crud-for-free=dev-master
```

Usage
-----

[](#usage)

This example assumes you have a MySQL DB table named 'products', with columns 'id' and 'description'. You need to write a corresponding class 'Product' (note capital first letter ...).

```
// file: /src/Product.php
namespace ;

class Product extends \Mattsmithdev\PdoCrud\DatabaseTable
{
    // private properties with EXACTLY same names as DB table columns
    private $id;
    private $description;

    public function getDescription()
    {
        return $this->description;
    }
}
```

```
// file: /public-web/index.php or /src/SomeController->method()

require_once __DIR__ . '/';

// the DatabaseManager class needs the following 4 constants to be defined in order to create the DB connection
define('DB_HOST', '');
define('DB_USER', '');
define('DB_PASS', '');
define('DB_NAME', '');

// get all products from DB as array of Product objects
$products = \\Product::getAll();

// outputs something like:
//  hammer, nail, nuts, bolts
foreach ($products as $product){
    print $product->getDescription() . ', ';
}
```

For more details see below. Also there is a full sample web application project on GitGub at: (pdo-crud-for-free-example-project)\[\]

More detailed usage instructions (and important assumptions)
============================================================

[](#more-detailed-usage-instructions-and-important-assumptions)

ASSUMPTION 1: lowerCamelCase - DB table column names matching PHP Class properties
----------------------------------------------------------------------------------

[](#assumption-1-lowercamelcase---db-table-column-names-matching-php-class-properties)

This tool assumes your database table column names, and their corresponding PHP private class properties are named consistently in 'lowerCamelCase' e.g.

```
id
title
category
price

```

ASSUMPTION 2: lower case plural DB table name mapping to upper case singular PHP class name
-------------------------------------------------------------------------------------------

[](#assumption-2-lower-case-plural-db-table-name-mapping-to-upper-case-singular-php-class-name)

If you have a DB table '**products**' this will correspond to a PHP class '**Product**'

table names are named lower case, and are plural nouns, e.g '**users**' PHP class names are named with a capital first letter, and are singular nouns, e.g. '**User**'

ASSUMPTION 3: no constructor for your PHP classes
-------------------------------------------------

[](#assumption-3-no-constructor-for-your-php-classes)

due to the nature of PDO populating properties of objects when DB rows are converted into object instances do not have a constructor for the PHP classes that correspond to your DB tables

so you'd create a new object, and use the objects public 'setter' methods e.g.

```
$p = new Product();
$p->setDescription('hammer');
$p->setPrice(9.99);
etc.

```

step 1: create your DB tables
-----------------------------

[](#step-1-create-your-db-tables)

e.g. create your tables (with integer 'id' field, primary key, auto-increment)

e.g. SQL table to store DVD data

```
id:int (primary key, autoincrement)
title:text
category:text
price:float

```

step 2: create a corresponding PHP class, and subclass from Mattsmithdev\\PdoCrud\\DatabaseTable
------------------------------------------------------------------------------------------------

[](#step-2-create-a-corresponding-php-class-and-subclass-from-mattsmithdevpdocruddatabasetable)

e.g.

```
