Jump to content


Click here to lend your support to: Traq and make a donation at pledgie.com !
Photo

Avalons database features


  • Please log in to reply
3 replies to this topic

#1 Jack

Jack

    Project Founder

  • Administrators
  • 673 posts
  • LocationAustralia

Posted 10 November 2011 - 10:27 AM

The database features in the framework are quite interesting.

If you don't want to directly use a model to fetch information you can tell the query builder to use the model class before executing it, like so:


$db->select()->from('posts')->_model('User')->exec()->fetchAll();


This returns an array of User model objects.

Another cool thing is callbacks, if you want to process the data within the query chain, you can do so:


$db->select()->from('users')->_callback(array('Post','format_text'))->exec()->fetchAll();


This will most likely be primarily used by the model system but can be used by the query builder.

What it does it after executing the query and before returning the result, the database class runs the data through the callback function which does whatever is needed then returns the processed data back to the database class.

#2 Jack

Jack

    Project Founder

  • Administrators
  • 673 posts
  • LocationAustralia

Posted 05 January 2012 - 07:12 AM

Here is some more information on the database model system, or "ORM" so to speak.

Models work like so:

class User extends Model
{
protected static $_name = 'users'; // table name
protected static $_properties = array( // table columns
'id',
'username',
'password',
'group_id' // required if setting up a belongs to relationship. format: {table_singular_name}_id
);

protected static $_belongs_to = array('group'); // belongs to: $user->group->name
protected static $_has_many = array('tickets'); // has many: $user->tickets->fetch_all();

// custom function to easily get the users profile URL.
public function href()
{
return "/path/to/profiles/" . $this->_data['username']; // row data is stored in the _data array.
}

// currently, this is the only way to validate model data.
public function is_valid()
{
$errors = array();
if (empty($this->_data['username']))
{
$errors['username'] = 'Username cannot be empty';
}

if (count($errors))
{
$this->errors = $errors;
return false;
}
else
{
return true;
}
}
}


The base idea of the "ORM" is similar to Rails' ActiveRecord, but not as advanced, for example you can do something like


$new_user = new User(array('username' => 'tester', 'password' => '1234'));
if ($new_user->is_valid())
{
$new_user->save();
}
else
{
print_r($new_user->errors);
}

$user = User::find(2); // or User::find('username', 'test_user');
$user->email = [email protected]';
$user->save();


But cannot do something like:

$user = User::find(2);
$ticket = $user->tickets->new();


The DB model system is very basic, and that's the idea, it uses the basic query builder which is not a replacement to the query() function.

For advanced queries:

$db = Database::driver();
$db->query("SELECT summary FROM tickets WHERE `milestone_id` IN (1,2)");




Note: While model's dont require the properties/columns to be explicitly stated, it is recommended as the model system was designed for them to be set.

#3 Jack

Jack

    Project Founder

  • Administrators
  • 673 posts
  • LocationAustralia

Posted 05 January 2012 - 01:23 PM

I figured i would share since you are still in early-ish stages.


Actually, only Traq 3 is in the early stages, the framework and it's database stuff is pretty solid.

Avalon get's everything from my other framework (my PHP 5.3+ one) Radium. I just need to pull out the latest database classes and tweak them to work with Avalon (PHP 5.2 framework).

Radium's database system has only one driver, a PDO wrapper, but has the code there to use other drivers if people want to create them.

The query builder works like:


$db = Database::driver();
$db->select(array('id', 'username'))->from('users')->where('group_id', 2, '>=')->exec()->fetch_all();


That method chain is compiled into:

SELECT id, username FROM users WHERE `group_id` >= '2'

Like I said before, the query builder is basic and not meant for complex queries, joins and such are not supported.

If you want to see a great example of Traq (and Avalons) future, check out my single-project bug tracker I've been working on, Bugger. (work on that, and it's framework contribute to Traq and it's framework, DB system for example.)

#4 Jack

Jack

    Project Founder

  • Administrators
  • 673 posts
  • LocationAustralia

Posted 26 February 2012 - 06:50 PM

The database now supports multiple connections.


// Initiates the main database connection and loads models,
// the main connection is what is specified in the config/database.php file.
Database::init()

// Initiates another connection named 'second_connection' with
// the passed config array, which should look the same as
// the config/database.php file.
$second_connection = Database::factory($config, 'second_connection');

// To get access a connection use the Database::connection() method
// while passing the connection name, no name will return the main connection.
$second_connection = Database::connection('second_connection');

// To have a model use a different connection other than the main
// one, change the _connection_name value.
class User extends Model
{
protected static $_connection_name = 'second_connection';
...
}



0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users