-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cleanups to make 0.5 release more stable #43
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
e8d09f7
insert() return model, not id
romaninsh 5982bde
$m->insertRows() added
romaninsh 344de87
join->addFields(['fo','bar']); now works
romaninsh a242beb
implemented model::reload()
romaninsh 5ad33cb
expand documentation for persitence
romaninsh 210abd4
rename fieldValues into field
romaninsh acb727f
update to use 'field' and remove 'fieldValues'
romaninsh 7e5d2a6
remove reference to fieldValues
romaninsh 10cfe86
removed TODO and cleanup
romaninsh 80a1915
hook allows us to control all models that are being added
romaninsh afb74fa
renamed insertRows() into import()
romaninsh 4b3350f
added implementation for title fields
romaninsh d18fd6e
Cleaned up insert / import
romaninsh 1d978fa
clean up some method scope (we're at 45 public method threshold in CC)
romaninsh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
language: php | ||
|
||
sudo: required | ||
|
||
php: | ||
- '5.5' | ||
- '5.6' | ||
|
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 |
---|---|---|
|
@@ -11,8 +11,8 @@ Contents: | |
quickstart | ||
design | ||
model | ||
results | ||
persistence | ||
results | ||
fields | ||
conditions | ||
sql | ||
|
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,219 @@ | ||
|
||
|
||
============================= | ||
Saving and Loading Model Data | ||
============================= | ||
|
||
.. php:class:: Model | ||
|
||
In order to load and store data of your model inside the database your model should be | ||
"associated" with persistence layer. | ||
|
||
Associating with Persistence | ||
============================ | ||
|
||
Create your persistence object first:: | ||
|
||
$db = \atk4\data\Persistence::connect($dsn); | ||
|
||
There are several ways to link your model up with the persistence:: | ||
|
||
$m = new Model_Invoice($db); | ||
|
||
$m = $db->add(new Model_Invoice()); | ||
|
||
$m = $db->add('Invoice'); | ||
|
||
.. php:method:: load | ||
|
||
Load active record from the DataSet:: | ||
|
||
$m->load(10); | ||
echo $m['name']; | ||
|
||
If record not found, will throw exception. | ||
|
||
.. php:method:: tryLoad | ||
|
||
Same as load() but will silently fail if record is not found:: | ||
|
||
$m->tryLoad(10); | ||
$m->set($data); | ||
|
||
$m->save(); // will either create new record or update existing | ||
|
||
.. php:method:: loadAny | ||
|
||
Attempt to load any matching record. You can use this in conjunciton with setOrder():: | ||
|
||
$m->loadAny(); | ||
echo $m['name']; | ||
|
||
.. php:method:: tryLoadAny | ||
|
||
Attempt to load any record, but silently fail if there are no records in the DataSet. | ||
|
||
.. php:method:: unload | ||
|
||
Remove active record and restore model to default state:: | ||
|
||
$m->load(10); | ||
$m->unload(); | ||
|
||
$m['name'] = 'New User'; | ||
$m->save(); // creates new user | ||
|
||
.. php:method:: delete($id = null) | ||
|
||
Remove current record from DataSet. You can optionally pass ID if you wish to delete | ||
a different record. If you pass ID of a currently loaded record, it will be unloaded. | ||
|
||
Actions | ||
======= | ||
|
||
Action is a multi-row operation that will affect all the records inside DataSet. Actions | ||
will not affect records outside of DataSet (records that do not match conditions) | ||
|
||
.. php:method:: action($action, $args = []) | ||
|
||
Prepares a special object reperesnting "action" of a persistance layer based around | ||
your current model:: | ||
|
||
$m = Model_User(); | ||
$m->addCondition('last_login', '<', date('Y-m-d', strtotime('-2 months'))); | ||
|
||
$m->action('delete')->execute(); | ||
|
||
|
||
Action Types | ||
------------ | ||
|
||
Actions can be grouped by their result. Some action will be executed and will not | ||
produce any results. Others will respond with either one value or multiple rows of | ||
data. | ||
|
||
- no results | ||
- single value | ||
- single row | ||
- single column | ||
- array of hashes | ||
|
||
Action can be executed at any time and that will return an expected result:: | ||
|
||
$m = Model_Invoice(); | ||
$val = $m->action('count')->getOne(); | ||
|
||
Most actions are sufficiently smart to understand what type of result you are expecting, | ||
so you can have the following code:: | ||
|
||
$m = Model_Invoice(); | ||
$val = $m->action('count')(); | ||
|
||
When used inside the same Persistence, sometimes actions can be used without executing:: | ||
|
||
$m = Model_Product($db); | ||
$m->addCondition('name', $product_name); | ||
$id_query_action = $m->action('getOne',['id']); | ||
|
||
$m = Model_Invoice($db); | ||
$m->insert(['qty'=>20, 'product_id'=>$id_query_action]); | ||
|
||
Insert operation will check if you are using same persistence. If the persistence object | ||
is different, it will execute action and will use result instead. | ||
|
||
Being able to embed actions inside next query allows Agile Data to reduce number of | ||
queries issued. | ||
|
||
The default action type can be set when executing action, for example:: | ||
|
||
$a = $m->action('field', 'user', 'getOne'); | ||
|
||
echo $a(); // same as $a->getOne(); | ||
|
||
SQL Actions | ||
----------- | ||
|
||
The following actions are currently supported by Persistence_SQL: | ||
|
||
- select - produces query that returns DataSet (array of hashes) | ||
- delete - produces query for deleting DataSet (no result) | ||
|
||
The following two queries returns un-populated query, which means if you wish to use | ||
it, you'll have to populate it yourself with some values: | ||
|
||
- insert - produces an un-populated insert query (no result). | ||
- update - produces query for updating DataSet (no result) | ||
|
||
Example of using update:: | ||
|
||
$m = Model_Invoice($db); | ||
$m->addCondition('has_discount', true); | ||
|
||
$m->action('update') | ||
->set('has_dicount', false) | ||
->execute(); | ||
|
||
You must be aware that set() operates on a DSQL object and will no longer | ||
work with your model fields. You should use the object like this if you can:: | ||
|
||
$m->action('update') | ||
->set($m->getElement('has_discount'), false) | ||
->execute(); | ||
|
||
See $actual for more details. | ||
|
||
There are ability to execute aggregation functions:: | ||
|
||
echo $m->action('fx', ['max', 'salary'])->getOne(); | ||
|
||
and finally you can also use count:: | ||
|
||
echo $m->action('count')->getOne(); | ||
|
||
|
||
SQL Actions on Linked Records | ||
----------------------------- | ||
|
||
In conjunction with Model::refLink() you can produce expressions for creating | ||
sub-selects. The functionality is nicely wrapped inside Field_SQL_Many::addField():: | ||
|
||
$client->hasMany('Invoice') | ||
->addField('total_gross', ['aggregate'=>'sum', 'field'=>'gross']); | ||
|
||
This operation is actually consisting of 3 following operations:: | ||
|
||
1. Related model is created and linked up using refLink that essentially places | ||
a condition between $client and $invoice assuming they will appear inside same query. | ||
|
||
2. Action is created from $invoice using 'fx' and requested method / field. | ||
|
||
3. Expression is created with name 'total_gross' that uses Action. | ||
|
||
Here is a way how to intervene with the process:: | ||
|
||
$client->hasMany('Invoice'); | ||
$client->addExpression('last_sale', function($m) { | ||
return $m->refLink('Invoice') | ||
->setOrder('date desc') | ||
->setLimit(1) | ||
->action('field', ['total_gross'], 'getOne'); | ||
|
||
}); | ||
|
||
The code above uses refLink and also creates expression, but it tweaks the action used. | ||
|
||
|
||
Action Matrix | ||
-------------- | ||
|
||
SQL actions apply the following: | ||
|
||
- insert: init, mode | ||
- update: init, mode, conditions, limit, order, hook | ||
- delete: init, mode, conditions | ||
- select: init, fields, conditions, limit, order, hook | ||
- count: init, field, conditions, hook, | ||
- field: init, field, conditions | ||
- fx: init, field, conditions | ||
|
||
|
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
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely looks more clear this way