Zend_Db_Select.xml 59 KB

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