Initial values from Database

19 views
Skip to first unread message

pa...@egallys.com

unread,
Jul 23, 2014, 1:40:27 AM7/23/14
to pommp...@googlegroups.com
Hello,


What about initialize default value of an BaseObject from default value in database schema ?

Table A schema:
- id serial
- field_1 integer not null default 123
- field_2 varchar
- field_3 boolean not null default 't'

$object = new A();

$object
->getField1() === 123;
$object
->getField2() === null;
$object
->getField3() === false

For now :
$object = new A();
$object
->getField1() // throws an Exception
From the table schema, $object must have a field_1 value, 123 if nothing was set.



Another question, Will a PommEntityManager break the Pomm philosophy ?

For now we are doing
$a = $connection->getMapFor('\EntityA')->findByPk(...)

So to get $a we must know $connection.
If we have to find entityA from connection1 entityB from connection2 and entityC from connection3, we must have $connection1, $connection2 and $connection3

But with PommEntityManager:
$a = $pommEntityManager->getMapFor('\EntityA')->findByPk(...);
$b = $pommEntityManager->getMapFor('\EntityB')->findByPk(...);
$c = $pommEntityManager->getMapFor('\EntityC')->findByPk(...);

$d = new \EntityD();

$pommEntityManager->save($d);

$a->setField1(12345);
$pommEntityManager->save($a);

 
The PommEntityManager will be in charge of getting the right Pomm Connection and Pomm Database from a BaseObject classname.

Grégoire Hubert

unread,
Jul 23, 2014, 3:28:03 AM7/23/14
to pommp...@googlegroups.com
On 7/23/14, pa...@egallys.com <pa...@egallys.com> wrote:
> Hello,

Hello,

>
> What about initialize default value of an BaseObject from default value in
> database schema ?
>
> Table A schema:
> - id serial
> - field_1 integer not null default 123
> - field_2 varchar
> - field_3 boolean not null default 't'
>
> $object = new A();
>
> $object->getField1() === 123;
> $object->getField2() === null;
> $object->getField3() === false
>

Since entities are hydrated with the result of a projection operation
(SELECT, RETURNING), which can be overrided, they cannot be statically
bound with a structure thus default values. This is why entities are
schemaless data containers with no knowledge of database nor
persistence.

> Another question, Will a PommEntityManager break the Pomm philosophy ?
>
> For now we are doing
> $a = $connection->getMapFor('\EntityA')->findByPk(...)
>
> So to get $a we must know $connection.
> If we have to find entityA from connection1 entityB from connection2 and
> entityC from connection3, we must have $connection1, $connection2 and
> $connection3
>
> But with PommEntityManager:
> $a = $pommEntityManager->getMapFor('\EntityA')->findByPk(...);
> $b = $pommEntityManager->getMapFor('\EntityB')->findByPk(...);
> $c = $pommEntityManager->getMapFor('\EntityC')->findByPk(...);
>
> $d = new \EntityD();
> $pommEntityManager->save($d);
>
> $a->setField1(12345);
> $pommEntityManager->save($a);
>
>
> The PommEntityManager will be in charge of getting the right Pomm
> Connection and Pomm Database from a BaseObject classname.

An entity manager is -- surprisingly -- entity centric. In order to
make you benefit from relational goodness, Pomm is mapping oriented.
Most of its logic is defined in the Map classes.
Aside of that, your point of not having to deal with connections is
good, I am going to open a feature request concerning that point.

Cheers,
Grégoire

> --
> You received this message because you are subscribed to the Google Groups
> "PommProject" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to pommproject...@googlegroups.com.
> Visit this group at http://groups.google.com/group/pommproject.
> For more options, visit https://groups.google.com/d/optout.
>


--
Grégoire HUBERT
Pomm : http://www.pomm-project.org

pa...@egallys.com

unread,
Jul 23, 2014, 3:56:23 AM7/23/14
to pommp...@googlegroups.com
Hello,

Since entities are hydrated with the result of a projection operation
(SELECT, RETURNING), which can be overrided, they cannot be statically
bound with a structure thus default values. This is why entities are
schemaless data containers with no knowledge of database nor
persistence.

Maybe, do not throw the exception, if the $object->isNew() === true ?
 
An entity manager is -- surprisingly -- entity centric. In order to
make you benefit from relational goodness, Pomm is mapping oriented.
Most of its logic is defined in the Map classes.
Aside of that, your point of not having to deal with connections is
good, I am going to open a feature request concerning that point.


PommEntityManager is not the right name.
What i'm mean is a Database centric.
It deals with all Pomm Databases, and maybe proxified some BaseObjectMap methods

getMapFor() could be (ugly way) : 
public function getMapFor($class) {
   
return $this->getConnectionForClass($class)->getMapFor($class);
}

save($entity) could be (ugly way)  :
public function save(BaseObject $entity) {
   
return $this->getConnectionForClass(get_class($entity))->getMapFor(get_class($entity))->saveOne($entity);
}

Each $map->action($entity) could be proxified by the manager like
$manager->action($entity);

//with in Manager
function action($entity) {
   
return $this->getConnectionForClass(get_class($entity))->getMapFor(get_class($entity))->action($entity)
}

 

Grégoire Hubert

unread,
Jul 23, 2014, 6:18:29 AM7/23/14
to pommp...@googlegroups.com
On 7/23/14, pa...@egallys.com <pa...@egallys.com> wrote:
IMHO the Service class should be responsible for that. I am not very
keen on hidding classes under a "magical" mechanism that can save or
call methods for you. Maybe I am wrong.

Cheers,
Reply all
Reply to author
Forward
0 new messages