Zend_Db_Select.xml 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!-- Reviewed: no -->
  3. <sect1 id="zend.db.select">
  4. <title>Zend_Db_Select</title>
  5. <sect2 id="zend.db.select.introduction">
  6. <title>Introduction</title>
  7. <para>
  8. The <classname>Zend_Db_Select</classname> object represents a <acronym>SQL</acronym> <acronym>SELECT</acronym>
  9. query statement. The class has methods for adding individual parts to the query. You can
  10. specify some parts of the query using <acronym>PHP</acronym> methods and data structures, and the class
  11. forms the correct <acronym>SQL</acronym> syntax for you. After you build a query, you can execute the query
  12. as if you had written it as a string.
  13. </para>
  14. <para>
  15. The value offered by <classname>Zend_Db_Select</classname> includes:
  16. </para>
  17. <itemizedlist>
  18. <listitem>
  19. <para>
  20. Object-oriented methods for specifying <acronym>SQL</acronym> queries in a piece-by-piece manner;
  21. </para>
  22. </listitem>
  23. <listitem>
  24. <para>
  25. Database-independent abstraction of some parts of the <acronym>SQL</acronym> query;
  26. </para>
  27. </listitem>
  28. <listitem>
  29. <para>
  30. Automatic quoting of metadata identifiers in most cases, to support identifiers
  31. containing <acronym>SQL</acronym> reserved words and special characters;
  32. </para>
  33. </listitem>
  34. <listitem>
  35. <para>
  36. Quoting identifiers and values, to help reduce risk of <acronym>SQL</acronym> injection attacks.
  37. </para>
  38. </listitem>
  39. </itemizedlist>
  40. <para>
  41. Using <classname>Zend_Db_Select</classname> is not mandatory. For very simple SELECT
  42. queries, it is usually simpler to specify the entire <acronym>SQL</acronym> query as a string and execute
  43. it using Adapter methods like <methodname>query()</methodname> or
  44. <methodname>fetchAll()</methodname>. Using
  45. <classname>Zend_Db_Select</classname> is helpful if you need to assemble a SELECT query
  46. procedurally, or based on conditional logic in your application.
  47. </para>
  48. </sect2>
  49. <sect2 id="zend.db.select.creating">
  50. <title>Creating a Select Object</title>
  51. <para>
  52. You can create an instance of a <classname>Zend_Db_Select</classname> object using the
  53. <methodname>select()</methodname> method of a <classname>Zend_Db_Adapter_Abstract</classname>
  54. object.
  55. </para>
  56. <example id="zend.db.select.creating.example-db">
  57. <title>Example of the database adapter's select() method</title>
  58. <programlisting language="php"><![CDATA[
  59. $db = Zend_Db::factory( ...options... );
  60. $select = $db->select();
  61. ]]></programlisting>
  62. </example>
  63. <para>
  64. Another way to create a <classname>Zend_Db_Select</classname> object is with its
  65. constructor, specifying the database adapter as an argument.
  66. </para>
  67. <example id="zend.db.select.creating.example-new">
  68. <title>Example of creating a new Select object</title>
  69. <programlisting language="php"><![CDATA[
  70. $db = Zend_Db::factory( ...options... );
  71. $select = new Zend_Db_Select($db);
  72. ]]></programlisting>
  73. </example>
  74. </sect2>
  75. <sect2 id="zend.db.select.building">
  76. <title>Building Select queries</title>
  77. <para>
  78. When building the query, you can add clauses of the query one by one. There is a
  79. separate method to add each clause to the <classname>Zend_Db_Select</classname> object.
  80. </para>
  81. <example id="zend.db.select.building.example">
  82. <title>Example of the using methods to add clauses</title>
  83. <programlisting language="php"><![CDATA[
  84. // Create the Zend_Db_Select object
  85. $select = $db->select();
  86. // Add a FROM clause
  87. $select->from( ...specify table and columns... )
  88. // Add a WHERE clause
  89. $select->where( ...specify search criteria... )
  90. // Add an ORDER BY clause
  91. $select->order( ...specify sorting criteria... );
  92. ]]></programlisting>
  93. </example>
  94. <para>
  95. You also can use most methods of the <classname>Zend_Db_Select</classname> object with a
  96. convenient fluent interface. A fluent interface means that each method returns a
  97. reference to the object on which it was called, so you can immediately call another
  98. method.
  99. </para>
  100. <example id="zend.db.select.building.example-fluent">
  101. <title>Example of the using the fluent interface</title>
  102. <programlisting language="php"><![CDATA[
  103. $select = $db->select()
  104. ->from( ...specify table and columns... )
  105. ->where( ...specify search criteria... )
  106. ->order( ...specify sorting criteria... );
  107. ]]></programlisting>
  108. </example>
  109. <para>
  110. The examples in this section show usage of the fluent interface, but you can use the
  111. non-fluent interface in all cases. It is often necessary to use the non-fluent
  112. interface, for example, if your application needs to perform some logic before adding a
  113. clause to a query.
  114. </para>
  115. <sect3 id="zend.db.select.building.from">
  116. <title>Adding a FROM clause</title>
  117. <para>
  118. Specify the table for this query using the <methodname>from()</methodname> method.
  119. You can specify the table name as a simple string.
  120. <classname>Zend_Db_Select</classname> applies identifier quoting around the table
  121. name, so you can use special characters.
  122. </para>
  123. <example id="zend.db.select.building.from.example">
  124. <title>Example of the from() method</title>
  125. <programlisting language="php"><![CDATA[
  126. // Build this query:
  127. // SELECT *
  128. // FROM "products"
  129. $select = $db->select()
  130. ->from( 'products' );
  131. ]]></programlisting>
  132. </example>
  133. <para>
  134. You can also specify the correlation name (sometimes called the "table alias") for
  135. a table. Instead of a simple string, use an associative array mapping the
  136. correlation name to the table name. In other clauses of the <acronym>SQL</acronym> query, use this
  137. correlation name. If your query joins more than one table,
  138. <classname>Zend_Db_Select</classname> generates unique correlation names based on
  139. the table names, for any tables for which you don't specify the correlation name.
  140. </para>
  141. <example id="zend.db.select.building.from.example-cname">
  142. <title>Example of specifying a table correlation name</title>
  143. <programlisting language="php"><![CDATA[
  144. // Build this query:
  145. // SELECT p.*
  146. // FROM "products" AS p
  147. $select = $db->select()
  148. ->from( array('p' => 'products') );
  149. ]]></programlisting>
  150. </example>
  151. <para>
  152. Some <acronym>RDBMS</acronym> brands support a leading schema specifier for a table. You can specify
  153. the table name as "<code>schemaName.tableName</code>", where
  154. <classname>Zend_Db_Select</classname> quotes each part individually, or you may
  155. specify the schema name separately. A schema name specified in the table name takes
  156. precedence over a schema provided separately in the event that both are provided.
  157. </para>
  158. <example id="zend.db.select.building.from.example-schema">
  159. <title>Example of specifying a schema name</title>
  160. <programlisting language="php"><![CDATA[
  161. // Build this query:
  162. // SELECT *
  163. // FROM "myschema"."products"
  164. $select = $db->select()
  165. ->from( 'myschema.products' );
  166. // or
  167. $select = $db->select()
  168. ->from('products', '*', 'myschema');
  169. ]]></programlisting>
  170. </example>
  171. </sect3>
  172. <sect3 id="zend.db.select.building.columns">
  173. <title>Adding Columns</title>
  174. <para>
  175. In the second argument of the <methodname>from()</methodname> method, you can
  176. specify the columns to select from the respective table. If you specify no columns,
  177. the default is "<code>*</code>", the <acronym>SQL</acronym> wildcard for "all columns".
  178. </para>
  179. <para>
  180. You can list the columns in a simple array of strings, or as an associative mapping
  181. of column alias to column name. If you only have one column to query, and you don't
  182. need to specify a column alias, you can list it as a plain string instead of an
  183. array.
  184. </para>
  185. <para>
  186. If you give an empty array as the columns argument, no columns from the respective
  187. table are included in the result set. See a
  188. <link linkend="zend.db.select.building.join.example-no-columns">code example</link>
  189. under the section on the <methodname>join()</methodname> method.
  190. </para>
  191. <para>
  192. You can specify the column name as "<code>correlationName.columnName</code>".
  193. <classname>Zend_Db_Select</classname> quotes each part individually. If you don't
  194. specify a correlation name for a column, it uses the correlation name for the table
  195. named in the current <methodname>from()</methodname> method.
  196. </para>
  197. <example id="zend.db.select.building.columns.example">
  198. <title>Examples of specifying columns</title>
  199. <programlisting language="php"><![CDATA[
  200. // Build this query:
  201. // SELECT p."product_id", p."product_name"
  202. // FROM "products" AS p
  203. $select = $db->select()
  204. ->from(array('p' => 'products'),
  205. array('product_id', 'product_name'));
  206. // Build the same query, specifying correlation names:
  207. // SELECT p."product_id", p."product_name"
  208. // FROM "products" AS p
  209. $select = $db->select()
  210. ->from(array('p' => 'products'),
  211. array('p.product_id', 'p.product_name'));
  212. // Build this query with an alias for one column:
  213. // SELECT p."product_id" AS prodno, p."product_name"
  214. // FROM "products" AS p
  215. $select = $db->select()
  216. ->from(array('p' => 'products'),
  217. array('prodno' => 'product_id', 'product_name'));
  218. ]]></programlisting>
  219. </example>
  220. </sect3>
  221. <sect3 id="zend.db.select.building.columns-expr">
  222. <title>Adding Expression Columns</title>
  223. <para>
  224. Columns in <acronym>SQL</acronym> queries are sometimes expressions, not simply column names from a
  225. table. Expressions should not have correlation names or quoting applied. If your
  226. column string contains parentheses, <classname>Zend_Db_Select</classname> recognizes
  227. it as an expression.
  228. </para>
  229. <para>
  230. You also can create an object of type <classname>Zend_Db_Expr</classname>
  231. explicitly, to prevent a string from being treated as a column name.
  232. <classname>Zend_Db_Expr</classname> is a minimal class that contains a single
  233. string. <classname>Zend_Db_Select</classname> recognizes objects of type
  234. <classname>Zend_Db_Expr</classname> and converts them back to string, but does not
  235. apply any alterations, such as quoting or correlation names.
  236. </para>
  237. <note>
  238. <para>
  239. Using <classname>Zend_Db_Expr</classname> for column names is not necessary if
  240. your column expression contains parentheses;
  241. <classname>Zend_Db_Select</classname> recognizes parentheses and treats the
  242. string as an expression, skipping quoting and correlation names.
  243. </para>
  244. </note>
  245. <example id="zend.db.select.building.columns-expr.example">
  246. <title>Examples of specifying columns containing expressions</title>
  247. <programlisting language="php"><![CDATA[
  248. // Build this query:
  249. // SELECT p."product_id", LOWER(product_name)
  250. // FROM "products" AS p
  251. // An expression with parentheses implicitly becomes
  252. // a Zend_Db_Expr.
  253. $select = $db->select()
  254. ->from(array('p' => 'products'),
  255. array('product_id', 'LOWER(product_name)'));
  256. // Build this query:
  257. // SELECT p."product_id", (p.cost * 1.08) AS cost_plus_tax
  258. // FROM "products" AS p
  259. $select = $db->select()
  260. ->from(array('p' => 'products'),
  261. array('product_id',
  262. 'cost_plus_tax' => '(p.cost * 1.08)')
  263. );
  264. // Build this query using Zend_Db_Expr explicitly:
  265. // SELECT p."product_id", p.cost * 1.08 AS cost_plus_tax
  266. // FROM "products" AS p
  267. $select = $db->select()
  268. ->from(array('p' => 'products'),
  269. array('product_id',
  270. 'cost_plus_tax' =>
  271. new Zend_Db_Expr('p.cost * 1.08'))
  272. );
  273. ]]></programlisting>
  274. </example>
  275. <para>
  276. In the cases above, <classname>Zend_Db_Select</classname> does not alter the string
  277. to apply correlation names or identifier quoting. If those changes are necessary to
  278. resolve ambiguity, you must make the changes manually in the string.
  279. </para>
  280. <para>
  281. If your column names are <acronym>SQL</acronym> keywords or contain special characters, you should use
  282. the Adapter's <methodname>quoteIdentifier()</methodname> method and interpolate the result into
  283. the string. The <methodname>quoteIdentifier()</methodname> method uses <acronym>SQL</acronym> quoting to delimit
  284. the identifier, which makes it clear that it is an identifier for a table or a
  285. column, and not any other part of <acronym>SQL</acronym> syntax.
  286. </para>
  287. <para>
  288. Your code is more database-independent if you use the
  289. <methodname>quoteIdentifier()</methodname> method instead of typing quotes literally in your
  290. string, because some <acronym>RDBMS</acronym> brands use nonstandard symbols for quoting identifiers.
  291. The <methodname>quoteIdentifier()</methodname> method is designed to use the appropriate
  292. quoting symbols based on the adapter type. The <methodname>quoteIdentifier()</methodname>
  293. method also escapes any quote characters that appear within the identifier name
  294. itself.
  295. </para>
  296. <example id="zend.db.select.building.columns-quoteid.example">
  297. <title>Examples of quoting columns in an expression</title>
  298. <programlisting language="php"><![CDATA[
  299. // Build this query,
  300. // quoting the special column name "from" in the expression:
  301. // SELECT p."from" + 10 AS origin
  302. // FROM "products" AS p
  303. $select = $db->select()
  304. ->from(array('p' => 'products'),
  305. array('origin' =>
  306. '(p.' . $db->quoteIdentifier('from') . ' + 10)')
  307. );
  308. ]]></programlisting>
  309. </example>
  310. </sect3>
  311. <sect3 id="zend.db.select.building.columns-atomic">
  312. <title>Adding columns to an existing FROM or JOIN table</title>
  313. <para>
  314. There may be cases where you wish to add columns to an existing FROM or JOIN table
  315. after those methods have been called. The <methodname>columns()</methodname> method allows you
  316. to add specific columns at any point before the query is executed. You can supply
  317. the columns as either a string or <classname>Zend_Db_Expr</classname> or as an array
  318. of these elements. The second argument to this method can be omitted, implying that
  319. the columns are to be added to the FROM table, otherwise an existing correlation
  320. name must be used.
  321. </para>
  322. <example id="zend.db.select.building.columns-atomic.example">
  323. <title>Examples of adding columns with the columns() method</title>
  324. <programlisting language="php"><![CDATA[
  325. // Build this query:
  326. // SELECT p."product_id", p."product_name"
  327. // FROM "products" AS p
  328. $select = $db->select()
  329. ->from(array('p' => 'products'), 'product_id')
  330. ->columns('product_name');
  331. // Build the same query, specifying correlation names:
  332. // SELECT p."product_id", p."product_name"
  333. // FROM "products" AS p
  334. $select = $db->select()
  335. ->from(array('p' => 'products'), 'p.product_id')
  336. ->columns('product_name', 'p');
  337. // Alternatively use columns('p.product_name')
  338. ]]></programlisting>
  339. </example>
  340. </sect3>
  341. <sect3 id="zend.db.select.building.join">
  342. <title>Adding Another Table to the Query with JOIN</title>
  343. <para>
  344. Many useful queries involve using a <acronym>JOIN</acronym> to combine rows from multiple
  345. tables. You can add tables to a <classname>Zend_Db_Select</classname> query using
  346. the <methodname>join()</methodname> method. Using this method is similar to the
  347. <methodname>from()</methodname> method, except you can also specify a join condition in most
  348. cases.
  349. </para>
  350. <example id="zend.db.select.building.join.example">
  351. <title>Example of the join() method</title>
  352. <programlisting language="php"><![CDATA[
  353. // Build this query:
  354. // SELECT p."product_id", p."product_name", l.*
  355. // FROM "products" AS p JOIN "line_items" AS l
  356. // ON p.product_id = l.product_id
  357. $select = $db->select()
  358. ->from(array('p' => 'products'),
  359. array('product_id', 'product_name'))
  360. ->join(array('l' => 'line_items'),
  361. 'p.product_id = l.product_id');
  362. ]]></programlisting>
  363. </example>
  364. <para>
  365. The second argument to <methodname>join()</methodname> is a string that is the join condition.
  366. This is an expression that declares the criteria by which rows in one table match
  367. rows in the other table. You can use correlation names in this expression.
  368. </para>
  369. <note>
  370. <para>
  371. No quoting is applied to the expression you specify for the join condition; if
  372. you have column names that need to be quoted, you must use
  373. <methodname>quoteIdentifier()</methodname> as you form the string for the join condition.
  374. </para>
  375. </note>
  376. <para>
  377. The third argument to <methodname>join()</methodname> is an array of column names, like that
  378. used in the <methodname>from()</methodname> method. It defaults to "<code>*</code>", supports
  379. correlation names, expressions, and <classname>Zend_Db_Expr</classname> in the same
  380. way as the array of column names in the <methodname>from()</methodname> method.
  381. </para>
  382. <para>
  383. To select no columns from a table, use an empty array for the list of columns. This
  384. usage works in the <methodname>from()</methodname> method too, but typically you want some
  385. columns from the primary table in your queries, whereas you might want no columns
  386. from a joined table.
  387. </para>
  388. <example id="zend.db.select.building.join.example-no-columns">
  389. <title>Example of specifying no columns</title>
  390. <programlisting language="php"><![CDATA[
  391. // Build this query:
  392. // SELECT p."product_id", p."product_name"
  393. // FROM "products" AS p JOIN "line_items" AS l
  394. // ON p.product_id = l.product_id
  395. $select = $db->select()
  396. ->from(array('p' => 'products'),
  397. array('product_id', 'product_name'))
  398. ->join(array('l' => 'line_items'),
  399. 'p.product_id = l.product_id',
  400. array() ); // empty list of columns
  401. ]]></programlisting>
  402. <para>
  403. Note the empty <methodname>array()</methodname> in the above example in place of a list of
  404. columns from the joined table.
  405. </para>
  406. </example>
  407. <para>
  408. <acronym>SQL</acronym> has several types of joins. See the list below for the methods to support
  409. different join types in <classname>Zend_Db_Select</classname>.
  410. </para>
  411. <itemizedlist>
  412. <listitem>
  413. <para>
  414. <command>INNER JOIN</command> with the
  415. <methodname>join(table, join, [columns])</methodname> or
  416. <methodname>joinInner(table, join, [columns])</methodname> methods.
  417. </para>
  418. <para>
  419. This may be the most common type of join. Rows from each table are compared
  420. using the join condition you specify. The result set includes only the rows
  421. that satisfy the join condition. The result set can be empty if no rows
  422. satisfy this condition.
  423. </para>
  424. <para>
  425. All <acronym>RDBMS</acronym> brands support this join type.
  426. </para>
  427. </listitem>
  428. <listitem>
  429. <para>
  430. <command>LEFT JOIN</command> with the
  431. <methodname>joinLeft(table, condition, [columns])</methodname> method.
  432. </para>
  433. <para>
  434. All rows from the left operand table are included, matching rows from the
  435. right operand table included, and the columns from the right operand table
  436. are filled with NULLs if no row exists matching the left table.
  437. </para>
  438. <para>
  439. All <acronym>RDBMS</acronym> brands support this join type.
  440. </para>
  441. </listitem>
  442. <listitem>
  443. <para>
  444. <command>RIGHT JOIN</command> with the
  445. <methodname>joinRight(table, condition, [columns])</methodname> method.
  446. </para>
  447. <para>
  448. Right outer join is the complement of left outer join. All rows from the
  449. right operand table are included, matching rows from the left operand table
  450. included, and the columns from the left operand table are filled with NULLs
  451. if no row exists matching the right table.
  452. </para>
  453. <para>
  454. Some <acronym>RDBMS</acronym> brands don't support this join type, but in general any right
  455. join can be represented as a left join by reversing the order of the
  456. tables.
  457. </para>
  458. </listitem>
  459. <listitem>
  460. <para>
  461. <command>FULL JOIN</command> with the
  462. <methodname>joinFull(table, condition, [columns])</methodname> method.
  463. </para>
  464. <para>
  465. A full outer join is like combining a left outer join and a right outer
  466. join. All rows from both tables are included, paired with each other on the
  467. same row of the result set if they satisfy the join condition, and
  468. otherwise paired with NULLs in place of columns from the other table.
  469. </para>
  470. <para>
  471. Some <acronym>RDBMS</acronym> brands don't support this join type.
  472. </para>
  473. </listitem>
  474. <listitem>
  475. <para>
  476. <command>CROSS JOIN</command> with the
  477. <methodname>joinCross(table, [columns])</methodname> method.
  478. </para>
  479. <para>
  480. A cross join is a Cartesian product. Every row in the first table is
  481. matched to every row in the second table. Therefore the number of rows in
  482. the result set is equal to the product of the number of rows in each table.
  483. You can filter the result set using conditions in a WHERE clause; in this
  484. way a cross join is similar to the old <acronym>SQL</acronym>-89 join syntax.
  485. </para>
  486. <para>
  487. The <methodname>joinCross()</methodname> method has no parameter to specify the join
  488. condition. Some <acronym>RDBMS</acronym> brands don't support this join type.
  489. </para>
  490. </listitem>
  491. <listitem>
  492. <para>
  493. <command>NATURAL JOIN</command> with the
  494. <methodname>joinNatural(table, [columns])</methodname> method.
  495. </para>
  496. <para>
  497. A natural join compares any column(s) that appear with the same name in
  498. both tables. The comparison is equality of all the column(s); comparing the
  499. columns using inequality is not a natural join. Only natural inner joins
  500. are supported by this <acronym>API</acronym>, even though <acronym>SQL</acronym> permits natural outer joins as
  501. well.
  502. </para>
  503. <para>
  504. The <methodname>joinNatural()</methodname> method has no parameter to specify the join
  505. condition.
  506. </para>
  507. </listitem>
  508. </itemizedlist>
  509. <para>
  510. In addition to these join methods, you can simplify your queries by using the
  511. JoinUsing methods. Instead of supplying a full condition to your join, you simply
  512. pass the column name on which to join and the <classname>Zend_Db_Select</classname>
  513. object completes the condition for you.
  514. </para>
  515. <example id="zend.db.select.building.joinusing.example">
  516. <title>Example of the joinUsing() method</title>
  517. <programlisting language="php"><![CDATA[
  518. // Build this query:
  519. // SELECT *
  520. // FROM "table1"
  521. // JOIN "table2"
  522. // ON "table1".column1 = "table2".column1
  523. // WHERE column2 = 'foo'
  524. $select = $db->select()
  525. ->from('table1')
  526. ->joinUsing('table2', 'column1')
  527. ->where('column2 = ?', 'foo');
  528. ]]></programlisting>
  529. </example>
  530. <para>
  531. Each of the applicable join methods in the <classname>Zend_Db_Select</classname>
  532. component has a corresponding 'using' method.
  533. </para>
  534. <itemizedlist>
  535. <listitem>
  536. <para>
  537. <methodname>joinUsing(table, join, [columns])</methodname> and
  538. <methodname>joinInnerUsing(table, join, [columns])</methodname>
  539. </para>
  540. </listitem>
  541. <listitem>
  542. <para>
  543. <methodname>joinLeftUsing(table, join, [columns])</methodname>
  544. </para>
  545. </listitem>
  546. <listitem>
  547. <para>
  548. <methodname>joinRightUsing(table, join, [columns])</methodname>
  549. </para>
  550. </listitem>
  551. <listitem>
  552. <para>
  553. <methodname>joinFullUsing(table, join, [columns])</methodname>
  554. </para>
  555. </listitem>
  556. </itemizedlist>
  557. </sect3>
  558. <sect3 id="zend.db.select.building.where">
  559. <title>Adding a WHERE Clause</title>
  560. <para>
  561. You can specify criteria for restricting rows of the result set using the
  562. <methodname>where()</methodname> method. The first argument of this method is a <acronym>SQL</acronym> expression,
  563. and this expression is used in a <acronym>SQL</acronym> <acronym>WHERE</acronym> clause in the query.
  564. </para>
  565. <example id="zend.db.select.building.where.example">
  566. <title>Example of the where() method</title>
  567. <programlisting language="php"><![CDATA[
  568. // Build this query:
  569. // SELECT product_id, product_name, price
  570. // FROM "products"
  571. // WHERE price > 100.00
  572. $select = $db->select()
  573. ->from('products',
  574. array('product_id', 'product_name', 'price'))
  575. ->where('price > 100.00');
  576. ]]></programlisting>
  577. </example>
  578. <note>
  579. <para>
  580. No quoting is applied to expressions given to the <methodname>where()</methodname> or
  581. <methodname>orWhere()</methodname> methods. If you have column names that need to be
  582. quoted, you must use <methodname>quoteIdentifier()</methodname> as you form the string for
  583. the condition.
  584. </para>
  585. </note>
  586. <para>
  587. The second argument to the <methodname>where()</methodname> method is optional. It is a value
  588. to substitute into the expression. <classname>Zend_Db_Select</classname> quotes the
  589. value and substitutes it for a question-mark ("<code>?</code>") symbol in the
  590. expression.
  591. </para>
  592. <para>
  593. This method accepts only one parameter. If you have an expression into which you
  594. need to substitute multiple variables, you must format the string manually,
  595. interpolating variables and performing quoting yourself.
  596. </para>
  597. <example id="zend.db.select.building.where.example-param">
  598. <title>Example of a parameter in the where() method</title>
  599. <programlisting language="php"><![CDATA[
  600. // Build this query:
  601. // SELECT product_id, product_name, price
  602. // FROM "products"
  603. // WHERE (price > 100.00)
  604. $minimumPrice = 100;
  605. $select = $db->select()
  606. ->from('products',
  607. array('product_id', 'product_name', 'price'))
  608. ->where('price > ?', $minimumPrice);
  609. ]]></programlisting>
  610. </example>
  611. <para>
  612. You can invoke the <methodname>where()</methodname> method multiple times on the same
  613. <classname>Zend_Db_Select</classname> object. The resulting query combines the
  614. multiple terms together using <acronym>AND</acronym> between them.
  615. </para>
  616. <example id="zend.db.select.building.where.example-and">
  617. <title>Example of multiple where() methods</title>
  618. <programlisting language="php"><![CDATA[
  619. // Build this query:
  620. // SELECT product_id, product_name, price
  621. // FROM "products"
  622. // WHERE (price > 100.00)
  623. // AND (price < 500.00)
  624. $minimumPrice = 100;
  625. $maximumPrice = 500;
  626. $select = $db->select()
  627. ->from('products',
  628. array('product_id', 'product_name', 'price'))
  629. ->where('price > ?', $minimumPrice)
  630. ->where('price < ?', $maximumPrice);
  631. ]]></programlisting>
  632. </example>
  633. <para>
  634. If you need to combine terms together using <acronym>OR</acronym>, use the
  635. <methodname>orWhere()</methodname> method. This method is used in the same way as the
  636. <methodname>where()</methodname> method, except that the term specified is preceded by
  637. <acronym>OR</acronym>, instead of <acronym>AND</acronym>.
  638. </para>
  639. <example id="zend.db.select.building.where.example-or">
  640. <title>Example of the orWhere() method</title>
  641. <programlisting language="php"><![CDATA[
  642. // Build this query:
  643. // SELECT product_id, product_name, price
  644. // FROM "products"
  645. // WHERE (price < 100.00)
  646. // OR (price > 500.00)
  647. $minimumPrice = 100;
  648. $maximumPrice = 500;
  649. $select = $db->select()
  650. ->from('products',
  651. array('product_id', 'product_name', 'price'))
  652. ->where('price < ?', $minimumPrice)
  653. ->orWhere('price > ?', $maximumPrice);
  654. ]]></programlisting>
  655. </example>
  656. <para>
  657. <classname>Zend_Db_Select</classname> automatically puts parentheses around each
  658. expression you specify using the <methodname>where()</methodname> or <methodname>orWhere()</methodname>
  659. methods. This helps to ensure that Boolean operator precedence does not cause
  660. unexpected results.
  661. </para>
  662. <example id="zend.db.select.building.where.example-parens">
  663. <title>Example of parenthesizing Boolean expressions</title>
  664. <programlisting language="php"><![CDATA[
  665. // Build this query:
  666. // SELECT product_id, product_name, price
  667. // FROM "products"
  668. // WHERE (price < 100.00 OR price > 500.00)
  669. // AND (product_name = 'Apple')
  670. $minimumPrice = 100;
  671. $maximumPrice = 500;
  672. $prod = 'Apple';
  673. $select = $db->select()
  674. ->from('products',
  675. array('product_id', 'product_name', 'price'))
  676. ->where("price < $minimumPrice OR price > $maximumPrice")
  677. ->where('product_name = ?', $prod);
  678. ]]></programlisting>
  679. </example>
  680. <para>
  681. In the example above, the results would be quite different without the parentheses,
  682. because <acronym>AND</acronym> has higher precedence than <acronym>OR</acronym>.
  683. <classname>Zend_Db_Select</classname> applies the parentheses so the effect is that
  684. each expression in successive calls to the <methodname>where()</methodname> bind more tightly
  685. than the <acronym>AND</acronym> that combines the expressions.
  686. </para>
  687. </sect3>
  688. <sect3 id="zend.db.select.building.group">
  689. <title>Adding a GROUP BY Clause</title>
  690. <para>
  691. In <acronym>SQL</acronym>, the <command>GROUP BY</command> clause allows you to reduce the rows of a query
  692. result set to one row per unique value found in the column(s) named in the
  693. <command>GROUP BY</command> clause.
  694. </para>
  695. <para>
  696. In <classname>Zend_Db_Select</classname>, you can specify the column(s) to use for
  697. calculating the groups of rows using the <methodname>group()</methodname> method. The argument
  698. to this method is a column or an array of columns to use in the
  699. <command>GROUP BY</command> clause.
  700. </para>
  701. <example id="zend.db.select.building.group.example">
  702. <title>Example of the group() method</title>
  703. <programlisting language="php"><![CDATA[
  704. // Build this query:
  705. // SELECT p."product_id", COUNT(*) AS line_items_per_product
  706. // FROM "products" AS p JOIN "line_items" AS l
  707. // ON p.product_id = l.product_id
  708. // GROUP BY p.product_id
  709. $select = $db->select()
  710. ->from(array('p' => 'products'),
  711. array('product_id'))
  712. ->join(array('l' => 'line_items'),
  713. 'p.product_id = l.product_id',
  714. array('line_items_per_product' => 'COUNT(*)'))
  715. ->group('p.product_id');
  716. ]]></programlisting>
  717. </example>
  718. <para>
  719. Like the columns array in the <methodname>from()</methodname> method, you can use correlation
  720. names in the column name strings, and the column is quoted as an identifier unless
  721. the string contains parentheses or is an object of type
  722. <classname>Zend_Db_Expr</classname>.
  723. </para>
  724. </sect3>
  725. <sect3 id="zend.db.select.building.having">
  726. <title>Adding a HAVING Clause</title>
  727. <para>
  728. In <acronym>SQL</acronym>, the <constant>HAVING</constant> clause applies a restriction condition on groups of
  729. rows. This is similar to how a <constant>WHERE</constant> clause applies a restriction
  730. condition on rows. But the two clauses are different because <constant>WHERE</constant>
  731. conditions are applied before groups are defined, whereas <constant>HAVING</constant>
  732. conditions are applied after groups are defined.
  733. </para>
  734. <para>
  735. In <classname>Zend_Db_Select</classname>, you can specify conditions for restricting
  736. groups using the <methodname>having()</methodname> method. Its usage is similar to that of the
  737. <methodname>where()</methodname> method. The first argument is a string containing a <acronym>SQL</acronym>
  738. expression. The optional second argument is a value that is used to replace a
  739. positional parameter placeholder in the <acronym>SQL</acronym> expression. Expressions given in
  740. multiple invocations of the <methodname>having()</methodname> method are combined using the
  741. Boolean <constant>AND</constant> operator, or the <code>OR</code> operator if you use the
  742. <methodname>orHaving()</methodname> method.
  743. </para>
  744. <example id="zend.db.select.building.having.example">
  745. <title>Example of the having() method</title>
  746. <programlisting language="php"><![CDATA[
  747. // Build this query:
  748. // SELECT p."product_id", COUNT(*) AS line_items_per_product
  749. // FROM "products" AS p JOIN "line_items" AS l
  750. // ON p.product_id = l.product_id
  751. // GROUP BY p.product_id
  752. // HAVING line_items_per_product > 10
  753. $select = $db->select()
  754. ->from(array('p' => 'products'),
  755. array('product_id'))
  756. ->join(array('l' => 'line_items'),
  757. 'p.product_id = l.product_id',
  758. array('line_items_per_product' => 'COUNT(*)'))
  759. ->group('p.product_id')
  760. ->having('line_items_per_product > 10');
  761. ]]></programlisting>
  762. </example>
  763. <note>
  764. <para>
  765. No quoting is applied to expressions given to the <methodname>having()</methodname> or
  766. <methodname>orHaving()</methodname> methods. If you have column names that need to be
  767. quoted, you must use <methodname>quoteIdentifier()</methodname> as you form the string for
  768. the condition.
  769. </para>
  770. </note>
  771. </sect3>
  772. <sect3 id="zend.db.select.building.order">
  773. <title>Adding an ORDER BY Clause</title>
  774. <para>
  775. In <acronym>SQL</acronym>, the <code>ORDER BY</code> clause specifies one or more columns or
  776. expressions by which the result set of a query is sorted. If multiple columns are
  777. listed, the secondary columns are used to resolve ties; the sort order is
  778. determined by the secondary columns if the preceding columns contain identical
  779. values. The default sorting is from least value to greatest value. You can also
  780. sort by greatest value to least value for a given column in the list by specifying
  781. the keyword <constant>DESC</constant> after that column.
  782. </para>
  783. <para>
  784. In <classname>Zend_Db_Select</classname>, you can use the <methodname>order()</methodname>
  785. method to specify a column or an array of columns by which to sort. Each element of
  786. the array is a string naming a column. Optionally with the <constant>ASC</constant>
  787. <constant>DESC</constant> keyword following it, separated by a space.
  788. </para>
  789. <para>
  790. Like in the <methodname>from()</methodname> and <methodname>group()</methodname> methods, column names are
  791. quoted as identifiers, unless they contain parentheses or are an object of
  792. type <classname>Zend_Db_Expr</classname>.
  793. </para>
  794. <example id="zend.db.select.building.order.example">
  795. <title>Example of the order() method</title>
  796. <programlisting language="php"><![CDATA[
  797. // Build this query:
  798. // SELECT p."product_id", COUNT(*) AS line_items_per_product
  799. // FROM "products" AS p JOIN "line_items" AS l
  800. // ON p.product_id = l.product_id
  801. // GROUP BY p.product_id
  802. // ORDER BY "line_items_per_product" DESC, "product_id"
  803. $select = $db->select()
  804. ->from(array('p' => 'products'),
  805. array('product_id'))
  806. ->join(array('l' => 'line_items'),
  807. 'p.product_id = l.product_id',
  808. array('line_items_per_product' => 'COUNT(*)'))
  809. ->group('p.product_id')
  810. ->order(array('line_items_per_product DESC',
  811. 'product_id'));
  812. ]]></programlisting>
  813. </example>
  814. </sect3>
  815. <sect3 id="zend.db.select.building.limit">
  816. <title>Adding a LIMIT Clause</title>
  817. <para>
  818. Some <acronym>RDBMS</acronym> brands extend <acronym>SQL</acronym> with a query clause known as the <constant>LIMIT</constant>
  819. clause. This clause reduces the number of rows in the result set to at most a
  820. number you specify. You can also specify to skip a number of rows before starting
  821. to output. This feature makes it easy to take a subset of a result set, for example
  822. when displaying query results on progressive pages of output.
  823. </para>
  824. <para>
  825. In <classname>Zend_Db_Select</classname>, you can use the <methodname>limit()</methodname>
  826. method to specify the count of rows and the number of rows to skip. The first
  827. argument to this method is the desired count of rows. The second argument is the
  828. number of rows to skip.
  829. </para>
  830. <example id="zend.db.select.building.limit.example">
  831. <title>Example of the limit() method</title>
  832. <programlisting language="php"><![CDATA[
  833. // Build this query:
  834. // SELECT p."product_id", p."product_name"
  835. // FROM "products" AS p
  836. // LIMIT 10, 20
  837. $select = $db->select()
  838. ->from(array('p' => 'products'),
  839. array('product_id', 'product_name'))
  840. ->limit(10, 20);
  841. ]]></programlisting>
  842. </example>
  843. <note>
  844. <para>
  845. The <constant>LIMIT</constant> syntax is not supported by all <acronym>RDBMS</acronym> brands. Some <acronym>RDBMS</acronym>
  846. require different syntax to support similar functionality. Each
  847. <classname>Zend_Db_Adapter_Abstract</classname> class includes a method to
  848. produce <acronym>SQL</acronym> appropriate for that <acronym>RDBMS</acronym>.
  849. </para>
  850. </note>
  851. <para>
  852. Use the <methodname>limitPage()</methodname> method for an alternative way to specify row count
  853. and offset. This method allows you to limit the result set to one of a series of
  854. fixed-length subsets of rows from the query's total result set. In other words, you
  855. specify the length of a "page" of results, and the ordinal number of the single
  856. page of results you want the query to return. The page number is the first argument
  857. of the <methodname>limitPage()</methodname> method, and the page length is the second argument.
  858. Both arguments are required; they have no default values.
  859. </para>
  860. <example id="zend.db.select.building.limit.example2">
  861. <title>Example of the limitPage() method</title>
  862. <programlisting language="php"><![CDATA[
  863. // Build this query:
  864. // SELECT p."product_id", p."product_name"
  865. // FROM "products" AS p
  866. // LIMIT 10, 20
  867. $select = $db->select()
  868. ->from(array('p' => 'products'),
  869. array('product_id', 'product_name'))
  870. ->limitPage(2, 10);
  871. ]]></programlisting>
  872. </example>
  873. </sect3>
  874. <sect3 id="zend.db.select.building.distinct">
  875. <title>Adding the DISTINCT Query Modifier</title>
  876. <para>
  877. The <methodname>distinct()</methodname> method enables you to add the <constant>DISTINCT</constant>
  878. keyword to your <acronym>SQL</acronym> query.
  879. </para>
  880. <example id="zend.db.select.building.distinct.example">
  881. <title>Example of the distinct() method</title>
  882. <programlisting language="php"><![CDATA[
  883. // Build this query:
  884. // SELECT DISTINCT p."product_name"
  885. // FROM "products" AS p
  886. $select = $db->select()
  887. ->distinct()
  888. ->from(array('p' => 'products'), 'product_name');
  889. ]]></programlisting>
  890. </example>
  891. </sect3>
  892. <sect3 id="zend.db.select.building.for-update">
  893. <title>Adding the FOR UPDATE Query Modifier</title>
  894. <para>
  895. The <methodname>forUpdate()</methodname> method enables you to add the <code>FOR UPDATE</code>
  896. modifier to your <acronym>SQL</acronym> query.
  897. </para>
  898. <example id="zend.db.select.building.for-update.example">
  899. <title>Example of forUpdate() method</title>
  900. <programlisting language="php"><![CDATA[
  901. // Build this query:
  902. // SELECT FOR UPDATE p.*
  903. // FROM "products" AS p
  904. $select = $db->select()
  905. ->forUpdate()
  906. ->from(array('p' => 'products'));
  907. ]]></programlisting>
  908. </example>
  909. </sect3>
  910. </sect2>
  911. <sect2 id="zend.db.select.execute">
  912. <title>Executing Select Queries</title>
  913. <para>
  914. This section describes how to execute the query represented by a
  915. <classname>Zend_Db_Select</classname> object.
  916. </para>
  917. <sect3 id="zend.db.select.execute.query-adapter">
  918. <title>Executing Select Queries from the Db Adapter</title>
  919. <para>
  920. You can execute the query represented by the <classname>Zend_Db_Select</classname>
  921. object by passing it as the first argument to the <methodname>query()</methodname> method of a
  922. <classname>Zend_Db_Adapter_Abstract</classname> object. Use the
  923. <classname>Zend_Db_Select</classname> objects instead of a string query.
  924. </para>
  925. <para>
  926. The <methodname>query()</methodname> method returns an object of type
  927. <classname>Zend_Db_Statement</classname> or <acronym>PDO</acronym>Statement, depending on the adapter
  928. type.
  929. </para>
  930. <example id="zend.db.select.execute.query-adapter.example">
  931. <title>Example using the Db adapter's query() method</title>
  932. <programlisting language="php"><![CDATA[
  933. $select = $db->select()
  934. ->from('products');
  935. $stmt = $db->query($select);
  936. $result = $stmt->fetchAll();
  937. ]]></programlisting>
  938. </example>
  939. </sect3>
  940. <sect3 id="zend.db.select.execute.query-select">
  941. <title>Executing Select Queries from the Object</title>
  942. <para>
  943. As an alternative to using the <methodname>query()</methodname> method of the adapter object,
  944. you can use the <methodname>query()</methodname> method of the
  945. <classname>Zend_Db_Select</classname> object. Both methods return an object of type
  946. <classname>Zend_Db_Statement</classname> or <acronym>PDO</acronym>Statement, depending on the adapter
  947. type.
  948. </para>
  949. <example id="zend.db.select.execute.query-select.example">
  950. <title>Example using the Select object's query method</title>
  951. <programlisting language="php"><![CDATA[
  952. $select = $db->select()
  953. ->from('products');
  954. $stmt = $select->query();
  955. $result = $stmt->fetchAll();
  956. ]]></programlisting>
  957. </example>
  958. </sect3>
  959. <sect3 id="zend.db.select.execute.tostring">
  960. <title>Converting a Select Object to a SQL String</title>
  961. <para>
  962. If you need access to a string representation of the <acronym>SQL</acronym> query corresponding to the
  963. <classname>Zend_Db_Select</classname> object, use the <methodname>__toString()</methodname>
  964. method.
  965. </para>
  966. <example id="zend.db.select.execute.tostring.example">
  967. <title>Example of the __toString() method</title>
  968. <programlisting language="php"><![CDATA[
  969. $select = $db->select()
  970. ->from('products');
  971. $sql = $select->__toString();
  972. echo "$sql\n";
  973. // The output is the string:
  974. // SELECT * FROM "products"
  975. ]]></programlisting>
  976. </example>
  977. </sect3>
  978. </sect2>
  979. <sect2 id="zend.db.select.other">
  980. <title>Other methods</title>
  981. <para>
  982. This section describes other methods of the <classname>Zend_Db_Select</classname> class
  983. that are not covered above: <methodname>getPart()</methodname> and <methodname>reset()</methodname>.
  984. </para>
  985. <sect3 id="zend.db.select.other.get-part">
  986. <title>Retrieving Parts of the Select Object</title>
  987. <para>
  988. The <methodname>getPart()</methodname> method returns a representation of one part of your <acronym>SQL</acronym>
  989. query. For example, you can use this method to return the array of expressions for
  990. the <constant>WHERE</constant> clause, or the array of columns (or column expressions) that
  991. are in the <constant>SELECT</constant> list, or the values of the count and offset for the
  992. <constant>LIMIT</constant> clause.
  993. </para>
  994. <para>
  995. The return value is not a string containing a fragment of <acronym>SQL</acronym> syntax. The return
  996. value is an internal representation, which is typically an array structure
  997. containing values and expressions. Each part of the query has a different
  998. structure.
  999. </para>
  1000. <para>
  1001. The single argument to the <methodname>getPart()</methodname> method is a string that
  1002. identifies which part of the Select query to return. For example, the string
  1003. <code>'from'</code> identifies the part of the Select object that stores
  1004. information about the tables in the <constant>FROM</constant> clause, including joined
  1005. tables.
  1006. </para>
  1007. <para>
  1008. The <classname>Zend_Db_Select</classname> class defines constants you can use for
  1009. parts of the <acronym>SQL</acronym> query. You can use these constant definitions, or you can the
  1010. literal strings.
  1011. </para>
  1012. <table id="zend.db.select.other.get-part.table">
  1013. <title>Constants used by getPart() and reset()</title>
  1014. <tgroup cols="2">
  1015. <thead>
  1016. <row>
  1017. <entry>Constant</entry>
  1018. <entry>String value</entry>
  1019. </row>
  1020. </thead>
  1021. <tbody>
  1022. <row>
  1023. <entry><constant>Zend_Db_Select::DISTINCT</constant></entry>
  1024. <entry><code>'distinct'</code></entry>
  1025. </row>
  1026. <row>
  1027. <entry><constant>Zend_Db_Select::FOR_UPDATE</constant></entry>
  1028. <entry><code>'forupdate'</code></entry>
  1029. </row>
  1030. <row>
  1031. <entry><constant>Zend_Db_Select::COLUMNS</constant></entry>
  1032. <entry><code>'columns'</code></entry>
  1033. </row>
  1034. <row>
  1035. <entry><constant>Zend_Db_Select::FROM</constant></entry>
  1036. <entry><code>'from'</code></entry>
  1037. </row>
  1038. <row>
  1039. <entry><constant>Zend_Db_Select::WHERE</constant></entry>
  1040. <entry><code>'where'</code></entry>
  1041. </row>
  1042. <row>
  1043. <entry><constant>Zend_Db_Select::GROUP</constant></entry>
  1044. <entry><code>'group'</code></entry>
  1045. </row>
  1046. <row>
  1047. <entry><constant>Zend_Db_Select::HAVING</constant></entry>
  1048. <entry><code>'having'</code></entry>
  1049. </row>
  1050. <row>
  1051. <entry><constant>Zend_Db_Select::ORDER</constant></entry>
  1052. <entry><code>'order'</code></entry>
  1053. </row>
  1054. <row>
  1055. <entry><constant>Zend_Db_Select::LIMIT_COUNT</constant></entry>
  1056. <entry><code>'limitcount'</code></entry>
  1057. </row>
  1058. <row>
  1059. <entry><constant>Zend_Db_Select::LIMIT_OFFSET</constant></entry>
  1060. <entry><code>'limitoffset'</code></entry>
  1061. </row>
  1062. </tbody>
  1063. </tgroup>
  1064. </table>
  1065. <example id="zend.db.select.other.get-part.example">
  1066. <title>Example of the getPart() method</title>
  1067. <programlisting language="php"><![CDATA[
  1068. $select = $db->select()
  1069. ->from('products')
  1070. ->order('product_id');
  1071. // You can use a string literal to specify the part
  1072. $orderData = $select->getPart( 'order' );
  1073. // You can use a constant to specify the same part
  1074. $orderData = $select->getPart( Zend_Db_Select::ORDER );
  1075. // The return value may be an array structure, not a string.
  1076. // Each part has a different structure.
  1077. print_r( $orderData );
  1078. ]]></programlisting>
  1079. </example>
  1080. </sect3>
  1081. <sect3 id="zend.db.select.other.reset">
  1082. <title>Resetting Parts of the Select Object</title>
  1083. <para>
  1084. The <methodname>reset()</methodname> method enables you to clear one specified part of the <acronym>SQL</acronym>
  1085. query, or else clear all parts of the <acronym>SQL</acronym> query if you omit the argument.
  1086. </para>
  1087. <para>
  1088. The single argument is optional. You can specify the part of the query to clear,
  1089. using the same strings you used in the argument to the <methodname>getPart()</methodname>
  1090. method. The part of the query you specify is reset to a default state.
  1091. </para>
  1092. <para>
  1093. If you omit the parameter, <methodname>reset()</methodname> changes all parts of the query to
  1094. their default state. This makes the <classname>Zend_Db_Select</classname> object
  1095. equivalent to a new object, as though you had just instantiated it.
  1096. </para>
  1097. <example id="zend.db.select.other.reset.example">
  1098. <title>Example of the reset() method</title>
  1099. <programlisting language="php"><![CDATA[
  1100. // Build this query:
  1101. // SELECT p.*
  1102. // FROM "products" AS p
  1103. // ORDER BY "product_name"
  1104. $select = $db->select()
  1105. ->from(array('p' => 'products')
  1106. ->order('product_name');
  1107. // Changed requirement, instead order by a different columns:
  1108. // SELECT p.*
  1109. // FROM "products" AS p
  1110. // ORDER BY "product_id"
  1111. // Clear one part so we can redefine it
  1112. $select->reset( Zend_Db_Select::ORDER );
  1113. // And specify a different column
  1114. $select->order('product_id');
  1115. // Clear all parts of the query
  1116. $select->reset();
  1117. ]]></programlisting>
  1118. </example>
  1119. </sect3>
  1120. </sect2>
  1121. </sect1>
  1122. <!--
  1123. vim:se ts=4 sw=4 et:
  1124. -->