PHPackages                             mattsmithdev/pdo-crud-for-free-repositories - 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-repositories

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

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

make it really simply to do db crud with pdo

v2.8(3mo ago)51.0k↓100%[3 issues](https://github.com/dr-matt-smith/pdo-crud-for-free-repositories/issues)1MITPHPPHP ^7.2.5|^8.0CI failing

Since Apr 22Pushed 3mo ago1 watchersCompare

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

READMEChangelog (10)Dependencies (1)Versions (25)Used By (1)

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

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

[![Build Status](https://camo.githubusercontent.com/ad8c39a944a0cde7c5259f700993afe9f513fe50d698e1b925c6d0d75d765819/68747470733a2f2f7472617669732d63692e6f72672f64722d6d6174742d736d6974682f70646f2d637275642d666f722d667265652d7265706f7369746f726965732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/dr-matt-smith/pdo-crud-for-free-repositories) [![Latest Stable Version](https://camo.githubusercontent.com/2bd93d692e856f0cc13e7f931b748cc84186b936ff7ba28fa4961b58d7c4d0ba/68747470733a2f2f706f7365722e707567782e6f72672f6d617474736d6974686465762f70646f2d637275642d666f722d667265652d7265706f7369746f726965732f762f737461626c65)](https://packagist.org/packages/mattsmithdev/pdo-crud-for-free-repositories) [![Total Downloads](https://camo.githubusercontent.com/0c8c88b52c948fac8abb0354bb91e026963481ec72b41eab2a0169093f3a2219/68747470733a2f2f706f7365722e707567782e6f72672f6d617474736d6974686465762f70646f2d637275642d666f722d667265652d7265706f7369746f726965732f646f776e6c6f616473)](https://packagist.org/packages/mattsmithdev/pdo-crud-for-free-repositories) [![Latest Unstable Version](https://camo.githubusercontent.com/6beb3cc643b2967c7035bccc1b648e613215ce2d413c0da2b5a71c499d97d9ad/68747470733a2f2f706f7365722e707567782e6f72672f6d617474736d6974686465762f70646f2d637275642d666f722d667265652d7265706f7369746f726965732f762f756e737461626c65)](https://packagist.org/packages/mattsmithdev/pdo-crud-for-free-repositories) [![License](https://camo.githubusercontent.com/b52d0cf19f3ed1e7dad2bbf2e736799f5bc6da70897d70885bd6b0ecb2b91dd7/68747470733a2f2f706f7365722e707567782e6f72672f6d617474736d6974686465762f70646f2d637275642d666f722d667265652d7265706f7369746f726965732f6c6963656e7365)](https://packagist.org/packages/mattsmithdev/pdo-crud-for-free-repositories)

Note - this is essentially an alternative approach to the [pdo-crud-for-free](link-packagist) package

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) methods, 'for free', simply by creating an entity repository sub-class of Mattsmithdev\\PdoCrudRepo\\DatabaseTableRepository.

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

Example project using this library
----------------------------------

[](#example-project-using-this-library)

There is an example project illustrating use of this library:

-

Install
-------

[](#install)

Via Composer

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

Usage
-----

[](#usage)

This example assumes you have a MySQL DB table named 'movie', with columns 'id' and 'title'. You need to write a corresponding class 'Movie' (note capitalization on the first letter - since this is a PHP class). Also you need to write a repository class to work between your PHP class and is corresponding table, in this example the repository class is named 'MovieRepository':

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

    class Movie
    {
        // private properties with EXACTLY same names as DB table columns
        private $id;
        private $title;

        public function getId()
        {
            return $this->id;
        }

        public function getTitle()
        {
            return $this->title;
        }
    }
```

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

    use Mattsmithdev\PdoCrudRepo\DatabaseTableRepository;

    class MovieRepository extends DatabaseTableRepository
    {
        // no methods needed if you've followed defaults
        // all the 'magic' is done through relfection ...
    }
```

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

    require_once __DIR__ . '/';

    // create a repository object
    use \MovieRepository;
    $movieRepository = new MovieRepository();

    // get all records from DB as an array of Dvd objects
    $movies = $movieRepository->findAll();

    // output each Dvd object as HTML lines in the form 'title = Jaws II'
    foreach($movies as $movie){
        /**
         * @var $movie \Movie
         */
        print 'id = ' . $movie->getId();
        print '';
        print 'title = ' . $movie->getTitle();
        print '';
    }
```

Finally, you need to have defined your DB connection credentials in a file `.env` as follows:

```
    MYSQL_USER=root
    MYSQL_PASSWORD=passpass
    MYSQL_HOST=127.0.0.1
    MYSQL_PORT=3306
    MYSQL_DATABASE=evote
```

The named database schema will be created, if it does not already exist...

For more details see below. Also there is a full sample web application project on GitGub at: [pdo-crud-for-free-repositories-example-project](https://github.com/dr-matt-smith/pdo-crud-for-free-repositories-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
vatRate
firstName
aLongVariableNameOfSeveralWords

```

ASSUMPTION 2: No constructor for your PHP classes.
--------------------------------------------------

[](#assumption-2-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.

```
    $m = new Movie();
    $m->setTitle('Jaws');
    $m->setPrice(9.99);
    etc.
```

ASSUMPTION 3: Each class has an integer, `id` property
------------------------------------------------------

[](#assumption-3-each-class-has-an-integer-id-property)

Each Entity class should have an integer `id` property. This property should be an `AUTO_INCREMENT` primary key in the database table schema, e.g.

```
    -- SQL statement to create the table --
    create table if not exists movie (
        id integer primary key AUTO_INCREMENT,
        title text,
        price float
    );
```

NOTE: Please don't name this anything else, not `idMovie` or `movieId` or `ID` etc. - just plain old `id`

ASSUMPTION 4: DB table name is singular and all lower case
----------------------------------------------------------

[](#assumption-4-db-table-name-is-singular-and-all-lower-case)

This tool assumes your database table name is singular, all **lower case**. E.g.

- table name: `movie`

    - entity class name: `Movie.php`
- table name: `moviecategory`

    - entity class name: `MovieCategory.php`
- table name: `alongtablename`

    - entity class name: `ALongTableName`

Step 1: Create your DB tables.
------------------------------

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

You need a database schema

- if not present, a new one will be created

For each entity class you need a corresponding DB table (with integer 'id' field, primary key, auto-increment)

- you can create these in your DB management tool
- you can use the Repository method `createTable()`
    - which can attempt to infer column data types from yhour entity properties, or use your own provided SQL

Step 2: Create a corresponding PHP (entity) class
-------------------------------------------------

[](#step-2-create-a-corresponding-php-entity-class)

e.g.

```
