Skip to content

Instantly share code, notes, and snippets.

@chanmix51
Last active August 29, 2015 14:09
Show Gist options
  • Save chanmix51/3f47f8c2e4440f53daee to your computer and use it in GitHub Desktop.
Save chanmix51/3f47f8c2e4440f53daee to your computer and use it in GitHub Desktop.
Pomm 2 examples
<?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'
]
]);
<?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'));
}
}
<?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')
;
}
}
$ 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'.
<?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, ',', ' ')
);
}
$ 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 | |
+----+---------------+---------+-----------------------------------------------+---------+---------+
@brikou
Copy link

brikou commented Apr 13, 2015

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!)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment