Last active
August 29, 2015 14:09
-
-
Save chanmix51/3f47f8c2e4440f53daee to your computer and use it in GitHub Desktop.
Pomm 2 examples
This file contains hidden or 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
<?php | |
use \PommProject\Foundation\Pomm; | |
$loader = require __DIR__.'/vendor/autoload.php'; | |
$loader->add(null, __DIR__.'/sources/lib'); | |
// customize the DSN with your database parameters. | |
return new Pomm(['elcaro' => | |
[ | |
'dsn' => 'pgsql://user:pass@host:port/db_name', | |
'class:session_builder' => '\PommProject\ModelManager\SessionBuilder' | |
] | |
]); |
This file contains hidden or 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
<?php | |
namespace Model\Elcaro\ElcaroSchema; | |
use PommProject\ModelManager\Model\FlexibleEntity; | |
/** | |
* Employee | |
* | |
* Flexible entity for relation | |
* elcaro.employee | |
* | |
* @see FlexibleEntity | |
*/ | |
class Employee extends FlexibleEntity | |
{ | |
public function getFirstName() | |
{ | |
return ucwords($this->get('first_name')); | |
} | |
public function getLastName() | |
{ | |
return strtoupper($this->get('last_name')); | |
} | |
} |
This file contains hidden or 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
<?php | |
namespace Model\Elcaro\ElcaroSchema; | |
use PommProject\ModelManager\Model\Model; | |
use PommProject\ModelManager\Model\Projection; | |
use PommProject\ModelManager\Model\ModelTrait\WriteQueries; | |
use PommProject\Foundation\Where; | |
use Model\Elcaro\ElcaroSchema\AutoStructure\Employee as EmployeeStructure; | |
use Model\Elcaro\ElcaroSchema\Employee; | |
/** | |
* EmployeeModel | |
* | |
* Model class for table employee. | |
* | |
* @see Model | |
*/ | |
class EmployeeModel extends Model | |
{ | |
use WriteQueries; | |
/** | |
* __construct() | |
* | |
* Model constructor | |
* | |
* @access public | |
* @return void | |
*/ | |
public function __construct() | |
{ | |
$this->structure = new EmployeeStructure; | |
$this->flexible_entity_class = "\Model\Elcaro\ElcaroSchema\Employee"; | |
} | |
public function createProjection() | |
{ | |
return parent::createProjection() | |
->setField('age', 'age(%birth_date)', 'interval') | |
; | |
} | |
} |
This file contains hidden or 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
$ php vendor/bin/pomm.php generate:schema-all -a Model -d sources/lib elcaro elcaro | |
✓ Creating file 'sources/lib/Model/Elcaro/ElcaroSchema/AutoStructure/Department.php'. | |
✓ Creating file 'sources/lib/Model/Elcaro/ElcaroSchema/DepartmentModel.php'. | |
✓ Creating file 'sources/lib/Model/Elcaro/ElcaroSchema/Department.php'. | |
✓ Creating file 'sources/lib/Model/Elcaro/ElcaroSchema/AutoStructure/Employee.php'. | |
✓ Creating file 'sources/lib/Model/Elcaro/ElcaroSchema/EmployeeModel.php'. | |
✓ Creating file 'sources/lib/Model/Elcaro/ElcaroSchema/Employee.php'. |
This file contains hidden or 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
<?php | |
use PommProject\Foundation\Where; | |
$pomm = require __DIR__."/.pomm_cli_bootstrap.php"; | |
$employees = $pomm['elcaro'] | |
->getModel('\Model\Elcaro\ElcaroSchema\EmployeeModel') | |
->findWhere(new Where('birth_date < $* and not is_manager', ['1980-01-01'])) | |
; | |
foreach ($employees as $employee) { | |
printf( | |
"%s %s '%s' old dept %s salary %s€.\n", | |
$employee['first_name'], | |
$employee['last_name'], | |
$employee['age']->format('%y years %m months'), | |
$employee['department_id'], | |
number_format($employee['day_salary'], 0, ',', ' ') | |
); | |
} |
This file contains hidden or 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
$ php vendor/bin/pomm.php inspect:relation elcaro employee elcaro | |
Relation elcaro.employee | |
+----+---------------+---------+-----------------------------------------------+---------+---------+ | |
| pk | name | type | default | notnull | comment | | |
+----+---------------+---------+-----------------------------------------------+---------+---------+ | |
| * | employee_id | int4 | nextval('employee_employee_id_seq'::regclass) | yes | | | |
| | first_name | varchar | | yes | | | |
| | last_name | varchar | | yes | | | |
| | birth_date | date | | yes | | | |
| | is_manager | bool | false | yes | | | |
| | day_salary | numeric | | yes | | | |
| | department_id | int4 | | yes | | | |
+----+---------------+---------+-----------------------------------------------+---------+---------+ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey Gregoire can you provide me a simple example explaining how to embed/hydrate a nested model (mymodel_id field) using a natural join for example. Searching for best practices, cheer (BTW your lib rocks!)