Re[2]: [PEAR] DB_DataObject 2 joins to same table
| From: | Leo Márquez | Date: | Mon, 29 Nov 2004 08:49:17 +0000 |
| Subject: | Re[2]: [PEAR] DB_DataObject 2 joins to same table | ||
| References: | 1 2 | Groups: | php.pear.general |
| Request: | Send a blank email to pear-general+get-15889@lists.php.net to get a copy of this message | ||
Hi Justin and thank you for your answer.
Yo are ok, use may be a column name, not a member of dataobject.
I have changed to factory instantation instead new Dataobect but my
problem is still present.
I will try to explain that I want to do again. Perhaps this time I'm
more clean.
If I have this tables
[USER]
user_id (pk)
user_name
user_firstcar
user_secondcar
[CAR]
car_id (pk)
car_name
car_color
car_model
and I want to retrieve all the users of the database but with the car
name instead of the car_id I have to use joins but...
How knows the user object what column links to each instance of car?
If I do:
$c1=DB_DataObject::factory('CAR');
$c2=DB_DataObject::factory('CAR');
$user=DB_DataObject::factory('USER');
$user->joinAdd($c1);
$user->joinAdd($c2);
How knows the user instance that this last join is for
the user_secondcar column? How can I set it?
The SQL resultant query I want is:
SELECT * , CAR.car_name as c1_car_name ,
CAR.car_name as c2_car_name
FROM USER
INNER JOIN CAR AS CAR ON CAR.car_id=USER.user_firstcar ,
CAR AS c2 WHERE c2.car_id=USER.user_secondcar
And now I have:
SELECT * , CAR.car_name as c1_car_name ,
CAR.car_name as c2_car_name
FROM USER
INNER JOIN CAR AS CAR ON CAR.car_id=USER.user_firstcar ,
CAR AS c2 WHERE c2.car_id=USER.user_firstcar <--- I want user_secondcar here!!!
In the links.ini I have:
[USER]
USER_FIRSTCAR=CAR:CAR_ID
USER_SECOND=CAR:CAR_ID
The thing I want to do Is possible with dataobject and joins?
Thanks!
Friday, November 26, 2004, 10:51:19 PM, vas escriure:
JP> On Fri, 26 Nov 2004 14:16:50 +0100, Leo Márquez <leo@calidae.net> wrote:
>> Hi!,
>>
>> Hi have a tables relationship like the
>> 'Automatic Table Linking and Joins' example at PEAR DB_DAtaObject
>> Documentation. One table that have two columns that links to the same
>> table and the same column. For example:
>>
>> table USER
>> user_id
>> user_name
>> user_firstcar
>> user_secondcar
>>
>> table CAR
>> car_id
>> car_model
>> car_color
>>
>> on mydatabase.links.ini
>>
>> [USER]
>> user_firscar=CAR:car_id
>> user_secondcar=CAR:car_id
>>
>> In the PEAR doc example I see:
>>
>> ...
>> $car->use = 'first';
>> ...
>>
>> I have been looking for the 'use' attribute in doc of DB_DAtaObject
>> and DB and not have seen it. I understand that in my code I should
>> type:
JP> Could you please give us a link to said documentation? I've never
JP> heard of a "use" member variable in DB_DataObject. In fact, I'm
JP> absolutely sure it doesn't exist as it would conflict with column
JP> names.
>>
>> $car->use='user_firstcar' for the first join
>> and
>> $car2->use='user_secondcar' for the first join
>>
>> and the selectAs, etc.
>>
>> I have tried it and not work, always see the first instance of car. My
>> code is here:
>>
>> $mo=new MAGATZEM();
JP> You should really be using factory here.
JP> $mo = DB_DataObject::factory('MAGATZEM');
>> $mo->use='MAGATZEM_ORIGEN';
>>
>> $md=new MAGATZEM();
>> $md->use='MAGATZEM_DESTI';
>>
>> $dob->joinAdd($mo);
>> $dob->selectAs($mo,'mo_%s');
>>
>> $dob->joinAdd($md,'','md');
>> $dob->selectAs($md,'md_%s');
>>
>> $dob->find();
>>
>> Thanks about any help.
>>
--
Salutacions,
Leo Márquez
[ Enginyeria del software / Internet ]
[ Calidae - www.calidae.com ]