[ create a new paste ] login | about

Link: http://codepad.org/OEVsQmwt    [ raw code | fork ]

k4st - PHP, pasted on Jun 8:
<?php

error_reporting(E_ALL | E_STRICT);

define('DIR_WORKING', dirname(__FILE__));

require_once DIR_WORKING .'/system/__init__.php';
require_once DIR_WORKING .'/system/page-controller.php';

// bring in the stuff
require_once DIR_WORKING .'/system/abstract-query.php';
require_once DIR_WORKING .'/system/abstract-model.php';
require_once DIR_WORKING .'/system/model-dictionary.php';
require_once DIR_WORKING .'/system/concrete-query.php';
require_once DIR_WORKING .'/system/sql-query.php';



$model = new ModelDictionary();

// test with heirarchical models
$model->create('xml', struct()->

    rss->model(struct()->

        channel->model(struct()->

            title      ->string()->
            description->string()->
            pubDate    ->string()->
            
            item->model(struct()->
                title      ->string()->
                description->string()->
                pubDate    ->string()
            )
        )
    )
);

// test with relational models
$model->create('users', struct()->
    id      ->int()->primary_key()
            ->mapTo('posts', 'user_id')->
    name    ->string(20)->
    relatesTo('posts')->
    relatesTo('profiles')
);

$model->create('posts', struct()->
    id      ->int()->primary_key()->
    user_id ->int()
            ->mapTo('users', 'id')->
    title   ->string(255)->
    content_id->int()
            ->mapTo('content', 'id')->  
    relatesTo('profiles', through('users'))->
    relatesTo('content')
);

$model->create('profiles', struct()->
    user_id ->int()
            ->mapTo('users', 'id')->
    full_name->string(30)
);

$model->create('content', struct()->
    id      ->int()->primary_key()->
    text    ->string()
);

$query = in('posts', 'p')->select(ALL)->
         in('profiles')->select('full_name')->
         in('content')->select('text')->
         link('p', 'profiles')->
         link('p', 'content')->
         where->p('id')->eq(_)-> // underscore is a value to sub in later
         _and->profiles('user_id')->gt_eq->p('id');

$sql = new SqlQuery($query, $model);
var_dump($sql->compile(SqlQuery::SELECT));

/*
Produces this query:

SELECT p. * , profiles.full_name AS full_name, content.text AS text
FROM (
    posts p
    INNER JOIN (
        users t1
        INNER JOIN profiles ON t1.id = profiles.user_id
    ) ON p.user_id = t1.id
    INNER JOIN content ON p.content_id = content.id
)
WHERE p.id = ?
AND profiles.user_id >= p.id
*/


Create a new paste based on this one


Comments: