开发者

How to implement listing of items using design paterns with php nad DB?

开发者 https://www.devze.com 2022-12-20 13:29 出处:网络
I have the following problem with listing data from DB. Suppose we have table \"client\" in DB with fields:

I have the following problem with listing data from DB. Suppose we have table "client" in DB with fields:

id_client
username
password
last_login

query is:

SELECT username,password,last_login FROM client ORDER BY id_client LIMIT 20,20

Normal procedural listing of clients would be:

/* connect to DB using mysqli */

if ($result = $mysqli->query("SELECT username,password,last_login FROM 开发者_StackOverflowclient ORDER BY id_client LIMIT 20,20")) 
{
    while ($row = $result->fetch_row()) {
        printf (" -- %s (%s) -- ", $row[0], $row[1]);
    }
    $result->close();
}

But I want to impement it using desing pattern and my initial code would be:

class client 
{
   private id_client,
   username,
   password,
   last_login,

  ...

}

How to implement client class (and other classes) to perform query request for whole set of clients (not one by one) and list them with one shot(one query) using design patterns? Which pattern should I use?

Greetings


I guess you are looking for an ORM. There are several:

  • Doctrine
  • Propel
  • (CakePHP also has one built in)

The CakePHP data layer is an implementation of the ActiveRecord pattern. Cake will read your DB schema and automagically provide $Model->field and some auto getters/setters (like findAllById or findByName).

Here is the CakePHP documentation.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号