native-sql.txt 2.97 KB
Newer Older
phuson's avatar
phuson committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
++ Introduction

Doctrine_RawSql provides convient interface for building raw sql queries. Similar to Doctrine_Query, Doctrine_RawSql provides means for fetching arrays and objects, the way you prefer.

Using raw sql for fetching might be useful when you want to utilize database specific features such as query hints or the CONNECT keyword in Oracle.

Creating Doctrine_RawSql object is easy:

<code type="php">
$q = new Doctrine_RawSql();
</code>

Optionally a connection parameter can be given:

<code type="php">
$q = new Doctrine_RawSql($conn); // here $conn is an instance of Doctrine_Connection
</code>

++ Component queries

The first thing to notice when using Doctrine_RawSql is that you always have to place the fields you are selecting in curly brackets {}. Also for every selected component you have to call addComponent().

The following example should clarify the usage of these:

<code type="php">
$q = new Doctrine_RawSql();

$q->select('{u.*}')
  ->from('user')
  ->addComponent('user', 'User'); // here we tell that user table is bound to class called 'User'

$users = $q->execute();
$user[0]; // User object
</code>

36
Pay attention to following things:
phuson's avatar
phuson committed
37 38 39 40 41 42 43 44 45

# Fields must be in curly brackets
# For every selected table there must be one addComponent call


++ Fetching from multiple components

When fetching from multiple components the addComponent calls become a bit more complicated as not only do we have to tell which tables are bound to which components, we also have to tell the parser which components belongs to which.

46
Consider the following model:
phuson's avatar
phuson committed
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

<code type="php">
// file User.php
class User extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->hasColumn('name', 'string', 20);
    }
    public function setUp()
    {
        $this->hasMany('Phonenumber', array('local' => 'id',
                                            'foreign' => 'user_id'));
    }
}
// file Phonenumber.php
class Phonenumber extends Doctrine_Record
{
    public function setTableDefinition()
    {
        $this->hasColumn('phonenumber', 'string', 20);
        $this->hasColumn('user_id', 'integer');
    }
    public function setUp()
    {
        $this->hasOne('User', array('local' => 'user_id',
                                    'foreign' => 'id',
                                    'onDelete' => 'CASCADE'));
    }
}
</code>

79
In the following example we fetch all users and their phonenumbers:
phuson's avatar
phuson committed
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97

<code type="php">
$q = new Doctrine_RawSql();

$q->select('{u.*}, {p.*}')
  ->from('user u LEFT JOIN phonenumber p ON u.id = p.user_id')
  // here we tell that user table is bound to class called 'User'
  // we also add an alias for User class called 'u'
  // this alias will be used when referencing to User class
  ->addComponent('u', 'User u')
  // here we add another component that is bound to table phonenumber
  // notice how we reference that the Phonenumber class is "User's phonenumber"
  ->addComponent('p', 'u.Phonenumber p');


$users = $q->execute();
$users[0]; // User object
</code>