SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Database Objects Question

    If I have a basic database class, is this (below) the proper way to create a database object in other classes? IE. Should I create an instance in the construct and then will it be available elsewhere?

    Also, how would I call the below in usage? Would I just be able to create the MyClass object or do I have to create a Database object first?

    Thanks in advance for any helps (I'm relatively new with classes that aren't built for me with frameworks!).

    PHP Code:
    class MyClass {
        private 
    $db;
        
        function 
    __construct() {
            
    $this->db = new Database();
        }

        function 
    createSomething($attribute) {
            
    $this->db->query("INSERT INTO something (attribute) VALUES $attribute");
        }



  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I normally do something like this:
    PHP Code:
    class MyClass extends Database 
        
        function 
    __construct() { 
            
    parent::__construct();
        } 

        function 
    createSomething($sql) { 
            return 
    $this->query($sql); 
        }

    $obj = new MyClass;
    $obj->createSomething("INSERT INTO tblename(field1,field2) VALUES('$val1', '$val2')"); 
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Enthusiast
    Join Date
    Jan 2006
    Posts
    97
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug View Post
    I normally do something like this:
    PHP Code:
    class MyClass extends Database 
        
        function 
    __construct() { 
            
    parent::__construct();
        } 

        function 
    createSomething($sql) { 
            return 
    $this->query($sql); 
        }

    $obj = new MyClass;
    $obj->createSomething("INSERT INTO tblename(field1,field2) VALUES('$val1', '$val2')"); 
    Thank you for the help!

    Extending sounds like a better way to do it...

  4. #4
    SitePoint Guru
    Join Date
    Jun 2006
    Posts
    638
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have it in a more generic way:
    PHP Code:
    # main class
    abstract class DTO {
      public 
    $primaryKey null# holds the pk
      
    protected $_data = array(); # holds the data to be changed / get from db

      
    public function __constructor($id null) {
        if (
    $id) {
          
    $this->_data DB::selectRow("SELECT * FROM " get_called_class() . " WHERE id = '".$id."'");
          
    $this->primaryKey $id;
        }
      }

      public function 
    save() {
        
    $values = array();
        foreach (
    $this->_data as $key => $value) {
          
    $values[] = $key " = '" $value "'"# escape this if need be
        
    }
        if (
    $this->primaryKey !== null) {
          
    DB::update("UPDATE " get_called_class() . " SET " join(', '$values) . " WHERE id = '".$this->primaryKey."'");
        } else {
          
    DB::insert("INSERT INTO " get_called_class() . " SET " join(', '$values) );
        }
      }

      public function 
    __set($key$value) {
        
    $this->_data[$key] = $value;
      }

      public function 
    __get($key) {
        return 
    $this->_data($key);
      }
    }

    # Models / DTOs / whatever you want to call em
    class User extends DTO {

    }

    class 
    Foo extends DTO {

    }

    # Usage
    $user = new User();
    $user->name 'bob';
    $user->age 15;
    $user->save(); # INSERT INTO user SET name = 'bob', age = '15';

    $otherUser = new User(5); # SELECT * FROM user WHERE id = 5;
    $otherUser->name 'not bob';
    $otherUser->save(); # UPDATE user SET name = 'not bob' WHERE id = '5'; 
    That way, I almost never have to write any SQL.

  5. #5
    SitePoint Wizard frank1's Avatar
    Join Date
    Oct 2005
    Posts
    1,392
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Vali View Post
    I have it in a more generic way:
    PHP Code:
    # main class
    abstract class DTO {
      public 
    $primaryKey null# holds the pk
      
    protected $_data = array(); # holds the data to be changed / get from db

      
    public function __constructor($id null) {
        if (
    $id) {
          
    $this->_data DB::selectRow("SELECT * FROM " get_called_class() . " WHERE id = '".$id."'");
          
    $this->primaryKey $id;
        }
      }

      public function 
    save() {
        
    $values = array();
        foreach (
    $this->_data as $key => $value) {
          
    $values[] = $key " = '" $value "'"# escape this if need be
        
    }
        if (
    $this->primaryKey !== null) {
          
    DB::update("UPDATE " get_called_class() . " SET " join(', '$values) . " WHERE id = '".$this->primaryKey."'");
        } else {
          
    DB::insert("INSERT INTO " get_called_class() . " SET " join(', '$values) );
        }
      }

      public function 
    __set($key$value) {
        
    $this->_data[$key] = $value;
      }

      public function 
    __get($key) {
        return 
    $this->_data($key);
      }
    }

    # Models / DTOs / whatever you want to call em
    class User extends DTO {

    }

    class 
    Foo extends DTO {

    }

    # Usage
    $user = new User();
    $user->name 'bob';
    $user->age 15;
    $user->save(); # INSERT INTO user SET name = 'bob', age = '15';

    $otherUser = new User(5); # SELECT * FROM user WHERE id = 5;
    $otherUser->name 'not bob';
    $otherUser->save(); # UPDATE user SET name = 'not bob' WHERE id = '5'; 
    That way, I almost never have to write any SQL.
    i do almost similar but instead of it in php >5.3 i make those function static so that function sticks around through we dont have object (created) which eases task in many situation.
    plus i was having problem earlier in date_time function because of 'NOW()' in the generic insert function which i solved later....


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •