nathggns / Scaffold

Lightweight PHP API Framework
Other
8 stars 2 forks source link

Add function support #125

Closed nathggns closed 11 years ago

nathggns commented 11 years ago

These commits add SQL function support across Scaffold's database system.

Usage

<?php
// Get a random user
$users = new ModelUser();
$users->find[
    'order' => [
        Database::func_random()
    ],
    'limit' => 1
]);

// Gets the count using our system
$users = new ModelUser();
$users->alias('count', Database::func_count('*'));
echo $users->count;
// 1

$user = new ModelUser();
$user->create([
    'name' => 'Charlie'
]);
echo $users->count;
// 2

$users = new ModelUser();

echo $users->max('id');
// 2

echo $users->min('id');
// 1

Changelog

This pull request added the following two classes.

It adds the following service

Database::__construct has been modified to not need an instance of Config passed to it if it has been initiated before.

Dynamic has been modified to support being an alias for a class method. Simply pass the same array you'd pass to call_user_func

The following methods have been added

The following bugs have been fixed

nathggns commented 11 years ago

This fixes #85

andrewhathaway commented 11 years ago

Using Database::func_random() isn't too nice. Would be nice to have maybe something like the following, not sure if it's possible.

<?php
$user->random([]);

Where the array would be the where clauses.

nathggns commented 11 years ago

I never said that helpers like that wouldn't be possible, you could even define those yourself. I'll define some later, including that one.

However, I was thinking of using __call for functions as columns.

andrewhathaway commented 11 years ago

Ah. Awesome.

nathggns commented 11 years ago

@AndrewHathaway You can now pass functions to ModelDatabase by simply calling the function. ModelDatabase::max for example. You can pass arguments too.

nathggns commented 11 years ago

@ClaudioAlbertin Question. Should aliases functions be part of the export array?

andrewhathaway commented 11 years ago

Nice work. :)

nathggns commented 11 years ago

The random function uses the preexisting conditions. Set conditions, then use random.

nathggns commented 11 years ago

Merging. Any complaints about this implementation should be made in a separate issue.