Re: DB_DataObject: M:N relations

From: Date: Mon, 13 Oct 2003 23:39:48 +0000
Subject: Re: DB_DataObject: M:N relations
References: 1 2  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-22664@lists.php.net to get a copy of this message
In my case I have two tables: clients ------- id name email tel fax . ca. 20+ fields services -------- id name to join these tables, I need an additional table: clients_services ---------------- clients_id services_id db.links.ini may look like: [clients_services] clients_id = clients:id So I can search for clients having service #3 like: $clients_servicesobj->services_id = 3; if ($clients_servicesobj->find()>0) {
    $clientnames = array();
    while($clients_servicesobj->fetch()) {
        $clients_servicesobj->getLinks();
        ... in this case I can do:
        $clientnames[] = $clients_servicesobj->_clients_id->name;
        ...
    }
} .. same way to get the services of client #8 for example What I want is to use FormBuilder to build the "clients" form for me with the standard inputs for name, email, tel etc, and a <select multiple=multiple> (or even multiple checkboxes in a future version) for the client's services. This is a bit hard because the clients table has no corresponding field for that. I have two ideas on how to solve this: 1. This functionality (i.e. creating inputs for M:N relations) should be explicitly requested by using individually altered properties of the DataObject-derived table classes. I mean the same way as specifying dateFields and textFields for FormBuilder in DataObject-derived classes: $table->textFields = array('long_description') causes FormBuilder to generate a <textarea> instead of an <input type=text> for the field named long_description. $clientstable->multipleFields = array(
    array("tablename" => ".."
.... 2. Settings may be stored in the database.links.ini file but I have no idea on the syntax. Maybe the two solutions above should be used combined. Having [clients_services] clients_id = clients:id in the db.links.ini file and using $clientstable->multipleFields = array(
    array("crossLinkTable" => "clients_services",
      // db.links.ini contains the link between
      // the table ($clientstable)
      // and the crossLinkTable (clients_services)
      // ("clients_id = clients:id" see above)
          "crossLink"      => "services_id = services:id")
      // this specifies the link between
      // the crossLinkTable and
      // the linked table (services)
      // both a) crossLinkTable and b) crossLink has
      // to be specified since
      // a) multiple links may refer to the master tables'
      //    id (clients:id) in db.links.ini, so no guessing
      //    is possible
      // b) the crossLinkTable may have multiple links
      //    (foreign keys) set in db.links.ini, so no
      //    guessing is possible
); can be a solution. DataObject and FormBuilder both could use these settings for generating the form / storing the data posted. However, involving DataObject is still considerable since (imho) it was designed to work with one table at a moment ($do->__table) (but it is able to handle links and this is one more reason to implement it here..) Please share all your thoughts with me. Norbert Mocsnik Markus Wolff wrote:
Mocsnik Norbert wrote:
Hi, DB_DataObject and DB_DataObject_FormBuilder can save me up to 30% of development time in one of my projects and I'd like to use them in this one as well as in earlier projects. However, this one needs a <select> input in the form with the possibility to select multiple items at the same time. In relational databases we store this using 1:N - N:1 relations with an additional table between them. (e.g. clients - clients_services - services). My problem is that I don't know if I implement such a feature should it be really in DB_DataObject or another class should be created using multiple DataObject-based components. I think the best would be to implement this in DB_DataObject so it could store the whole "client" form in one turn ($clienttable->insert()), and even DB_DataObject_FormBuilder can use it to generate the corresponding html element..
Mmmh, can you show some example scenario that explains how you want to use this exactly? What kind of element should be created to realize a represantation of an N:N relationship? Regarding the DataObject part, I think Fabien Franzen has been working on some DataObject enhancement that allows such relations pretty easily. Maybe you want to contact him and ask how he did it?
If anyone could help me on how to do this, I'd be happy to implement it in both DB_DataObject and DB_DataObject_FormBuilder and post it to the list. Any ideas are welcome.
I'll surely try :-) CU Markus


« previous php.pear.dev (#22664) next »