PHPackages                             parm/parm - 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. parm/parm

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

parm/parm
=========

PHP Active Record for MySQL. Connects to the your database and generates the Object Relational Mapping. Built in Database Query Processor for custom queries. Ability to use closures to process rows

4.0.5(8y ago)555.0k3[12 issues](https://github.com/cassell/Parm/issues)1BSD-2-ClausePHPPHP &gt;=5.5.0

Since Sep 19Pushed 8y ago1 watchersCompare

[ Source](https://github.com/cassell/Parm)[ Packagist](https://packagist.org/packages/parm/parm)[ Docs](http://github.com/cassell/parm)[ RSS](/packages/parm-parm/feed)WikiDiscussions master Synced 3w ago

READMEChangelog (3)Dependencies (5)Versions (62)Used By (1)

[![Parm Logo](https://camo.githubusercontent.com/591fc68bf151652148d857e606204fc19164d40fb3dcb0233afe4555ed1c1e06/68747470733a2f2f7261772e6769746875622e636f6d2f63617373656c6c2f5061726d2f6c6f676f2f7061726d2d6c6f676f2d3630302e706e67)](https://camo.githubusercontent.com/591fc68bf151652148d857e606204fc19164d40fb3dcb0233afe4555ed1c1e06/68747470733a2f2f7261772e6769746875622e636f6d2f63617373656c6c2f5061726d2f6c6f676f2f7061726d2d6c6f676f2d3630302e706e67)

Parm [![Build Status](https://camo.githubusercontent.com/2e37b87386a462be6f638f6b973ae3cd05e7c6deb99d62886357e22cc24277f3/68747470733a2f2f7472617669732d63692e6f72672f63617373656c6c2f5061726d2e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/cassell/Parm) [![Code Climate](https://camo.githubusercontent.com/4191925ccf4c2fce5693c2eff907aa111add74f34411dc31a6ba9dbd0497e01f/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f63617373656c6c2f5061726d2f6261646765732f6770612e737667)](https://codeclimate.com/github/cassell/Parm) [![Test Coverage](https://camo.githubusercontent.com/74b703abe68d064254493f952c51e0e3e309a9bf8199e296fb7878d5c33dd1be/68747470733a2f2f636f6465636c696d6174652e636f6d2f6769746875622f63617373656c6c2f5061726d2f6261646765732f636f7665726167652e737667)](https://codeclimate.com/github/cassell/Parm/coverage)
==============================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================

[](#parm---)

PHP Active Record for MySQL -- PHP, AR, ORM, DAO, OMG!

It generates models based on your schema and its powerful closure based query processing and ability to handle large data sets make it powerful and flexible.

1. PSR-4 Compliant and works with Composer
2. Handles all the CRUD (Creating, Reading, Updating, and Deleting)
3. Easily output data as JSON for APIs
4. Fast queries that can easily be limited to a subset of fields in a table ("select first\_name, last\_name from table" vs. "select \* from table"). And you can still use objects when using a subset of the fields.
5. SQL UPDATEs are minimal and only the modified columns/fields are sent to the database
6. Closure based query processing that lets you handle data efficiently and in a fully customizable manner
7. PagedCollection makes it very easy to page through a set of records one page at a time (Go through 1,000,000 records 1,000 at a time)
8. Models can be generated into a namespace or generated into the global namespace
9. Handles all escaping of input values when saving to the database
10. Bindings automatically escape of query values
11. Process any SQL query (multiple tables and joins) using the same closure based process model. Easily output the results to an Array or JSON
12. You can easily extend the Factories and Objects to encapsulate the logic of a model (fat models)
13. Will return the proper data type for the field (if it is a MySQL int(11) column an integer will be returned)
14. Method chaining of filters, limits, etc
15. Generates an autoloader for all of the generated classes/models if you don't generate them into a PSR autoloader directory
16. Convert Timezones Using MySQL Timezone Tables (if time\_zone tables are loaded)
17. Generated Code is creating using Mustache Templates
18. Full test suite using PHPUnit and Travis CI
19. Fully documented and generated classes are generated with PHPDoc "DocBlock" comments to assist your IDE

Example Usage
=============

[](#example-usage)

> See much more detail examples below. Note: You should also look at the tests as they contain many more examples

```
$user = User::findId(17); // find record with primary key 17
$user->setFirstName("John"); // set the first name
$user->save(); // save to the database

```

Setup and Generation
--------------------

[](#setup-and-generation)

### Composer (Packagist)

[](#composer-packagist)

```
"parm/parm": "^3.0"

```

### Example Database Configuration

[](#example-database-configuration)

```
\Parm\Config::setupConnection('parm_namespaced_tests', 'database-name-on-server','database-host','database-username','database-password');

```

or you can pass a Doctrine DBAL Connection

```
\Parm\Config::addConnection('parm-global-tests', new Doctrine\DBAL\Connection([
    'dbname' => $GLOBALS['db__name'],
    'user' => $GLOBALS['db_username'],
    'password' => $GLOBALS['db_password'],
    'host' => $GLOBALS['db_host']
], new Doctrine\DBAL\Driver\PDOMySql\Driver(), null, null));

```

### Example Generator Configuration

[](#example-generator-configuration)

```
$generator = new Parm\Generator\DatabaseGenerator(Parm\Config::getDatabase('people-database-name'));
$generator->setDestinationDirectory('/classes/dao/peopleDatabase');
$generator->setGeneratedNamespace("\\Dao\\PeopleDatabase");
$generator->generate();

```

Extending Models
----------------

[](#extending-models)

You can easily extend the models to encapsulate simple business logic. The examples below use these extended objects for brevity.

```
class User extends Project\Dao\UserDaoObject
{
	static function getFactory(\Doctrine\DBAL\Connection $connection = null)
	{
		return new UserFactory($connection);
	}

	//example function
	public function getFullName()
	{
		return $this->getFirstName() . " " . $this->getLastName();
	}
}

class UserFactory extends Project\Dao\UserDaoFactory
{
	function loadDataObject(Array $row = null)
	{
		return new User($row);
	}
}

```

CRUD
----

[](#crud)

### Creating

[](#creating)

```
$user = new User();
$user->setFirstName('Ada');
$user->setLastName('Lovelace');
$user->setEmail('lovelace@example.com');
$user->save();
echo $user->getId() // will print the new primary key

```

### Reading

[](#reading)

Finding an object with id 17.

```
// shorthand
$user = User::findId(17);

// you can also use a factory
$f = new UserFactory();
$user = $f->findId(17);

```

Finding all objects form a table (returns a Collection)

```
$f = new UserFactory();
$users = $f->findAll();

```

Limit the query to the first 20 rows

```
$f = new UserFactory();
$f->setLimit(20);
$users = $f->getCollection();

```

Querying for objects filtered by a column (the following four statements are all equivalent)

```
$f = new UserFactory();
$f->whereEquals("archived","0");
$users = $f->getCollection();

$f = new UserFactory();
$f->whereEquals(User::ARCHIVED_COLUMN,"0");

$f = new UserFactory();
$f->addBinding(new new \Parm\Binding\EqualsBinding(User::ARCHIVED_COLUMN,"0"));

// if use_global_namespace.php is included
$f = new UserFactory();
$f->addBinding(new EqualsBinding(User::ARCHIVED_COLUMN,"0"));

```

Contains searches for objects

```
// looking for users with example.com in their email
$f = new UserFactory();
$f->addBinding(new \Parm\Binding\ContainsBinding("email","example.com"));

// looking for users with example.com in their email using a case sensitive search
$f = new UserFactory();
$f->addBinding(new \Parm\Binding\CaseSensitiveContainsBinding("email","example.com"));

```

String based where clauses

```
// looking for active users
$f = new UserFactory();
$f->addBinding("user.archived != 1");

```

Filter by array

```
// looking for users created before today
$f = new UserFactory();
$f->addBinding(new \Parm\Binding\InBinding("zipcode_id",array(1,2,3,4)));

```

Filter by foreign key using an object

```
$f = new UserFactory();
$company = Company::findId(1);
$f->addBinding(new \Parm\Binding\ForeignKeyObjectBinding($company));

```

Date based searches

```
// looking for users created before today
$f = new UserFactory();
$f->addBinding(new \Parm\Binding\DateBinding("create_date",'
