Re: PEAR:DB_DataObjects 'better' way question
| From: | David Sanders | Date: | Thu, 15 Feb 2007 00:06:15 +0000 |
| Subject: | Re: PEAR:DB_DataObjects 'better' way question | ||
| References: | 1 2 3 4 5 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-26580@lists.php.net to get a copy of this message | ||
With the joinAdd way, you need to watch when represents changes so you can
concat your users array then append it to smarty when it changes.
But as I said, I'd just use 2 loops.
> Hi Again,
>
> Thanks! Just so I have it clear... (note that I took your suggestion and
> modified the auth table to use represents_id as apposed to the name)
>
> *******
> The following bit of code only returns one of the usernames (as shown)
> that have that represents value and you can't use it to get more than one?
> ID Represents Usernames Active
> 1 The Gap admin 1
> 2 PowerPlant dball 1
>
> $represents = DB_DataObject::factory('represents');
>
> $represents->find();
> while ($represents->fetch()) {
>
> $users = $represents->getLink('represents_id', 'auth',
> 'represents');
> $smarty->append('represents', array(
> 'ID' => $represents->represents_id,
> 'REPRESENTS' => $represents->name,
> 'USERNAME' => $users->username,
> 'ACTIVE' => $represents->active
> ));
> }
>
> *******
> This bit of code finds them all but returns them as three rows as shown
> and it can't combine them (automatically) like desired.
> ID Represents Usernames Active
> 1 The Gap admin 1
> 1 The Gap manager 1
> 2 PowerPlant dball 1
>
> $represents = DB_DataObject::factory('represents');
> $users = DB_DataObject::factory('auth');
> $represents->selectAs();
>
> $represents->joinAdd($users);
> $represents->selectAs($users, 'users_%s');
>
> $represents->find();
>
> while ($represents->fetch()) {
>
> $smarty->append('represents', array(
> 'ID' => $represents->represents_id,
> 'REPRESENTS' => $represents->name,
> 'USERNAME' => $represents->users_username,,
> 'ACTIVE' => $represents->active
> ));
> }
>
> So my original code was probably one of the better ways of doing this?
> Or would using and parsing the ->joinAdd() table result be better as
> only one database call?
>
> Thanks again,
> David
>
> David Sanders wrote:
>> Ah ok,
>>
>> joinAdd() and getLink() are 2 separate ways to use foreign keys.
>>
>> Use joinAdd() when you want to join tables together in a query, before
>> you find().
>>
>> Use getLink() after you've called fetch() for a table with a foreign key
>> to find its corresponding row in the 'parent' table. Note getLink()
>> will resolve a foreign key and not vice versa (ie you can't find all the
>> rows in other tables that'd reference this table). So in your case
>> you'd call getLink() only on your users table to retrieve the
>> corresponding row in the represents table.
>>
>>
>> David Ball wrote:
>>> Dear David,
>>>
>>> Thanks for your reply. Thanks for the tips about escaping and the
>>> foreign keys....I'll fix those.
>>>
>>> Here is my join code (ie trying to implement the same thing as the
>>> loop) but it doesn't return any usernames and has the following debug
>>> error "DataObjects_Represents: ERROR: getLink: row not set name". I
>>> guess there is something I don't understand here. I also don't see how
>>> this is going to return multiple usernames either? And I guess this
>>> way is better (if it works) for performance reasons?
>>>
>>> $represents = DB_DataObject::factory('represents');
>>> $represents->selectAs();
>>>
>>> $users = $represents->getLink('name', 'auth',
>>> 'represents');
>>> $represents->joinAdd($users);
>>> $represents->selectAs($users, 'users_%s');
>>>
>>> $represents->find();
>>> while ($represents->fetch()) {
>>> $smarty->append('represents', array(
>>> 'ID' => $represents->represents_id,
>>> 'REPRESENTS' => $represents->name,
>>> 'USERNAME' => $represents->users_username,
>>> 'ACTIVE' => $represents->active
>>> ));
>>> }
>>>
>>> Thanks again!
>>>
>>> Cheers,
>>> David
>
> --
> PEAR General Mailing List (http://pear.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
--
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
========================================================================
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.
========================================================================