~coughphp/coughphp/2.0

« back to all changes in this revision

Viewing changes to docs/constructing_collections.markdown

  • Committer: Anthony Bush
  • Date: 2008-08-23 03:35:08 UTC
  • mfrom: (262.1.18 coughphp-release-1.3)
  • Revision ID: anthony@anthonybush.com-20080823033508-uy4yn5pmio6wcetv
Accept release-1.3 branch changes into trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
Constructing Collections
2
 
========================
3
 
 
4
 
Collections can be retrieved in many ways, including:
5
 
 
6
 
* From a related object
7
 
* Through manual instantiation.
8
 
* Using custom SQL
9
 
 
10
 
From a related object
11
 
---------------------
12
 
 
13
 
        $author = Author::constructByKey($authorId);
14
 
        $books = $author->getBook_Collection();
15
 
 
16
 
Through manual instantiation
17
 
----------------------------
18
 
 
19
 
        $book = new Book_Collection();
20
 
        $book->load();
21
 
 
22
 
Using custom SQL
23
 
----------------
24
 
 
25
 
        $book = new Book_Collection();
26
 
        $book->loadBySql($sql);
27
 
 
28
 
Building on the base SQL
29
 
------------------------
30
 
 
31
 
Building on the base SQL is currently somewhat of a challenge, as it requires knowledge of what parts of the SQL the base includes.  As such, it might be necessary to use PHP's string functions to replace parts of the SQL.
32
 
 
33
 
Assuming the base SQL doesn't include an ORDER BY clause, the following would work:
34
 
 
35
 
        $book = new Book_Collection();
36
 
        $sql = $book->getLoadSql();
37
 
        $sql .= ' ORDER BY book.title DESC LIMIT 100';
38
 
        $book->loadBySql($sql);
39
 
 
40
 
In the future, it could work like this:
41
 
 
42
 
        $book = new Book_Collection();
43
 
        $sql = $book->getLoadSql();
44
 
        $sql->setOrderBy('book.title DESC');
45
 
        $sql->setLimit(100);
46
 
        $book->loadBySql($sql);