-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #22 from byjg/softdelete
Add Soft Delete and Others
- Loading branch information
Showing
32 changed files
with
1,398 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
# Active Record | ||
|
||
Active Record is the M in MVC - the model - which is the layer of the system responsible | ||
for representing business data and logic. Active Record facilitates the creation and use of | ||
business objects whose data requires persistent storage to a database. | ||
It is an implementation of the Active Record pattern which itself is a description of an | ||
Object Relational Mapping system. | ||
|
||
## How to use Active Record | ||
|
||
- Create a model and add the annotations to the class and properties. (see [Model](model.md)) | ||
- Add the `ActiveRecord` trait to your model. | ||
- Initialize the Active Record with the `initialize` static method (need to do only once) | ||
|
||
e.g.: | ||
|
||
```php | ||
<?php | ||
[TableAttribute('my_table')] | ||
class MyClass | ||
{ | ||
// Add the ActiveRecord trait to enable the Active Record | ||
use ActiveRecord; | ||
|
||
#[FieldAttribute(primaryKey: true)] | ||
public ?int $id; | ||
|
||
#[FieldAttribute(fieldName: "some_property")] | ||
public ?int $someProperty; | ||
} | ||
|
||
// Initialize the Active Record | ||
MyClass::initialize($dbDriver); | ||
``` | ||
|
||
## Using the Active Record | ||
|
||
Once is properly configured you can use the Active Record to save, update, delete and retrieve the data. | ||
|
||
### Insert a new record | ||
|
||
```php | ||
<?php | ||
// Create a new instance | ||
$myClass = MyClass::new(); | ||
$myClass->someProperty = 123; | ||
$myClass->save(); | ||
|
||
// Another example Create a new instance | ||
$myClass = MyClass::new(['someProperty' => 123]); | ||
$myClass->save(); | ||
``` | ||
|
||
### Retrieve a record | ||
|
||
```php | ||
<?php | ||
$myClass = MyClass::get(1); | ||
$myClass->someProperty = 456; | ||
$myClass->save(); | ||
``` | ||
|
||
### Complex Filter | ||
|
||
```php | ||
<?php | ||
$myClassList = MyClass::filter((new IteratorFilter())->and('someProperty', Relation::EQUAL, 123)); | ||
foreach ($myClassList as $myClass) { | ||
echo $myClass->someProperty; | ||
} | ||
``` | ||
|
||
### Delete a record | ||
|
||
```php | ||
<?php | ||
$myClass = MyClass::get(1); | ||
$myClass->delete(); | ||
``` | ||
|
||
### Using the `Query` class | ||
|
||
```php | ||
<?php | ||
$query = MyClass::joinWith('other_table'); | ||
// do some query here | ||
// ... | ||
// Execute the query | ||
$myClassList = MyClass::query($query); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Auto Discovering Relationship | ||
|
||
The `FieldAttribute` has a parameter `parentTable` that is used to define the parent table of the field. This is used in | ||
the case of a foreign key. | ||
|
||
Once this attribute is set, the `Repository` can auto-discover the relationship between the tables | ||
and generate the proper SQL with the relationship to retrieve the data. | ||
|
||
## How to use | ||
|
||
You can use the `parentTable` parameter in the `FieldAttribute` to define the parent table of the field. | ||
|
||
```php | ||
<?php | ||
#[TableAttribute('table1')] | ||
class Class1 | ||
{ | ||
#[FieldAttribute(primaryKey: true)] | ||
public ?int $id; | ||
} | ||
|
||
#[TableAttribute('table2')] | ||
class Class2 | ||
{ | ||
#[FieldAttribute(primaryKey: true)] | ||
public ?int $id; | ||
|
||
#[FieldAttribute(fieldName: "id_table1", parentTable: "table1")] | ||
public ?int $idTable1; | ||
} | ||
|
||
$repository1 = new Repository($dbDriver, Class1::class); | ||
$repository2 = new Repository($dbDriver, Class2::class); | ||
``` | ||
|
||
This will automatically create the relationship between `table1` and `table2`. | ||
|
||
To generate the SQL with the relationship you can use the `ORM` static class: | ||
|
||
```php | ||
<?php | ||
$query = ORM::getQueryInstance("table1", "table2"); | ||
``` | ||
|
||
The command above will return a query object similar to: | ||
|
||
```php | ||
<?php | ||
$query = Query::getInstance() | ||
->table('table1') | ||
->join('table2', 'table2.id_table1 = table1.id'); | ||
``` | ||
|
||
## Limitations | ||
|
||
- This feature does not support multiple relationships between the same tables | ||
- Primary keys with two or more fields are not supported. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.