1
Dave_L
XoopsObject relationships
  • 2006/5/9 17:35

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


[cross-posted at dev.xoops.org]

Suppose you have two classes derived from XoopsObject (call them A and B) that have a many-to-many relationship. The conventional way to handle this in MySQL is to have one database table for A, one database table for B, and a third database table representing the links between the A's and B's.

How do you handle this within the XoopsObject / XoopsObjectHandler framework? Are there any good examples?

2
wtravel
Re: XoopsObject relationships

What exactly do you want to do with the classes?

3
McNaz
Re: XoopsObject relationships
  • 2006/5/9 18:55

  • McNaz

  • Just can't stay away

  • Posts: 574

  • Since: 2003/4/21


I would think that the member.php class in the kernel is a good example of a module handling a many to many relationhip.

HTH.

Cheers.

McNaz.

4
Dave_L
Re: XoopsObject relationships
  • 2006/5/9 19:18

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


wtravel:

Suppose I have these tables:

CREATE TABLE table1 (
  
id1 smallint(5unsigned NOT NULL auto_increment,
  
data1 varchar(255NOT NULL default '',
  
PRIMARY KEY  (id)
);

CREATE TABLE table2 (
  
id2 smallint(5unsigned NOT NULL auto_increment,
  
data2 DATE NOT NULL default '0000-00-00',
  
PRIMARY KEY  (id)
);

CREATE TABLE link (
  
id1 smallint(5unsigned NOT NULL default '0',
  
id2 smallint(5unsigned NOT NULL default '0',
  
PRIMARY KEY  (id1id2)
);


The third table links the first two.

A typical query might be:

SELECT FROM link AS ln
LEFT JOIN table1 
AS t1 ON t1.id1 ln.id1
LEFT JOIN table2 
AS t2 ON t2.id2 ln.id2
WHERE data1 LIKE 
'foo%' AND data2 '2006-01-15';


How would I implement this using the XoopsObject class?

-----

McNaz:

I glanced through lernel/member.php, but couldn't find any good examples. I'll try again.

5
wtravel
Re: XoopsObject relationships

It depends on what you would like to do with the result. What you could do is add the result to an array and use the array in a fuction that uses the XoopsObject Class. But it really depends on what the class should do .

6
Dave_L
Re: XoopsObject relationships
  • 2006/5/9 19:48

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


Ok, I think what you're saying is that a single XoopsObject should encapsulate all three tables, so the user of the object is not even aware that there are multiple tables involved. The query posted above might still be used, but it would be inside a private class method.

Or there might even be multiple XoopsObject's, if different "views" of the data is needed.

Does that make sense?

7
wtravel
Re: XoopsObject relationships

I think I know what you mean and indeed you are flexible in the way you want to use the query results. You can use multiple classes with the same result array as well.

Login

Who's Online

245 user(s) are online (185 user(s) are browsing Support Forums)


Members: 0


Guests: 245


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Apr 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits