Working with objects - Component overview - Collection - Fetching strategies.php 2.77 KB
Newer Older
hansbrix's avatar
hansbrix committed
1 2 3 4 5 6 7
Whenever you fetch records with eg. Doctrine_Table::findAll or Doctrine_Connection::query methods an instance of
Doctrine_Collection is returned. There are many types of collections in Doctrine and it is crucial to understand
the differences of these collections. Remember choosing the right fetching strategy (collection type) is one of the most 
influental things when it comes to boosting application performance.



8
* Immediate Collection
hansbrix's avatar
hansbrix committed
9 10 11 12 13 14 15 16 17 18 19
Fetches all records and all record data immediately into collection memory. Use this collection only if you really need to show all that data
in web page.



Example query:

SELECT id, name, type, created FROM user



20
* Batch Collection
hansbrix's avatar
hansbrix committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
Fetches all record primary keys into colletion memory. When individual collection elements are accessed this collection initializes proxy objects.
When the non-primary-key-property of a proxy object is accessed that object sends request to Batch collection which loads the data
for that specific proxy object as well as other objects close to that proxy object.



Example queries:

SELECT id FROM user

SELECT id, name, type, created FROM user WHERE id IN (1,2,3,4,5)

SELECT id, name, type, created FROM user WHERE id IN (6,7,8,9,10)

[ ... ]


38
* Lazy Collection
hansbrix's avatar
hansbrix committed
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
Lazy collection is exactly same as Batch collection with batch size preset to one.



Example queries:

SELECT id FROM user

SELECT id, name, type, created FROM user WHERE id = 1

SELECT id, name, type, created FROM user WHERE id = 2

SELECT id, name, type, created FROM user WHERE id = 3

[ ... ]


56
* Offset Collection
hansbrix's avatar
hansbrix committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
Offset collection is the same as immediate collection with the difference that it uses database provided limiting of queries.



Example queries:

SELECT id, name, type, created FROM user LIMIT 5

SELECT id, name, type, created FROM user LIMIT 5 OFFSET 5

SELECT id, name, type, created FROM user LIMIT 5 OFFSET 10

[ ... ]



<code type="php">
$table = $conn->getTable("User");

$table->setAttribute(Doctrine::ATTR_FETCHMODE, Doctrine::FETCH_IMMEDIATE);

$users = $table->findAll();

// or

$users = $conn->query("FROM User-I"); // immediate collection

foreach($users as $user) {
    print $user->name;
}


$table->setAttribute(Doctrine::ATTR_FETCHMODE, Doctrine::FETCH_LAZY);

$users = $table->findAll();

// or

$users = $conn->query("FROM User-L"); // lazy collection

foreach($users as $user) {
    print $user->name;
}

$table->setAttribute(Doctrine::ATTR_FETCHMODE, Doctrine::FETCH_BATCH);

$users = $table->findAll();

// or

$users = $conn->query("FROM User-B"); // batch collection

foreach($users as $user) {
    print $user->name;
}

$table->setAttribute(Doctrine::ATTR_FETCHMODE, Doctrine::FETCH_OFFSET);

$users = $table->findAll();

// or

$users = $conn->query("FROM User-O"); // offset collection

foreach($users as $user) {
    print $user->name;
}
</code>