Zend_Db_Select.xml 59 KB

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