/    Sign up×
Community /Pin to ProfileBookmark

Any tutorials/tips on using object with data from different tables?

I’ve been trying to change my programming style from procedure/function to OOP, and so far so good. However, there is some concept about OO that I’m not quite understand how to do it.

I have no problem coding classes handle data from 1 table, but not with multiple tables. I don’t know how should I make a class to deal with it.

What I have now is something like:

[CODE]
class A{
function getData(){
return $this->result[“field1”];
}
function getData2(){
return $this->result[“field2”];
}
}
class B{
function getData3(){
return $this->result[“B_field1”];
}
[/CODE]

How should I make a class if I want to pull data from table A join with table B? Should I write a new class (like A_? and rewrite all of the function to retrieve all of those fields? or are there any other ways?

What I have thought is creating A and B object in a new class and passing value to both of them to retrieve data, but it’s kind of too many operation. Instead of using a single query (select * from A, B where A.Bid=B.Bid), it would be 2 separate queries…

Thanks for any suggestion ?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 09.2008 — The cleanest way might be to define a view in the DB that does the join, then have a class that uses that view.
Copy linkTweet thisAlerts:
@aquaryauthorAug 09.2008 — In that case, I'll have to write methods to return those fields anyway, right?
Copy linkTweet thisAlerts:
@NogDogAug 09.2008 — It's hard for me to say yes or no that you must do something specific, without knowing specifically what you want to do. There is no law that says a given class must only deal with one and only one database table, nor is there a law that a given class must provide methods to return any db fields. It all depends on what that class's objects are intended to represent and what interface they need to present to prospective client code.
×

Success!

Help @aquary spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...