PEAR:DB_DataObjects 'better' way question
| From: | David Ball | Date: | Tue, 13 Feb 2007 15:11:05 +0000 |
| Subject: | PEAR:DB_DataObjects 'better' way question | ||
| Groups: | php.pear.general | ||
| Request: | Send a blank email to pear-general+get-26560@lists.php.net to get a copy of this message | ||
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();
}
--
-- Table structure for table auth
--
CREATE TABLE auth (
user_id bigint(20) unsigned NOT NULL auto_increment,
username varchar(50) NOT NULL,
password varchar(32) NOT NULL default '',
access varchar(15) character set ascii NOT NULL default 'public',
email varchar(30) NOT NULL,
represents varchar(30) NOT NULL,
first_name varchar(30) NOT NULL,
last_name varchar(30) NOT NULL,
active tinyint(1) NOT NULL,
last_updated datetime NOT NULL,
PRIMARY KEY (user_id),
UNIQUE KEY username (username),
KEY password (password)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;
--
-- Dumping data for table auth
--
INSERT INTO auth (user_id, username, password,
access,
email, represents, first_name, last_name,
active,
last_updated) VALUES
(1, 'admin', '<removed>', 'admin', '<removed>',
'The Gap', 'asdf',
'sadf', 1, '2007-02-14 01:01:29'),
(3, 'manager', '<removed>', 'manager',
'<removed>', 'PowerPlant',
'Michael', 'Smith', 1, '2007-03-10 19:10:08'),
(4, 'dball', '<removed>', 'manager',
'<removed>', 'The Gap', 'David',
'Gates', 1, '2007-02-13 18:23:51');
-- --------------------------------------------------------
--
-- Table structure for table represents
--
CREATE TABLE represents (
represents_id bigint(20) unsigned NOT NULL auto_increment,
name varchar(30) NOT NULL,
active tinyint(1) NOT NULL,
last_updated datetime NOT NULL,
PRIMARY KEY (represents_id),
UNIQUE KEY represents (name)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
--
-- Dumping data for table represents
--
INSERT INTO represents (represents_id, name,
active,
last_updated) VALUES
(1, 'The Gap', 1, '2007-02-13 22:01:42'),
(2, 'PowerPlant', 1, '2007-02-14 01:27:13');
PHP/SMARTY OUTPUT
ID Represents Usernames Active
1 The Gap admin dball 1 edit
2 PowerPlant manager 1 edit
Thanks,
David