For various reasons (like implementing extended validation with
specifications) i decided to extend DoctrineRecord. So assuming i have
the entity/class called Person, it would inherit from BasePerson (the
default behavior when autogenerated), and BasePerson would inherit
from My_Doctrine_Record. And of course, My_Doctrine_Record, inherits
from DoctrineRecord. So i just added a middle class, and did not let
BasePerson extend Doctrine_Record directly as it normally happens.
Now in Person, i have the setUp function where i add some entries into
an array, and the array is a member of Person. In order to retrieve a
Person i use this: $person = Doctrine::getTable( 'person' )->find
( (int)$id ); Everything works, the person is fetched, and i can play
with it, BUT, the array populated with the setUp method is empty!!
After some debug, it was clear that the array was populated correctly,
so the only explanation remaining was that the Person, was somehow
cloned, and the array got empty in the process. I debugged further,
overriding the constructor in My_Doctrine_Record, and added an "echo('
instance ')". In the setUp method of Person, i added "echo(' set-up
')";
So here's what was printed on the screen, when fetching a Person with
find: instance instance set-up instance.
Which means there were 3 instances of Person created, and only the
second one got it's setUp method called, and it was pretty obvious
that i was getting the last instance, since setUp was not called, and
my array was thus empty.
In the hope i explained things clearly, can anyone tell me why this is
happening, and how to fix it? I don't care that the class Person is
instantiated 3 times (rather wasteful and redundant i would say), but
i do care about my setUp method being called, or having the object
cloned properly (if cloning occurs on the third instance). In short i
need to be able to have that array member i was describing above (or
any member for that matter), keep it's values. Btw, the array member
is called $_specifications, so there isn't a naming conflict with one
of Doctrine_Record's members.
The setUp() is only called once, to setup the schema information for the
model. it isn't called for every instance of the model created. i think you
need to use something like construct()
On Tue, Jul 7, 2009 at 1:09 PM, Alex Farcas <alex.far...@gmail.com> wrote:
> Hey guys,
> For various reasons (like implementing extended validation with
> specifications) i decided to extend DoctrineRecord. So assuming i have
> the entity/class called Person, it would inherit from BasePerson (the
> default behavior when autogenerated), and BasePerson would inherit
> from My_Doctrine_Record. And of course, My_Doctrine_Record, inherits
> from DoctrineRecord. So i just added a middle class, and did not let
> BasePerson extend Doctrine_Record directly as it normally happens.
> Now in Person, i have the setUp function where i add some entries into
> an array, and the array is a member of Person. In order to retrieve a
> Person i use this: $person = Doctrine::getTable( 'person' )->find
> ( (int)$id ); Everything works, the person is fetched, and i can play
> with it, BUT, the array populated with the setUp method is empty!!
> After some debug, it was clear that the array was populated correctly,
> so the only explanation remaining was that the Person, was somehow
> cloned, and the array got empty in the process. I debugged further,
> overriding the constructor in My_Doctrine_Record, and added an "echo('
> instance ')". In the setUp method of Person, i added "echo(' set-up
> ')";
> So here's what was printed on the screen, when fetching a Person with
> find: instance instance set-up instance.
> Which means there were 3 instances of Person created, and only the
> second one got it's setUp method called, and it was pretty obvious
> that i was getting the last instance, since setUp was not called, and
> my array was thus empty.
> In the hope i explained things clearly, can anyone tell me why this is
> happening, and how to fix it? I don't care that the class Person is
> instantiated 3 times (rather wasteful and redundant i would say), but
> i do care about my setUp method being called, or having the object
> cloned properly (if cloning occurs on the third instance). In short i
> need to be able to have that array member i was describing above (or
> any member for that matter), keep it's values. Btw, the array member
> is called $_specifications, so there isn't a naming conflict with one
> of Doctrine_Record's members.
> Alex
-- Jonathan H. Wage (+1 415 992 5468)
Open Source Software Developer & Evangelist
sensiolabs.com | jwage.com | doctrine-project.org | symfony-project.org
You can contact Jonathan about Doctrine, Symfony and Open-Source or for
training, consulting, application development, or business related questions
at jonathan.w...@sensio.com
Thanks for the response Jon.
I replaced setUp with consturct and now everything works fine. I am
still concerned about the 3 instances, because for each instance, i
will load all the specifications for that object. Currently there are
few of them(as i'm just starting up), but as the domain model evolves,
i'm sure i will be having entities with 10-20+ specifications. So i
will end up with 30-60+ spec instances per entity!!
Is there a way to integrate them in a more clever manner, like
validators?