PHPackages                             activecollab/databaseobject - 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. activecollab/databaseobject

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

activecollab/databaseobject
===========================

Library that enables easy persistance of objects to the MySQL 5.7+ database

7.0.2(10mo ago)217.1k1[1 PRs](https://github.com/activecollab/databaseobject/pulls)1MITPHPPHP &gt;=8.2

Since Nov 11Pushed 9mo ago3 watchersCompare

[ Source](https://github.com/activecollab/databaseobject)[ Packagist](https://packagist.org/packages/activecollab/databaseobject)[ Docs](https://labs.activecollab.com)[ RSS](/packages/activecollab-databaseobject/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (8)Versions (39)Used By (1)

DatabaseObject Library
======================

[](#databaseobject-library)

[![Build Status](https://camo.githubusercontent.com/730bbab9316c46bee8f7cd26ea9f5a907b7bfd1cb9c04db7c4d772d7cc42f8e1/68747470733a2f2f7472617669732d63692e6f72672f616374697665636f6c6c61622f64617461626173656f626a6563742e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/activecollab/databaseobject)

DatabaseObject library is a set of classes that make work with database just a bit easier. Here are the key concepts:

1. Types - entities that map to a single table,
2. Entities - type instances that map to a single row of a type table,
3. Entity Manager - instances that provide access to custom type specific manipulation methods,
4. Collections - a group of objects that meets the given criteria. This group is perfect for HTTP responses because collections support data tagging and tag validation (ETag and HTTP 304),
5. Pool - manage registered types and make multi-type interaction possible,
6. Producers - customise the way pool produces new instances,
7. Validators - validate object properties before saving them to the database.

CRUD
----

[](#crud)

If you wish to work with entire tables, use CRUD methods provided by `ConnectionInterface`:

1. `ConnectionInterface::insert()` - insert one or more rows
2. `ConnectionInterface::update()` - update a set of rows that match the given conditions, if any
3. `ConnectionInterface::delete()` - drop a set of rows taht match the given conditions, if any

When you need to work with individual instances, `PoolInterface` provides following handy methods:

1. `PoolInterface::produce()` - create a new record based on the given parameters,
2. `PoolInterface::modify()` - change the given object with a set of parameters,
3. `PoolInterface::scrap()` - trash or permanently delete the given object.

Scrap
-----

[](#scrap)

Recently we added `ScrapInterface`. This interface should be implemented by models which support object trashing, instead of instant deletion. When `PoolInterface::scrap()` method is called, objects that implement `ScrapInterface` will be scrapped (marked as deleted or trashed, depending on a particular implementation), instead of being permanently deleted.

Finder
------

[](#finder)

To set conditions, use `where` method:

```
$pool->find(Writer::class)
     ->where('`birthday` > ?', '1800-01-01')
     ->ids();
```

This method can be called multiple times, and all conditions will be joined in one block with `AND` operator:

```
$pool->find(Writer::class)
     ->where('`birthday` > ?', '1800-01-01')
     ->where('`birthday` < ?', '1825-01-01')
     ->ids();
```

Finder can join a table, either by table name:

```
$pool->find(Writer::class)
     ->joinTable('writer_groups')
     ->where('`writer_groups`.`group_id` = ?', $group_id)
     ->ids();
```

or by related type:

```
$pool->find(Writer::class)
     ->join(WriterGroup::class)
     ->where('`writer_groups`.`group_id` = ?', $group_id)
     ->ids();
```

Note that in the second case, `WriterGroup` type needs to be registered in the pool.

DI Container
------------

[](#di-container)

Pool implements `ActiveCollab\ContainerAccess\ContainerAccessInterface`, so you can set any container that implements `Interop\Container\ContainerInterface` interface, and that container will be passed on and made available in finders, producers and objects:

```
$container = new Container([
    'dependency' => 'it works!',
]);

$pool->setContainer($container);

foreach ($pool->find(Writer::class)->all() as $writer) {
    print $writer->dependency . "\n"; // Prints it works!
}
```

Generated Fields
----------------

[](#generated-fields)

Generated fields are fields that exist in tables, but they are not controlled or managed by the entity class itself. Instead, values of these models are set elsewhere:

1. They are specifief as generated columns in table's definition,
2. Trigger set the values,
3. Values are set by external systems or processes.

Library provides access to values of these fields, via accessors methods, but these values can't be set using setter methods:

```
