PHPackages                             fivesqrd/atlas-foundation - 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. fivesqrd/atlas-foundation

ActiveLibrary

fivesqrd/atlas-foundation
=========================

Simple Data Mapper Library

v3.2.15(6y ago)21.2k1[1 issues](https://github.com/fivesqrd/atlas-foundation/issues)2PHP

Since Jan 24Pushed 6y ago1 watchersCompare

[ Source](https://github.com/fivesqrd/atlas-foundation)[ Packagist](https://packagist.org/packages/fivesqrd/atlas-foundation)[ RSS](/packages/fivesqrd-atlas-foundation/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (23)Used By (2)

Atlas Data Mapper
=================

[](#atlas-data-mapper)

Atlas is an open source [data mapper](https://en.wikipedia.org/wiki/Data_mapper_pattern) implementation for PHP.

Atlas creates barebones models for your project with minimal effort, allowing you to start working with them quickly. Extending or customising functionality is possible, but can wait until it is required.

The framework offers the following features:

- Minimal scaffolding required to create new models
- Easily expose business logic query layer
- Reduced application wide ripples from schema changes
- Automatic read/write routing
- Protection against SQL injection attacks
- RDBMS abstraction

Use Cases
---------

[](#use-cases)

Persisting a new user:

```
$user = new Model\User\Entity();
$user->set('_email', 'user@domain.com');
$user->set('_enabled', true);

/* Save to db */
$id = $this->model(Model\User::class)->save($user);

```

Fetching an instance of the user entity by primary key:

```
$user = $this->model(Model\User::class)->fetch($id);

```

Access properties using default getters:

```
$timestamp = $user->get('_lastLogin');

```

Persisting changes to the user model using default setters:

```
/* Fetch from db */
$user = $this->model(Model\User::class)->fetch($id);

/* Update entity */
$user->set('_lastLogin', time());

/* Save to db */
$this->model(Model\User::class)->save($user);

```

Querying user model business layer:

```
$users = $this->model(Model\User::class)->query()
    ->isEnabled(true)
    ->hasLoggedSince(strtotime('5 days ago'))
    ->fetch()->all();

foreach ($users as $user) {
    echo $user->get('_email');
}

```

Optimised queries for simple operations like counts:

```
$count = $this->model(Model\User::class)->named()
    ->withRecentLogIn()
    -fetch()->count();

```

Extending Model Classes
-----------------------

[](#extending-model-classes)

Access properties using custom getters:

```
$date = $user->getLastLogin('Y-m-d');

```

Persisting changes using custom setters:

```
/* Fetch from db */
$user = $this->model(Model\User::class)->fetch($id);

/* Update entity */
$user->setEmailAddress('user@domain.com');
$user->setEnabled(true);

/* Save to db */
$this->model(Model\User::class)->save($user);

```

Using named queries for consistent results:

```
$users = $this->model(Model\User::class)->named()
    ->withRecentLogIn()
    -fetch()->all();

```

Adding to named queries on the fly:

```
$users = $this->model(Model\User::class)->named()
    ->withRecentLogIn()
    ->isEnabled(true)
    -fetch()->all();

```

Performing operations on collections:

```
$users = $this->model(Model\User::class)->named()
    ->withRecentLogIn()
    -fetch()->all();

$emails = $users->getAllEmailAddresses();

```

Implementation
--------------

[](#implementation)

Each model consists of a set of classes. Each class extends a super class, to allow new models to be created with minimal effort.

Below is an example what a project with 3 models might look like

```
|- Model
   |-- User.php
   |-- User
       |-- Entity.php
       |-- Mapper.php
       |-- Collection.php
       |-- Query.php
       |-- Named.php
       |-- Relation.php
   |-- Customer.php
   |-- Customer
       ...
   |-- Content.php
   |-- Contact
       ...

```

Sample mapper classs

```
