redbullmarky Posted November 15, 2006 Share Posted November 15, 2006 Hi allAfter playing around with Rails a bit to see what all the fuss is about, as well as CakePHP and PHPonTrax, I've come across something pretty interesting that I'd like to incorporate in my new framework, however due to how it works, is a little beyond me at the moment.Active Record.I can get something similar working with one table (using a single model). However, I'm not sure how to go about the whole 'hasmany', 'hasone', 'belongsto', etc, as I can't get my head around the code. Can anyone give me an idea of how I might put something similar together? I'm pretty sure that it doesnt need to be as complex as the other frameworks. My confusion lies in when two models, each representing a database table, are pulled together to form the table 'JOIN's. Any ideas of simpler/alternative ways of doing this?CheersMark Quote Link to comment https://forums.phpfreaks.com/topic/27324-active-record-tabledatagateway/ Share on other sites More sharing options...
Jenk Posted November 15, 2006 Share Posted November 15, 2006 [code]<?phpclass ActiveRecord{ private $_data = array(); public function hasMany() { return (count($this->_data) > 1); } public function hasOne() { return (count($this->_data) == 1); }}?>[/code]etc. Quote Link to comment https://forums.phpfreaks.com/topic/27324-active-record-tabledatagateway/#findComment-124909 Share on other sites More sharing options...
redbullmarky Posted November 15, 2006 Author Share Posted November 15, 2006 hmmm i'll try and be clearer:[code]<?phpclass Model{ // id of the DB record public $id = null; public $hasMany = array(); public $belongsTo = array(); // finds all records based on conditions function findAll($conditions ...) { }}?>[/code][code]<?phpclass Post extends Model{ public $hasMany = array('Comment'); }class Comment extends Model{ public $belongsTo = array('Post');}?>[/code]now - i have two objects descending from model. if i use:[code]<?php$post = new Post();$post->findAll('title LIKE 'test%' AND published = 'y');?>[/code]i want a structure returned that contains all the posts with the title starting with 'test' and that have been specified as publish. in addition (and this is where my problem is - getting this done correctly) I want to somehow link the Post and Comment objects together so that:a) valid SQL is generated, handling the joins between tablesb) the matching posts AND the comments that belong to those posts are returned in some form.hope that makes senseCheersMark Quote Link to comment https://forums.phpfreaks.com/topic/27324-active-record-tabledatagateway/#findComment-124911 Share on other sites More sharing options...
Jenk Posted November 15, 2006 Share Posted November 15, 2006 That's not an active record problem. Have a look at the TableDataGateway pattern, and RowDataGateway. Quote Link to comment https://forums.phpfreaks.com/topic/27324-active-record-tabledatagateway/#findComment-124916 Share on other sites More sharing options...
redbullmarky Posted November 15, 2006 Author Share Posted November 15, 2006 ok thanks for clearing that up, I often saw this type of practice mentioned in the same breath as active record so hence my assumption.either way, i'm gonna be delving into stuff that doesnt exactly simplify things - ie, no laymans. Taking into account my example, what is the best way to allow these two seperate models to work together? using two models in this fashion is totally new to me and I get lost in the logic... Quote Link to comment https://forums.phpfreaks.com/topic/27324-active-record-tabledatagateway/#findComment-124919 Share on other sites More sharing options...
Jenk Posted November 15, 2006 Share Posted November 15, 2006 TableDataGateway is an object tailored to the table. I often implement it to return a RowDataGateway which is an objected tailored to a row from the table, simply put:[code]<?phpclass Posts{ private $_data = array(); private $_db; public function __construct(DatabaseObject $db) { $this->_db = $db; $this->_data = $this->_db->select('*', array('posts', 'users')); // returns array of records } public function getRecord($id = 0) { if (!isset($this->_data[$id]) || !is_array($this->_data[$id])) { throw new InvalidArgumentException('Record for id "' . $id . '" does not exist'); } return new PostsRecord($this->_data[$id]); }}?>[/code]I can then use the PostsRecord object to access the fields. Quote Link to comment https://forums.phpfreaks.com/topic/27324-active-record-tabledatagateway/#findComment-124931 Share on other sites More sharing options...
redbullmarky Posted November 15, 2006 Author Share Posted November 15, 2006 the TDGW/RDGW make sense, but still doesnt solve my issue of cross table joins. lets say you repeated your class above, for 'Comments'. you specified in your Posts object that it 'hasMany' Comments. as it's the Comments object that's responsible for dealing with all things comments, how do you link the two objects together to form the 'LEFT JOIN' part of the query, as Rails/Cake/PHPonTrax seem to do it?surely, for it to properly obey the correct oop rules, the Posts model would not just make assumtions of what the Comments table looks/acts like. Quote Link to comment https://forums.phpfreaks.com/topic/27324-active-record-tabledatagateway/#findComment-125214 Share on other sites More sharing options...
Jenk Posted November 16, 2006 Share Posted November 16, 2006 The data is collected at object instantiation, not on-the-fly.To the object, all it needs to see is a result set - it does not care if it's a left join, a right join or no join. Quote Link to comment https://forums.phpfreaks.com/topic/27324-active-record-tabledatagateway/#findComment-125456 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.