David Di Biase Mon Feb 20 05:10:57 -0500 2012

Subject: Direct query on database

Hi there,

I've been reading the docs and trying to find a way to directly query the database without assigning a model. The reason for this is, I want to create a new table. I'm building a database manager for my application and need the ability to do this.

I attempted tapping into the Configuration and Connection classes but I don't think I quite understand the structure of ActiveRecord ... yet. There seems to be a query method on the Connection class, but I don't know how to tap into that class without defining a model first. Oi veh.

A gentle push in the right direction would be great =)

Thanks,

Dave


Afeique Sheikh Mon Feb 20 11:04:24 -0500 2012

Hi Dave,

I hope this is of use:

1 $ar_adapter = ActiveRecord\ConnectionManager::get_connection()

will return ActiveRecord's default connection object. You can also specify the a specific connection name as an argument, e.g. get_connection('name');

A connection object corresponding to your database type will be returned, e.g.

object(ActiveRecord\MysqlAdapter)[9]
  public 'connection' => 
    object(PDO)[10]
  public 'last_query' => string '' (length=0)
  private 'logging' (ActiveRecord\Connection) => boolean false
  private 'logger' (ActiveRecord\Connection) => null
  public 'protocol' => string 'mysql' (length=5)

From that, you can use the PDO connection object to directly query your database via PDO::query (http://www.php.net/manual/en/pdo.query.php):

1 $connection = $ar_connection->connection;
2 $connection->query('DROP DATABASE useless;');

(1-1/1)