Jump to content

Recommended Posts

This is a question that keeps bottering me all the time, I have never had any response... Basically I am trying to abstract the hole database access of my program and make it a sort of 3-tyer.

 

i have a class called clssUser, wich basically has all the fields from the database table tab_users. it is something like:

 

class User

{

private $id;

private $fields;

 

public function __construct()

{

$this->id = null;

$this->fields = array(

'name' => '',

'age' => ''

);

}

 

public function __get($field)

{

if ($field == 'id')

{

return $this->id;

}

else

{

return $this->fields[$field];

}

}

 

public function __set($field, $value)

{

if (array_key_exists($field, $this->fields))

{

$this->fields[$field] = $value;

}

}

 

i also have a class called cllsProject, wich basically has all the fields from the database table tab_projects (every user has a project). it starts a bit different, tough:

 

...

private $id;

private $fields;

 

public function __construct()

{

$this->id = null;

$this->fields = array(

'id_user' => null,

'name' => ''

);

}

...

 

i would normally do the same for other of information i have to store (categories, rewards, ...).

A classe called clssDbCommunication wich does all the access to the database so further on I can do something like:

 

$dbc = new DBCommunication();

$user = new User($dbc);

$user->name = 'John';

$user->age = 35;

$user.save(); // will include or update accordinly. updates it's id.

 

$project = new Project($dbc);

$project->id_user = $user->id;

$project->name = 'Johns Project';

$project.save();

 

Technically speaking does it make sense to have the access code in clssUser ou clssDBCommunication? Should I do

$user.Save() or something like $dbc->AddUser($user)? Is this a good aproach in order to "abstract" the SQL in terms

of objects?

 

If I want to get a list of users, would I need another object let's say clssListUsers or the best would be to encapsulate the hole dataset onto clssUsers so i could do Users.next, Users.last and them read the values of the

current user? And how would you do this recordset encapsulation?

 

This is a harry issue I know but everytime I am coding I ended up with a lot of SQL, PHP, HTML everywhere and it just

make's sense that on my HTML i would just have PHP code doing basic layout decisions, on PHP I would have all the

code but all SQL method's would be encapsulated and centralized elsewhere...

 

how do you guys approach such problem on your every day programming life?

 

Thanks for any help,

Murilo

 

 

Link to comment
https://forums.phpfreaks.com/topic/228706-php-sqloo-abstraction-doubt/
Share on other sites

From briefly looking at what you are asking I think you should read up on design patterns, and in your case I think MVC ( Model, View, Controller) would best suit your needs. Using this approach allows you to logically separate the business logic (models) from the HTML ( view) by using a controller to request the data from the model and pass it to the view.

 

 

 

 

ActiveRecord is a common pattern to use for simple projects. You could abstract the ActiveRecord and use a Repository to actually retrieve/store the ActiveRecord's.

 

$repo = new UserRepository();
$user = $repo->find(1);
$user->firstname = 'John';
$user->lastname = 'Doe';
$repo->store($user);

 

This way the ActiveRecord doesn't need to know about the DB table structure or have a DB connection. A possible implementation for find() could be:

 

public function find($id) {
    // get the record from DB
    $record = new ActiveRecord($this->_columns);
    $record->populate($rowData);
    return $record;
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.