<commit_before># Analogue ORM

**Analogue** is a port of the laravel's *Eloquent ORM*, implemented as a Data Mapper. It aims to provide a flexible, decoupled and easy to use ORM for large application designs. The project started as an attempt to solve the 'Repository pattern dilemna', and evolved into a fully functionnal ORM build to work nicely over the Laravel Database Abstraction layer.

##Main Features :

* Map SQL Database to database agnostic objects.
* Store Entity as a 'root aggregate', saving related Entities on the fly. 
* Make queries using the Fluent Query Builder
* Lazy loading of relationships via Proxy objects.
* Eager Loading for solving the N+1 problem
* Timestamps support
* Soft Deletes
* Polymorphic Relationships
* Dynamic Relationships
* Flexible event system
* Out of the box Authentication Driver for Laravel.
* Easily Extendable


## Installation

Add this line to your composer.json file : 

```
"Analogue/ORM": "~1.0"
```

Then run : 

```
composer update
```

Add the Service Provider to config/app.php :

```
'Analogue\ORM\AnalogueServiceProvider',
```

If you're using facades, add this line to the aliases :
```
'Analogue' => 'Analogue\ORM\AnalogueFacade',
```

## Creating and using Entity objects

### Creating a custom Entity class 

First let's create a simple User model :

```php
namespace Acme\Users;

use Analogue\ORM\Entity;

class User extends Entity {

	public function __construct($email, $password)
	{
		$this->email = $email;
		$this->password = \Hash::make($password);
	}

}
```

Analogue's Entity objects are the equivalent of Eloquent objects, with the main difference that they have no interaction with the database at all. DB Queries takes place inside of a repository object. 

## Using the Analogue built-in repository

To interact with the database, you can either create your own repository classes by *extending the Analogue\ORM\Repository class* (see 05), or use a factory method to build a repository on the fly. Let's use the latter for now :

```php

$user = new Acme\Users\User('<EMAIL>', 'MySecr3tP4ssword^&!');

$userRepository = Analogue::repository('Acme\Users\User');

$userRepository->store($user);

```

## Running queries 

Analogue repositories embed a custom query builder, allowing you to build complex queries that will seamlessly map to your Entity classes. Results that contains many rows are returned as an EntityCollection object.

```

$users = $userRepository->all();

$user = $userRepository->find(45);

$user = $userRepository->where('email', '=', '<EMAIL>')->first();

$users = $userRepository->paginate(50);

...
```

## Creating & Registering Maps

When you instantiate a repository for an Entity class, Analogue automatically create an EntityMap object that contains the configuration for Analogue to map objects to the database. 

However, if you need a more complex mapping, like adding relationships to other entities, defining a custom table name, using timestamps... you need to create an EntityMap class.

Let's create a Post entity and a related Comment entity.

Post.php :
```php
namespace Acme\Posts;

use Analogue\ORM\Entity;

class Post extends Entity {
	
	public function __construct($title, $content)
	{
		$this->title = $title;
		$this->content = $content;
	}

}

```

Comment.php :
```php
namespace Acme\Comments;

use Analogue\ORM\Entity;

class Comment extends Entity {
	
	public function __construct($message)
	{
		$this->message = $message;
	}

}

PostMap.php
```
namespace Acme\Posts;

use Analogue\ORM\EntityMap;

class PostMap extends EntityMap
{
	protected $timestamps = true;

	public function comments($entity)
	{
		return $this->hasMany('Acme\Comments\Comment');
	}

}

```
<commit_msg>Fix Markdown<commit_after># Analogue ORM

**Analogue** is a port of the laravel's *Eloquent ORM*, implemented as a Data Mapper. It aims to provide a flexible, decoupled and easy to use ORM for large application designs. The project started as an attempt to solve the 'Repository pattern dilemna', and evolved into a fully functionnal ORM build to work nicely over the Laravel Database Abstraction layer.

##Main Features :

* Map SQL Database to database agnostic objects.
* Store Entity as a 'root aggregate', saving related Entities on the fly. 
* Make queries using the Fluent Query Builder
* Lazy loading of relationships via Proxy objects.
* Eager Loading for solving the N+1 problem
* Timestamps support
* Soft Deletes
* Polymorphic Relationships
* Dynamic Relationships
* Flexible event system
* Out of the box Authentication Driver for Laravel.
* Easily Extendable


## Installation

Add this line to your composer.json file : 

```
"Analogue/ORM": "~1.0"
```

Then run : 

```
composer update
```

Add the Service Provider to config/app.php :

```
'Analogue\ORM\AnalogueServiceProvider',
```

If you're using facades, add this line to the aliases :
```
'Analogue' => 'Analogue\ORM\AnalogueFacade',
```

## Creating and using Entity objects

### Creating a custom Entity class 

First let's create a simple User model :

```php
namespace Acme\Users;

use Analogue\ORM\Entity;

class User extends Entity {

	public function __construct($email, $password)
	{
		$this->email = $email;
		$this->password = \Hash::make($password);
	}

}
```

Analogue's Entity objects are the equivalent of Eloquent objects, with the main difference that they have no interaction with the database at all. DB Queries takes place inside of a repository object. 

## Using the Analogue built-in repository

To interact with the database, you can either create your own repository classes by *extending the Analogue\ORM\Repository class* (see 05), or use a factory method to build a repository on the fly. Let's use the latter for now :

```php

$user = new Acme\Users\User('<EMAIL>', 'MySecr3tP4ssword^&!');

$userRepository = Analogue::repository('Acme\Users\User');

$userRepository->store($user);

```

## Running queries 

Analogue repositories embed a custom query builder, allowing you to build complex queries that will seamlessly map to your Entity classes. Results that contains many rows are returned as an EntityCollection object.

```

$users = $userRepository->all();

$user = $userRepository->find(45);

$user = $userRepository->where('email', '=', '<EMAIL>')->first();

$users = $userRepository->paginate(50);

...
```

## Creating & Registering Maps

When you instantiate a repository for an Entity class, Analogue automatically create an EntityMap object that contains the configuration for Analogue to map objects to the database. 

However, if you need a more complex mapping, like adding relationships to other entities, defining a custom table name, using timestamps... you need to create an EntityMap class.

Let's create a Post entity and a related Comment entity.

Post.php :
```php
namespace Acme\Posts;

use Analogue\ORM\Entity;

class Post extends Entity {
	
	public function __construct($title, $content)
	{
		$this->title = $title;
		$this->content = $content;
	}

}

```

Comment.php :
```php
namespace Acme\Comments;

use Analogue\ORM\Entity;

class Comment extends Entity {
	
	public function __construct($message)
	{
		$this->message = $message;
	}

}
```

PostMap.php
```
namespace Acme\Posts;

use Analogue\ORM\EntityMap;

class PostMap extends EntityMap
{
	protected $timestamps = true;

	public function comments($entity)
	{
		return $this->hasMany('Acme\Comments\Comment');
	}

}

```

{'fraction_non_alphanumeric': 0.09944676831736608, 'fraction_numerical': 0.0029685602482795843, 'mean_word_length': 4.5354742345033605, 'pattern_counts': {'":': 2, '<': 7, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7333711', 'n_tokens_mistral': 2238, 'n_tokens_neox': 2094, 'n_words': 873}