Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8164331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:17:39+00:00 2026-06-06T19:17:39+00:00

I’m trying to figure out if I’m using the DAO pattern correctly and, more

  • 0

I’m trying to figure out if I’m using the DAO pattern correctly and, more specifically, how abstract db persistence should be by the time it gets to my mapper classes. I’m using PDO as the data-access abstraction object, but sometimes I wonder if I’m trying to abstract the queries too much.

I’ve just included how I’m abstracting select queries, but I’ve written methods for all of the CRUD operations.

class DaoPDO {

    function __construct() {
    
        // connection settings
        $this->db_host   = '';
        $this->db_user   = ''; 
        $this->db_pass   = ''; 
        $this->db_name   = '';
        
        
    }

    function __destruct() {
    
        // close connections when the object is destroyed
        $this->dbh = null;
    
    } 
    

    function db_connect() {
    
        try { 
        
            /**
             * connects to the database -
             * the last line makes a persistent connection, which
             * caches the connection instead of closing it 
             */
            $dbh = new PDO("mysql:host=$this->db_host;dbname=$this->db_name", 
                            $this->db_user, $this->db_pass, 
                            array(PDO::ATTR_PERSISTENT => true));

            
            return $dbh;
        
        } catch (PDOException $e) {
        
            // eventually write this to a file
            print "Error!: " . $e->getMessage() . "<br/>";
            die();
        
        }

    
    } // end db_connect()'


    
    function select($table, array $columns, array $where = array(1=>1), $select_multiple = false) {
    
        // connect to db
        $dbh = $this->db_connect();
        
        $where_columns  = array();
        $where_values   = array();
        
        foreach($where as $col => $val) {
        
            $col = "$col = ?";
        
            array_push($where_columns, $col);
            array_push($where_values, $val);
        
        }
        
        
        // comma separated list
        $columns = implode(",", $columns);

        // does not currently support 'OR' arguments
        $where_columns = implode(' AND ', $where_columns);
        

        
        $stmt = $dbh->prepare("SELECT $columns
                               FROM   $table
                               WHERE  $where_columns");
                               
                    
        $stmt->execute($where_values);
        
        if (!$select_multiple) {
        
            $result = $stmt->fetch(PDO::FETCH_OBJ);
            return $result;
        
        } else {
        
            $results = array();
        
            while ($row = $stmt->fetch(PDO::FETCH_OBJ)) {
            
                array_push($results, $row);
            
            }
            
            return $results;
        
        }

            
    
    } // end select()

    
} // end class

So, my two questions:

  1. Is this the correct use of a DAO, or am I misinterpreting its purpose?

  2. Is abstracting the query process to this degree unnecessary, or even uncommon? Sometimes I feel like I’m trying to make things too easy…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T19:17:41+00:00Added an answer on June 6, 2026 at 7:17 pm

    It looks more like you’re building a persistence abstraction layer on top of PDO (which is itself a persistence layer) rather than a data access object. While there are many forms that a DAO can take, the goal is to separate your business logic from the persistence mechanism.

      Business Logic
            |
            v
    Data Access Object
            |
            v
     Persistence Layer
    

    A DAO with db_connect and select is too closely modeled after the persistence layer. The simplest form of a generic DAO is to provide the basic CRUD operations at an object level without exposing the internals of the persistence mechanism.

    interface UserDao
    {
        /**
         * Store the new user and assign a unique auto-generated ID.
         */
        function create($user);
    
        /**
         * Return the user with the given auto-generated ID.
         */
        function findById($id);
    
        /**
         * Return the user with the given login ID.
         */
        function findByLogin($login);
    
        /**
         * Update the user's fields.
         */
        function update($user);
    
        /**
         * Delete the user from the database.
         */
        function delete($user);
    }
    

    If your business objects are the underlying PDO model objects, you can return them from the DAO. Be aware that depending on the underlying persistence mechanism you choose, this may not be ideal. I haven’t worked with PDO but assume it’s similar to other ORM tools that produce standard PHP objects without tying the business logic to the PDO API. So you’re probably okay here.

    If you were implementing persistence by accessing the mysqli library directly, for example, you would want to copy data to/from the result sets into your own model objects. This is the job of the DAO to keep it out of the business logic.

    By using an interface for the DAO you can now implement it for different persistence frameworks: PDO, Doctrine, raw SQL, whatever. While you’re unlikely to switch methods mid-project, the cost of using an interface is negligible compared to its other benefits, e.g. using a mock in unit testing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.