Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post

    Active record pattern issues

    Hiya, just threw together a generic active record class and I've sort of got a bit of coder's block... Just can't see my next step ...

    Basically my problem is binding it to a row, how should I handle it?

    Code:
    public function commit(){
    	if($this->prv_rowBound){
    		$this->prv_dbInst->query(sql::update($this->prv_table, $this->prv_vars, [...]));
    	}else{
    		$this->prv_dbInst->query(sql::insert($this->prv_table, $this->prv_vars));
    	}
    }
    The sql helper, update, has to take a primary key.

    If the object is created from a row request, then it needs to be bound to a primary key so changes can be commited... But right now the neatest way to do this eludes me. Right now the sql::update helper expects `id` as a primary key. But this isn't exactly flexible.

    Thoughts?
    Omnis mico antequam dominus Spookster!

  • #2
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    default the argument to 'id' so you only have to (but can) specify it if it's different?
    PHP Code:
    public static function update($table_name,$vars,$id_field='id') {
    ... 
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #3
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Can I expect the first field in a returned row of a query to be a primary key, if any?
    Omnis mico antequam dominus Spookster!

  • #4
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    not necessarily, what would happen with composite keys?

    One option would be for your models to be reading the metadata from the database to determine such things. In postgres you can use tables with the pg_ prefix to work out such things, I assume MySQL has a similar thing. If you take a look at the MetaColumns() function in adodb (http://adodb.sourceforge.net/) then it should give you a pointer.
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com

  • #5
    Senior Coder Mhtml's Avatar
    Join Date
    Jun 2002
    Location
    Sydney, Australia
    Posts
    3,531
    Thanks
    0
    Thanked 1 Time in 1 Post
    Woohoo! Decent reference material!

    Cheers man, I'll see what I can dig up.
    Omnis mico antequam dominus Spookster!


  •  

    Posting Permissions

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