Jump to content

Recommended Posts

Hi all
After 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?

Cheers
Mark
Link to comment
https://forums.phpfreaks.com/topic/27324-active-record-tabledatagateway/
Share on other sites

hmmm i'll try and be clearer:

[code]
<?php
class 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]
<?php
class 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 tables
b) the matching posts AND the comments that belong to those posts are returned in some form.

hope that makes sense
Cheers
Mark
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...
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]<?php

class 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.
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.
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.