Re: PEAR:DB_DataObjects 'better' way question
| From: | David Sanders | Date: | Wed, 14 Feb 2007 02:47:09 +0000 |
| Subject: | Re: PEAR:DB_DataObjects 'better' way question | ||
| References: | 1 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-26567@lists.php.net to get a copy of this message | ||
What was the problem with joining? It should fetch all the users that match the represents column. You'd only get one loop, but you'd also need to keep track of when the 'represents' changes from 'The Gap' to 'Powerplant'. Personally I'd keep the 2 loops.
As a couple of side notes:
You aren't escaping $represents->name. This happens automatically if you do
$users->represents = $represents->name;
instead of using the whereAdd() call.
Also you're definining your foreign keys on names and not IDs. I dunno what 'The Gap' and 'Powerplant' are but if they ever change, you'd need to update all the foreign keys in all the tables that reference 'represents'.
David Ball wrote:
Hi,
First I would like to say what a great package PEAR is, thanks for all your work. Anyways my question is how to do in the following situation. It could be obvious but I am new to php/SQL and wasn't sure what to even search/google for.
I have this feeling that there is a 'better' way of doing the following bit of code (the table definition and output follows). I am generating an array of all the rows in the table represents and putting them in a variable for smarty. I am also looking for all the users (could be many) that have the current loops represents name. (ie all the users where users->represents = represents->name) Joins and Links seem only for when there are uniques in both.
Any ideas or help please? Or am I going about this completely wrong?
$represents = DB_DataObject::factory('represents');
$users_temp = DB_DataObject::factory('auth');
$represents->find();
while ($represents->fetch()) {
$users = clone $users_temp;
$user_string = "";
$users->whereAdd("represents = '".$represents->name."'");
$users->find();
while ($users->fetch()) {
$user_string .= $users->username." "; }
$smarty->append('represents', array(
'ID' => $represents->represents_id,
'REPRESENTS' => $represents->name,
'USERNAME' => $user_string,
'ACTIVE' => $represents->active
));
$users->whereAdd();
}
--
David Sanders
Software Engineer
Baseline Solutions Pty Ltd
Suite 201 832 High St
East Kew VIC 3123 Australia
Phone: (03) 9249 9687 Fax: (03) 9249 9600
http://baselinesolutions.com.au
========================================================================
This e-mail contains privileged and confidential information. If you are
not the intended recipient of this e-mail, you are hereby notified that
you must not disseminate, copy or take any action in reliance on it.
Please notify Baseline Solutions Pty Ltd via e-mail
(security@baselinesols.com) if you are not the intended addressee of
this e-mail transmission. Any views or opinions presented in this
message are solely those of the author.
========================================================================