Subject Re: [firebird-php] What approach are people using? ORM for FB
Author Jochem Maas
myles@... wrote:
>
>
> I have a rather large PHP5 application that I'm building, using a MVC
> approach with Firebird on the back-end for everything. Its incredibly fast,
> and this is a dynamite technology combination!
>
> I'm building classes that represent my data access to Firebird. All of my
> access to the Firebird data is done through Stored Procedures, so I have
> been able to abstract the tables in such a way that represents the 'business
> objects' as best I can to map to PHP classes.
>
> But as I get through this application, I'm starting to find the need to
> create forms that work with multiple classes, or at least forms that work
> with a main class by that the class has a one to many relationship with
> other classes. I'm probably not saying this correctly, as this is more of a
> relational model way of expressing it, but here's a typical example of what
> I'm finding.
>
> I have a 'Job' (Work Order) that is stored in a single table in Firebird,
> and is represented as a single class called 'Job' in PHP. A job can have
> one or many workers assigned to it. There is a class for 'Worker' that is a
> single table in Firebird and a single class in PHP. There is a separate
> table in Firebird representing the relationship between the Job and the
> Workers assigned to it. This resolves a many to many relationship between
> Jobs & Workers.
>
> On my form in HTML, I have the job and all of its properties showing. But I
> need a table within the form that shows all the Workers assigned to it.
>
> When I look at the class of Job, I have singular properties (ie. Scheduled
> date, Priority, etc.). That's all fine. But how do I represent the
> collection of workers who are assigned to it? For me, its easy in a
> relational world because I just create a join table and that's it. But how
> is this represented in PHP as classes?
>
> I'm sure this is pretty rudimentary stuff, but I'm curious as to how other
> PHP-Firebird developers deal with this sort of thing. Are there any good
> reference books on how to 'think' in regards to Object-Relational Mapping so
> that I can follow some 'best practices' for this sort of thing and minimize
> the amount of management code I have to write to pull all of this together?
>
> Thanks in advance for any advice.

I too have an ORM wotsit built with php5/firebird - my tactic involves
defining field classes - each property in a dataobject [subclasses] instance is
actually a field object (these are stored in an array inside the dataobject and
retrieved via __get()/__set() for direct value access or a getField() method
when you need to work with the given field object.

there are a number of different field classes that are subclassed (I have more
field classes then in the example below) as follows:

field (abstract)
realfield
booleanfield
richtextfield
selectfield
referencefield (denotes a foreign key relationship)
virtualfield
assocfield
vectorfield

the interesting ones for you are the assocfield, which models a many-to-many relationship,
and the vectorfield, which models a one-to-many relationship. both these fields
return an array-like object (Iterator, Countable) for their 'value' - the array-like object
contains a collection of dataobjects that represent the data being pointed
to.

in your example I would have a Job and Worker dataobject class and give the Job class
an AssocField that points to the relevant Worker objects based on the configuration of the AssocField
- which ammounts to passing in some config (like join table, primary & foreign key names) into the
ctor of the AssocField (which is called in the ctor of the job class)

this is a very basic description, but may be it gives you an idea.

>
> Regards,
> Myles
>
> ============================
> Myles Wakeham
> Director of Engineering
> Tech Solutions US, Inc.
> Scottsdale, Arizona USA
> Phone (480) 451-7440