Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
aryelgois committed Feb 17, 2018
2 parents 1ca912c + 86b296f commit 3e4043a
Show file tree
Hide file tree
Showing 11 changed files with 307 additions and 162 deletions.
33 changes: 32 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,36 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
## [Unreleased]


## [4.0.0] - 2018-02-17

### Added
- Model `fill()` example
- Model methods:
- `checkUnknownColumn()`
- `checkReadOnly()`
- `getIterator()`
- `getRequiredColumns()`
- `isFresh()`
- `onColumnChange()`
- `undo()`

### Changed
- Update [aryelgois/utils]
- Forbid assigning a fresh foreign model
- `validateHook()` only receives the data to be validated
- Person methods
- Rename Hook to Event
- Improve exceptions

### Removed
- `setMultiple()`

### Fixed
- ModelIterator and ModelManager `__construct()`: Ensure a model class is used,
and not a model instance
- Delete fresh soft model


## [3.1.1] - 2018-02-09

### Fixed
Expand Down Expand Up @@ -142,7 +172,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- DatabaseObject.php


[Unreleased]: https://github.com/aryelgois/Medools/compare/v3.1.1...develop
[Unreleased]: https://github.com/aryelgois/Medools/compare/v4.0.0...develop
[4.0.0]: https://github.com/aryelgois/Medools/compare/v3.1.0...v4.0.0
[3.1.1]: https://github.com/aryelgois/Medools/compare/v3.1.0...v3.1.1
[3.1.0]: https://github.com/aryelgois/Medools/compare/v3.0...v3.1.0
[3.0]: https://github.com/aryelgois/Medools/compare/v3.0-alpha...v3.0
Expand Down
38 changes: 22 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Index:
- [SOFT_DELETE]
- [SOFT_DELETE_MODE]
- [Advanced]
- [Hooks]
- [Events]
- [ModelManager]
- [Changelog]

Expand Down Expand Up @@ -147,9 +147,11 @@ And change data with:
A [ModelIterator] is provided to access multiple rows, but it provides only one
at time.

Give it a model instance you want to iterate over, it can be a fresh one, and
some [filter][where_clause] array. Then it will `load()` each matched row, one
by one.
Give it a model class you want to iterate over, and some [filter][where_clause]
array. Then it will `load()` each matched row, one by one.

A shortcut is calling `getIterator()` directly from the model class, which just
asks for `$where`.


## Foreign models
Expand Down Expand Up @@ -178,13 +180,17 @@ Useful methods that are available:
to keep timezone consistent
- `getDatabase()`: Gives a direct access to the Database, already connected and
ready to use. See [catfan/Medoo] for details
- `getRequiredColumns()`: Gives a list of columns that must be set before saving
- `isFresh()`: Tells if the object is a new Model
- `jsonSerialize()`: You can [json_encode] models!
- `reload()`: Use to re fetch the row with model's Primary Key
- `undo()`: Removes changes. Pass a column name to only remove that column,
otherwise it removes all changes

You can also add custom methods in your models, to automatically get some data
in a format, or for doing a specific task.

> There are also [hook methods][hooks] that are automatically called by some
> There are also [event methods][events] that are automatically called by some
> base methods.

Expand Down Expand Up @@ -353,19 +359,19 @@ Possible value | When not deleted | When deleted

# Advanced

## Hooks
## Events

There is a Hook concept in this framework, where you can add specific methods
which are automatically called by default methods. It makes easier to extend
some functionalities.
There are some methods that can be extended by overriding event methods. It
makes easier to extend some functionalities.

Currently, these hooks are available:
Currently, these events are available:

- `onFirstSaveHook()`: Called on the first time a model is saved
- `onSaveHook()`: Called every time a model is saved
- `validateHook()`: Use it to validate the data before sending to the Database.
Make sure your code can validate some columns or all of them, depending on the
`$full` argument.
- `onColumnChange()`: Called when a column is changed. Useful to filter data
before storing in the model
- `onFirstSave()`: Called on the first time a model is saved
- `onSave()`: Called every time a model is saved
- `onValidate()`: Called when data needs to be validated, before storig in the
Database

## ModelManager

Expand Down Expand Up @@ -407,7 +413,7 @@ this class or in the model.
[SOFT_DELETE]: #soft_delete
[SOFT_DELETE_MODE]: #soft_delete_mode
[Advanced]: #advanced
[Hooks]: #hooks
[Events]: #events
[ModelManager]: #modelmanager

[config_example]: config/example.php
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"php": "^7.0",
"aryelgois/utils": "^0.2",
"aryelgois/utils": "^0.3.0",
"catfan/medoo": "^1.5"
},
"autoload": {
Expand Down
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 11 additions & 1 deletion src/Exceptions/NotForeignColumnException.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,14 @@
* @link https://www.github.com/aryelgois/Medools
*/
class NotForeignColumnException extends \InvalidArgumentException
{}
{
public function __construct(
string $model,
string $column,
Throwable $previous = null
) {
$message = "$model (`$column`) has no Foreign Key constraint";

parent::__construct($message, 0, $previous);
}
}
2 changes: 1 addition & 1 deletion src/MedooConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public static function getInstance($database, $anonymous = false)
throw new \BadMethodCallException('Medools config was not loaded');
}
if (!array_key_exists($database, self::$config['databases'])) {
throw new \RuntimeException('Unknown database');
throw new \RuntimeException("Unknown database '$database'");
}

if ($anonymous || !array_key_exists($database, self::$instances)) {
Expand Down
Loading

0 comments on commit 3e4043a

Please sign in to comment.