PageRenderTime 31ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/runtime/lib/query/Criteria.php

https://github.com/1989gaurav/Propel
PHP | 1781 lines | 773 code | 197 blank | 811 comment | 95 complexity | 60a3b773f252161e2463b914c526656d MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. /**
  10. * This is a utility class for holding criteria information for a query.
  11. *
  12. * BasePeer constructs SQL statements based on the values in this class.
  13. *
  14. * @author Hans Lellelid <hans@xmpl.org> (Propel)
  15. * @author Kaspars Jaudzems <kaspars.jaudzems@inbox.lv> (Propel)
  16. * @author Frank Y. Kim <frank.kim@clearink.com> (Torque)
  17. * @author John D. McNally <jmcnally@collab.net> (Torque)
  18. * @author Brett McLaughlin <bmclaugh@algx.net> (Torque)
  19. * @author Eric Dobbs <eric@dobbse.net> (Torque)
  20. * @author Henning P. Schmiedehausen <hps@intermeta.de> (Torque)
  21. * @author Sam Joseph <sam@neurogrid.com> (Torque)
  22. * @version $Revision$
  23. * @package propel.runtime.query
  24. */
  25. class Criteria implements IteratorAggregate
  26. {
  27. /** Comparison type. */
  28. const EQUAL = "=";
  29. /** Comparison type. */
  30. const NOT_EQUAL = "<>";
  31. /** Comparison type. */
  32. const ALT_NOT_EQUAL = "!=";
  33. /** Comparison type. */
  34. const GREATER_THAN = ">";
  35. /** Comparison type. */
  36. const LESS_THAN = "<";
  37. /** Comparison type. */
  38. const GREATER_EQUAL = ">=";
  39. /** Comparison type. */
  40. const LESS_EQUAL = "<=";
  41. /** Comparison type. */
  42. const LIKE = " LIKE ";
  43. /** Comparison type. */
  44. const NOT_LIKE = " NOT LIKE ";
  45. /** Comparison for array column types */
  46. const CONTAINS_ALL = "CONTAINS_ALL";
  47. /** Comparison for array column types */
  48. const CONTAINS_SOME = "CONTAINS_SOME";
  49. /** Comparison for array column types */
  50. const CONTAINS_NONE = "CONTAINS_NONE";
  51. /** PostgreSQL comparison type */
  52. const ILIKE = " ILIKE ";
  53. /** PostgreSQL comparison type */
  54. const NOT_ILIKE = " NOT ILIKE ";
  55. /** Comparison type. */
  56. const CUSTOM = "CUSTOM";
  57. /** Comparison type for update */
  58. const CUSTOM_EQUAL = "CUSTOM_EQUAL";
  59. /** Comparison type. */
  60. const DISTINCT = "DISTINCT";
  61. /** Comparison type. */
  62. const IN = " IN ";
  63. /** Comparison type. */
  64. const NOT_IN = " NOT IN ";
  65. /** Comparison type. */
  66. const ALL = "ALL";
  67. /** Comparison type. */
  68. const JOIN = "JOIN";
  69. /** Binary math operator: AND */
  70. const BINARY_AND = "&";
  71. /** Binary math operator: OR */
  72. const BINARY_OR = "|";
  73. /** "Order by" qualifier - ascending */
  74. const ASC = "ASC";
  75. /** "Order by" qualifier - descending */
  76. const DESC = "DESC";
  77. /** "IS NULL" null comparison */
  78. const ISNULL = " IS NULL ";
  79. /** "IS NOT NULL" null comparison */
  80. const ISNOTNULL = " IS NOT NULL ";
  81. /** "CURRENT_DATE" ANSI SQL function */
  82. const CURRENT_DATE = "CURRENT_DATE";
  83. /** "CURRENT_TIME" ANSI SQL function */
  84. const CURRENT_TIME = "CURRENT_TIME";
  85. /** "CURRENT_TIMESTAMP" ANSI SQL function */
  86. const CURRENT_TIMESTAMP = "CURRENT_TIMESTAMP";
  87. /** "LEFT JOIN" SQL statement */
  88. const LEFT_JOIN = "LEFT JOIN";
  89. /** "RIGHT JOIN" SQL statement */
  90. const RIGHT_JOIN = "RIGHT JOIN";
  91. /** "INNER JOIN" SQL statement */
  92. const INNER_JOIN = "INNER JOIN";
  93. /** logical OR operator */
  94. const LOGICAL_OR = "OR";
  95. /** logical AND operator */
  96. const LOGICAL_AND = "AND";
  97. protected $ignoreCase = false;
  98. protected $singleRecord = false;
  99. /**
  100. * Storage of select data. Collection of column names.
  101. * @var array
  102. */
  103. protected $selectColumns = array();
  104. /**
  105. * Storage of aliased select data. Collection of column names.
  106. * @var array
  107. */
  108. protected $asColumns = array();
  109. /**
  110. * Storage of select modifiers data. Collection of modifier names.
  111. * @var array
  112. */
  113. protected $selectModifiers = array();
  114. /**
  115. * Storage of conditions data. Collection of Criterion objects.
  116. * @var array
  117. */
  118. protected $map = array();
  119. /**
  120. * Storage of ordering data. Collection of column names.
  121. * @var array
  122. */
  123. protected $orderByColumns = array();
  124. /**
  125. * Storage of grouping data. Collection of column names.
  126. * @var array
  127. */
  128. protected $groupByColumns = array();
  129. /**
  130. * Storage of having data.
  131. * @var Criterion
  132. */
  133. protected $having = null;
  134. /**
  135. * Storage of join data. colleciton of Join objects.
  136. * @var array
  137. */
  138. protected $joins = array();
  139. protected $selectQueries = array();
  140. /**
  141. * The name of the database.
  142. * @var string
  143. */
  144. protected $dbName;
  145. /**
  146. * The primary table for this Criteria.
  147. * Useful in cases where there are no select or where
  148. * columns.
  149. * @var string
  150. */
  151. protected $primaryTableName;
  152. /** The name of the database as given in the contructor. */
  153. protected $originalDbName;
  154. /**
  155. * To limit the number of rows to return. <code>0</code> means return all
  156. * rows.
  157. */
  158. protected $limit = 0;
  159. /** To start the results at a row other than the first one. */
  160. protected $offset = 0;
  161. /**
  162. * Comment to add to the SQL query
  163. * @var string
  164. */
  165. protected $queryComment;
  166. // flag to note that the criteria involves a blob.
  167. protected $blobFlag = null;
  168. protected $aliases = array();
  169. protected $useTransaction = false;
  170. /**
  171. * Storage for Criterions expected to be combined
  172. * @var array
  173. */
  174. protected $namedCriterions = array();
  175. /**
  176. * Default operator for combination of criterions
  177. * @see addUsingOperator
  178. * @var string Criteria::LOGICAL_AND or Criteria::LOGICAL_OR
  179. */
  180. protected $defaultCombineOperator = Criteria::LOGICAL_AND;
  181. // flags for boolean functions
  182. protected $conditionalProxy = null;
  183. /**
  184. * Creates a new instance with the default capacity which corresponds to
  185. * the specified database.
  186. *
  187. * @param dbName The dabase name.
  188. */
  189. public function __construct($dbName = null)
  190. {
  191. $this->setDbName($dbName);
  192. $this->originalDbName = $dbName;
  193. }
  194. /**
  195. * Implementing SPL IteratorAggregate interface. This allows
  196. * you to foreach () over a Criteria object.
  197. */
  198. public function getIterator()
  199. {
  200. return new CriterionIterator($this);
  201. }
  202. /**
  203. * Get the criteria map, i.e. the array of Criterions
  204. * @return array
  205. */
  206. public function getMap()
  207. {
  208. return $this->map;
  209. }
  210. /**
  211. * Brings this criteria back to its initial state, so that it
  212. * can be reused as if it was new. Except if the criteria has grown in
  213. * capacity, it is left at the current capacity.
  214. * @return void
  215. */
  216. public function clear()
  217. {
  218. $this->map = array();
  219. $this->namedCriterions = array();
  220. $this->ignoreCase = false;
  221. $this->singleRecord = false;
  222. $this->selectModifiers = array();
  223. $this->selectColumns = array();
  224. $this->orderByColumns = array();
  225. $this->groupByColumns = array();
  226. $this->having = null;
  227. $this->asColumns = array();
  228. $this->joins = array();
  229. $this->selectQueries = array();
  230. $this->dbName = $this->originalDbName;
  231. $this->offset = 0;
  232. $this->limit = 0;
  233. $this->blobFlag = null;
  234. $this->aliases = array();
  235. $this->useTransaction = false;
  236. $this->ifLvlCount = false;
  237. $this->wasTrue = false;
  238. }
  239. /**
  240. * Add an AS clause to the select columns. Usage:
  241. *
  242. * <code>
  243. * Criteria myCrit = new Criteria();
  244. * myCrit->addAsColumn("alias", "ALIAS(".MyPeer::ID.")");
  245. * </code>
  246. *
  247. * @param string $name Wanted Name of the column (alias).
  248. * @param string $clause SQL clause to select from the table
  249. *
  250. * If the name already exists, it is replaced by the new clause.
  251. *
  252. * @return Criteria A modified Criteria object.
  253. */
  254. public function addAsColumn($name, $clause)
  255. {
  256. $this->asColumns[$name] = $clause;
  257. return $this;
  258. }
  259. /**
  260. * Get the column aliases.
  261. *
  262. * @return array An assoc array which map the column alias names
  263. * to the alias clauses.
  264. */
  265. public function getAsColumns()
  266. {
  267. return $this->asColumns;
  268. }
  269. /**
  270. * Returns the column name associated with an alias (AS-column).
  271. *
  272. * @param string $alias
  273. * @return string $string
  274. */
  275. public function getColumnForAs($as)
  276. {
  277. if (isset($this->asColumns[$as])) {
  278. return $this->asColumns[$as];
  279. }
  280. }
  281. /**
  282. * Allows one to specify an alias for a table that can
  283. * be used in various parts of the SQL.
  284. *
  285. * @param string $alias
  286. * @param string $table
  287. *
  288. * @return Criteria A modified Criteria object.
  289. */
  290. public function addAlias($alias, $table)
  291. {
  292. $this->aliases[$alias] = $table;
  293. return $this;
  294. }
  295. /**
  296. * Remove an alias for a table (useful when merging Criterias).
  297. *
  298. * @param string $alias
  299. *
  300. * @return Criteria A modified Criteria object.
  301. */
  302. public function removeAlias($alias)
  303. {
  304. unset($this->aliases[$alias]);
  305. return $this;
  306. }
  307. /**
  308. * Returns the aliases for this Criteria
  309. *
  310. * @return array
  311. */
  312. public function getAliases()
  313. {
  314. return $this->aliases;
  315. }
  316. /**
  317. * Returns the table name associated with an alias.
  318. *
  319. * @param string $alias
  320. * @return string $string
  321. */
  322. public function getTableForAlias($alias)
  323. {
  324. if (isset($this->aliases[$alias])) {
  325. return $this->aliases[$alias];
  326. }
  327. }
  328. /**
  329. * Returns the table name and alias based on a table alias or name.
  330. * Use this method to get the details of a table name that comes in a clause,
  331. * which can be either a table name or an alias name.
  332. *
  333. * @param string $tableAliasOrName
  334. * @return array($tableName, $tableAlias)
  335. */
  336. public function getTableNameAndAlias($tableAliasOrName)
  337. {
  338. if (isset($this->aliases[$tableAliasOrName])) {
  339. return array($this->aliases[$tableAliasOrName], $tableAliasOrName);
  340. } else {
  341. return array($tableAliasOrName, null);
  342. }
  343. }
  344. /**
  345. * Get the keys of the criteria map, i.e. the list of columns bearing a condition
  346. * <code>
  347. * print_r($c->keys());
  348. * => array('book.price', 'book.title', 'author.first_name')
  349. * </code>
  350. *
  351. * @return array
  352. */
  353. public function keys()
  354. {
  355. return array_keys($this->map);
  356. }
  357. /**
  358. * Does this Criteria object contain the specified key?
  359. *
  360. * @param string $column [table.]column
  361. * @return boolean True if this Criteria object contain the specified key.
  362. */
  363. public function containsKey($column)
  364. {
  365. // must use array_key_exists() because the key could
  366. // exist but have a NULL value (that'd be valid).
  367. return array_key_exists($column, $this->map);
  368. }
  369. /**
  370. * Does this Criteria object contain the specified key and does it have a value set for the key
  371. *
  372. * @param string $column [table.]column
  373. * @return boolean True if this Criteria object contain the specified key and a value for that key
  374. */
  375. public function keyContainsValue($column)
  376. {
  377. // must use array_key_exists() because the key could
  378. // exist but have a NULL value (that'd be valid).
  379. return (array_key_exists($column, $this->map) && ($this->map[$column]->getValue() !== null) );
  380. }
  381. /**
  382. * Whether this Criteria has any where columns.
  383. *
  384. * This counts conditions added with the add() method.
  385. *
  386. * @return boolean
  387. * @see add()
  388. */
  389. public function hasWhereClause()
  390. {
  391. return !empty($this->map);
  392. }
  393. /**
  394. * Will force the sql represented by this criteria to be executed within
  395. * a transaction. This is here primarily to support the oid type in
  396. * postgresql. Though it can be used to require any single sql statement
  397. * to use a transaction.
  398. * @return void
  399. */
  400. public function setUseTransaction($v)
  401. {
  402. $this->useTransaction = (boolean) $v;
  403. }
  404. /**
  405. * Whether the sql command specified by this criteria must be wrapped
  406. * in a transaction.
  407. *
  408. * @return boolean
  409. */
  410. public function isUseTransaction()
  411. {
  412. return $this->useTransaction;
  413. }
  414. /**
  415. * Method to return criteria related to columns in a table.
  416. *
  417. * Make sure you call containsKey($column) prior to calling this method,
  418. * since no check on the existence of the $column is made in this method.
  419. *
  420. * @param string $column Column name.
  421. * @return Criterion A Criterion object.
  422. */
  423. public function getCriterion($column)
  424. {
  425. return $this->map[$column];
  426. }
  427. /**
  428. * Method to return the latest Criterion in a table.
  429. *
  430. * @return Criterion A Criterion or null no Criterion is added.
  431. */
  432. public function getLastCriterion()
  433. {
  434. if($cnt = count($this->map)) {
  435. $map = array_values($this->map);
  436. return $map[$cnt - 1];
  437. }
  438. return null;
  439. }
  440. /**
  441. * Method to return criterion that is not added automatically
  442. * to this Criteria. This can be used to chain the
  443. * Criterions to form a more complex where clause.
  444. *
  445. * @param string $column Full name of column (for example TABLE.COLUMN).
  446. * @param mixed $value
  447. * @param string $comparison
  448. * @return Criterion
  449. */
  450. public function getNewCriterion($column, $value = null, $comparison = self::EQUAL)
  451. {
  452. return new Criterion($this, $column, $value, $comparison);
  453. }
  454. /**
  455. * Method to return a String table name.
  456. *
  457. * @param string $name Name of the key.
  458. * @return string The value of the object at key.
  459. */
  460. public function getColumnName($name)
  461. {
  462. if (isset($this->map[$name])) {
  463. return $this->map[$name]->getColumn();
  464. }
  465. return null;
  466. }
  467. /**
  468. * Shortcut method to get an array of columns indexed by table.
  469. * <code>
  470. * print_r($c->getTablesColumns());
  471. * => array(
  472. * 'book' => array('book.price', 'book.title'),
  473. * 'author' => array('author.first_name')
  474. * )
  475. * </code>
  476. *
  477. * @return array array(table => array(table.column1, table.column2))
  478. */
  479. public function getTablesColumns()
  480. {
  481. $tables = array();
  482. foreach ($this->keys() as $key) {
  483. $tableName = substr($key, 0, strrpos($key, '.' ));
  484. $tables[$tableName][] = $key;
  485. }
  486. return $tables;
  487. }
  488. /**
  489. * Method to return a comparison String.
  490. *
  491. * @param string $key String name of the key.
  492. * @return string A String with the value of the object at key.
  493. */
  494. public function getComparison($key)
  495. {
  496. if ( isset ( $this->map[$key] ) ) {
  497. return $this->map[$key]->getComparison();
  498. }
  499. return null;
  500. }
  501. /**
  502. * Get the Database(Map) name.
  503. *
  504. * @return string A String with the Database(Map) name.
  505. */
  506. public function getDbName()
  507. {
  508. return $this->dbName;
  509. }
  510. /**
  511. * Set the DatabaseMap name. If <code>null</code> is supplied, uses value
  512. * provided by <code>Propel::getDefaultDB()</code>.
  513. *
  514. * @param string $dbName The Database (Map) name.
  515. * @return void
  516. */
  517. public function setDbName($dbName = null)
  518. {
  519. $this->dbName = ($dbName === null ? Propel::getDefaultDB() : $dbName);
  520. }
  521. /**
  522. * Get the primary table for this Criteria.
  523. *
  524. * This is useful for cases where a Criteria may not contain
  525. * any SELECT columns or WHERE columns. This must be explicitly
  526. * set, of course, in order to be useful.
  527. *
  528. * @return string
  529. */
  530. public function getPrimaryTableName()
  531. {
  532. return $this->primaryTableName;
  533. }
  534. /**
  535. * Sets the primary table for this Criteria.
  536. *
  537. * This is useful for cases where a Criteria may not contain
  538. * any SELECT columns or WHERE columns. This must be explicitly
  539. * set, of course, in order to be useful.
  540. *
  541. * @param string $v
  542. */
  543. public function setPrimaryTableName($tableName)
  544. {
  545. $this->primaryTableName = $tableName;
  546. }
  547. /**
  548. * Method to return a String table name.
  549. *
  550. * @param string $name The name of the key.
  551. * @return string The value of table for criterion at key.
  552. */
  553. public function getTableName($name)
  554. {
  555. if (isset($this->map[$name])) {
  556. return $this->map[$name]->getTable();
  557. }
  558. return null;
  559. }
  560. /**
  561. * Method to return the value that was added to Criteria.
  562. *
  563. * @param string $name A String with the name of the key.
  564. * @return mixed The value of object at key.
  565. */
  566. public function getValue($name)
  567. {
  568. if (isset($this->map[$name])) {
  569. return $this->map[$name]->getValue();
  570. }
  571. return null;
  572. }
  573. /**
  574. * An alias to getValue() -- exposing a Hashtable-like interface.
  575. *
  576. * @param string $key An Object.
  577. * @return mixed The value within the Criterion (not the Criterion object).
  578. */
  579. public function get($key)
  580. {
  581. return $this->getValue($key);
  582. }
  583. /**
  584. * Overrides Hashtable put, so that this object is returned
  585. * instead of the value previously in the Criteria object.
  586. * The reason is so that it more closely matches the behavior
  587. * of the add() methods. If you want to get the previous value
  588. * then you should first Criteria.get() it yourself. Note, if
  589. * you attempt to pass in an Object that is not a String, it will
  590. * throw a NPE. The reason for this is that none of the add()
  591. * methods support adding anything other than a String as a key.
  592. *
  593. * @param string $key
  594. * @param mixed $value
  595. * @return Instance of self.
  596. */
  597. public function put($key, $value)
  598. {
  599. return $this->add($key, $value);
  600. }
  601. /**
  602. * Copies all of the mappings from the specified Map to this Criteria
  603. * These mappings will replace any mappings that this Criteria had for any
  604. * of the keys currently in the specified Map.
  605. *
  606. * if the map was another Criteria, its attributes are copied to this
  607. * Criteria, overwriting previous settings.
  608. *
  609. * @param mixed $t Mappings to be stored in this map.
  610. */
  611. public function putAll($t)
  612. {
  613. if (is_array($t)) {
  614. foreach ($t as $key=>$value) {
  615. if ($value instanceof Criterion) {
  616. $this->map[$key] = $value;
  617. } else {
  618. $this->put($key, $value);
  619. }
  620. }
  621. } elseif ($t instanceof Criteria) {
  622. $this->joins = $t->joins;
  623. }
  624. }
  625. /**
  626. * This method adds a new criterion to the list of criterias.
  627. * If a criterion for the requested column already exists, it is
  628. * replaced. If is used as follow:
  629. *
  630. * <code>
  631. * $crit = new Criteria();
  632. * $crit->add($column, $value, Criteria::GREATER_THAN);
  633. * </code>
  634. *
  635. * Any comparison can be used.
  636. *
  637. * The name of the table must be used implicitly in the column name,
  638. * so the Column name must be something like 'TABLE.id'.
  639. *
  640. * @param string $critOrColumn The column to run the comparison on, or Criterion object.
  641. * @param mixed $value
  642. * @param string $comparison A String.
  643. *
  644. * @return A modified Criteria object.
  645. */
  646. public function add($p1, $value = null, $comparison = null)
  647. {
  648. if ($p1 instanceof Criterion) {
  649. $this->map[$p1->getTable() . '.' . $p1->getColumn()] = $p1;
  650. } else {
  651. $criterion = new Criterion($this, $p1, $value, $comparison);
  652. $this->map[$p1] = $criterion;
  653. }
  654. return $this;
  655. }
  656. /**
  657. * This method creates a new criterion but keeps it for later use with combine()
  658. * Until combine() is called, the condition is not added to the query
  659. *
  660. * <code>
  661. * $crit = new Criteria();
  662. * $crit->addCond('cond1', $column1, $value1, Criteria::GREATER_THAN);
  663. * $crit->addCond('cond2', $column2, $value2, Criteria::EQUAL);
  664. * $crit->combine(array('cond1', 'cond2'), Criteria::LOGICAL_OR);
  665. * </code>
  666. *
  667. * Any comparison can be used.
  668. *
  669. * The name of the table must be used implicitly in the column name,
  670. * so the Column name must be something like 'TABLE.id'.
  671. *
  672. * @param string $name name to combine the criterion later
  673. * @param string $p1 The column to run the comparison on, or Criterion object.
  674. * @param mixed $value
  675. * @param string $comparison A String.
  676. *
  677. * @return A modified Criteria object.
  678. */
  679. public function addCond($name, $p1, $value = null, $comparison = null)
  680. {
  681. if ($p1 instanceof Criterion) {
  682. $this->namedCriterions[$name] = $p1;
  683. } else {
  684. $criterion = new Criterion($this, $p1, $value, $comparison);
  685. $this->namedCriterions[$name] = $criterion;
  686. }
  687. return $this;
  688. }
  689. /**
  690. * Combine several named criterions with a logical operator
  691. *
  692. * @param array $criterions array of the name of the criterions to combine
  693. * @param string $operator logical operator, either Criteria::LOGICAL_AND, or Criteria::LOGICAL_OR
  694. * @param string $name optional name to combine the criterion later
  695. */
  696. public function combine($criterions = array(), $operator = self::LOGICAL_AND, $name = null)
  697. {
  698. $operatorMethod = (strtoupper($operator) == self::LOGICAL_AND) ? 'addAnd' : 'addOr';
  699. $namedCriterions = array();
  700. foreach ($criterions as $key) {
  701. if (array_key_exists($key, $this->namedCriterions)) {
  702. $namedCriterions[]= $this->namedCriterions[$key];
  703. unset($this->namedCriterions[$key]);
  704. } else {
  705. throw new PropelException('Cannot combine unknown condition ' . $key);
  706. }
  707. }
  708. $firstCriterion = array_shift($namedCriterions);
  709. foreach ($namedCriterions as $criterion) {
  710. $firstCriterion->$operatorMethod($criterion);
  711. }
  712. if ($name === null) {
  713. $this->add($firstCriterion, null, null);
  714. } else {
  715. $this->addCond($name, $firstCriterion, null, null);
  716. }
  717. return $this;
  718. }
  719. /**
  720. * This is the way that you should add a join of two tables.
  721. * Example usage:
  722. * <code>
  723. * $c->addJoin(ProjectPeer::ID, FooPeer::PROJECT_ID, Criteria::LEFT_JOIN);
  724. * // LEFT JOIN FOO ON (PROJECT.ID = FOO.PROJECT_ID)
  725. * </code>
  726. *
  727. * @param mixed $left A String with the left side of the join.
  728. * @param mixed $right A String with the right side of the join.
  729. * @param mixed $joinType A String with the join operator
  730. * among Criteria::INNER_JOIN, Criteria::LEFT_JOIN,
  731. * and Criteria::RIGHT_JOIN
  732. *
  733. * @return Criteria A modified Criteria object.
  734. */
  735. public function addJoin($left, $right, $joinType = null)
  736. {
  737. if (is_array($left)) {
  738. $conditions = array();
  739. foreach ($left as $key => $value) {
  740. $condition = array($value, $right[$key]);
  741. $conditions []= $condition;
  742. }
  743. return $this->addMultipleJoin($conditions, $joinType);
  744. }
  745. $join = new Join();
  746. // is the left table an alias ?
  747. $dotpos = strrpos($left, '.');
  748. $leftTableAlias = substr($left, 0, $dotpos);
  749. $leftColumnName = substr($left, $dotpos + 1);
  750. list($leftTableName, $leftTableAlias) = $this->getTableNameAndAlias($leftTableAlias);
  751. // is the right table an alias ?
  752. $dotpos = strrpos($right, '.');
  753. $rightTableAlias = substr($right, 0, $dotpos);
  754. $rightColumnName = substr($right, $dotpos + 1);
  755. list($rightTableName, $rightTableAlias) = $this->getTableNameAndAlias($rightTableAlias);
  756. $join->addExplicitCondition(
  757. $leftTableName, $leftColumnName, $leftTableAlias,
  758. $rightTableName, $rightColumnName, $rightTableAlias,
  759. Join::EQUAL);
  760. $join->setJoinType($joinType);
  761. return $this->addJoinObject($join);
  762. }
  763. /**
  764. * Add a join with multiple conditions
  765. * @deprecated use Join::setJoinCondition($criterion) instead
  766. *
  767. * @see http://propel.phpdb.org/trac/ticket/167, http://propel.phpdb.org/trac/ticket/606
  768. *
  769. * Example usage:
  770. * $c->addMultipleJoin(array(
  771. * array(LeftPeer::LEFT_COLUMN, RightPeer::RIGHT_COLUMN), // if no third argument, defaults to Criteria::EQUAL
  772. * array(FoldersPeer::alias( 'fo', FoldersPeer::LFT ), FoldersPeer::alias( 'parent', FoldersPeer::RGT ), Criteria::LESS_EQUAL )
  773. * ),
  774. * Criteria::LEFT_JOIN
  775. * );
  776. *
  777. * @see addJoin()
  778. * @param array $conditions An array of conditions, each condition being an array (left, right, operator)
  779. * @param string $joinType A String with the join operator. Defaults to an implicit join.
  780. *
  781. * @return Criteria A modified Criteria object.
  782. */
  783. public function addMultipleJoin($conditions, $joinType = null)
  784. {
  785. $join = new Join();
  786. $joinCondition = null;
  787. foreach ($conditions as $condition) {
  788. $left = $condition[0];
  789. $right = $condition[1];
  790. if ($pos = strrpos($left, '.')) {
  791. $leftTableAlias = substr($left, 0, $pos);
  792. $leftColumnName = substr($left, $pos + 1);
  793. list($leftTableName, $leftTableAlias) = $this->getTableNameAndAlias($leftTableAlias);
  794. } else {
  795. list($leftTableName, $leftTableAlias) = array(null, null);
  796. $leftColumnName = $left;
  797. }
  798. if ($pos = strrpos($right, '.')) {
  799. $rightTableAlias = substr($right, 0, $pos);
  800. $rightColumnName = substr($right, $pos + 1);
  801. list($rightTableName, $rightTableAlias) = $this->getTableNameAndAlias($rightTableAlias);
  802. } else {
  803. list($rightTableName, $rightTableAlias) = array(null, null);
  804. $rightColumnName = $right;
  805. }
  806. if (!$join->getRightTableName()) {
  807. $join->setRightTableName($rightTableName);
  808. }
  809. if (!$join->getRightTableAlias()) {
  810. $join->setRightTableAlias($rightTableAlias);
  811. }
  812. $conditionClause = $leftTableAlias ? $leftTableAlias . '.' : ($leftTableName ? $leftTableName . '.' : '');
  813. $conditionClause .= $leftColumnName;
  814. $conditionClause .= isset($condition[2]) ? $condition[2] : JOIN::EQUAL;
  815. $conditionClause .= $rightTableAlias ? $rightTableAlias . '.' : ($rightTableName ? $rightTableName . '.' : '');
  816. $conditionClause .= $rightColumnName;
  817. $criterion = $this->getNewCriterion($leftTableName.'.'.$leftColumnName, $conditionClause, Criteria::CUSTOM);
  818. if (null === $joinCondition) {
  819. $joinCondition = $criterion;
  820. } else {
  821. $joinCondition = $joinCondition->addAnd($criterion);
  822. }
  823. }
  824. $join->setJoinType($joinType);
  825. $join->setJoinCondition($joinCondition);
  826. return $this->addJoinObject($join);
  827. }
  828. /**
  829. * Add a join object to the Criteria
  830. *
  831. * @param Join $join A join object
  832. *
  833. * @return Criteria A modified Criteria object
  834. */
  835. public function addJoinObject(Join $join)
  836. {
  837. if (!in_array($join, $this->joins)) { // compare equality, NOT identity
  838. $this->joins[] = $join;
  839. }
  840. return $this;
  841. }
  842. /**
  843. * Get the array of Joins.
  844. * @return array Join[]
  845. */
  846. public function getJoins()
  847. {
  848. return $this->joins;
  849. }
  850. /**
  851. * Adds a Criteria as subQuery in the From Clause.
  852. *
  853. * @param Criteria $subQueryCriteria Criteria to build the subquery from
  854. * @param string $alias alias for the subQuery
  855. *
  856. * @return Criteria this modified Criteria object (Fluid API)
  857. */
  858. public function addSelectQuery(Criteria $subQueryCriteria, $alias = null)
  859. {
  860. if (null === $alias) {
  861. $alias = 'alias_' . ($subQueryCriteria->forgeSelectQueryAlias() + count($this->selectQueries));
  862. }
  863. $this->selectQueries[$alias] = $subQueryCriteria;
  864. return $this;
  865. }
  866. /**
  867. * Checks whether this Criteria has a subquery.
  868. *
  869. * @return Boolean
  870. */
  871. public function hasSelectQueries()
  872. {
  873. return (bool) $this->selectQueries;
  874. }
  875. /**
  876. * Get the associative array of Criteria for the subQueries per alias.
  877. *
  878. * @return array Criteria[]
  879. */
  880. public function getSelectQueries()
  881. {
  882. return $this->selectQueries;
  883. }
  884. /**
  885. * Get the Criteria for a specific subQuery.
  886. *
  887. * @param string $alias alias for the subQuery
  888. * @return Criteria
  889. */
  890. public function getSelectQuery($alias)
  891. {
  892. return $this->selectQueries[$alias];
  893. }
  894. /**
  895. * checks if the Criteria for a specific subQuery is set.
  896. *
  897. * @param string $alias alias for the subQuery
  898. * @return boolean
  899. */
  900. public function hasSelectQuery($alias)
  901. {
  902. return isset($this->selectQueries[$alias]);
  903. }
  904. public function forgeSelectQueryAlias()
  905. {
  906. $aliasNumber = 0;
  907. foreach ($this->getSelectQueries() as $c1) {
  908. $aliasNumber += $c1->forgeSelectQueryAlias();
  909. }
  910. return ++$aliasNumber;
  911. }
  912. /**
  913. * Adds "ALL" modifier to the SQL statement.
  914. * @return Criteria Modified Criteria object (for fluent API)
  915. */
  916. public function setAll()
  917. {
  918. $this->removeSelectModifier(self::DISTINCT);
  919. $this->addSelectModifier(self::ALL);
  920. return $this;
  921. }
  922. /**
  923. * Adds "DISTINCT" modifier to the SQL statement.
  924. * @return Criteria Modified Criteria object (for fluent API)
  925. */
  926. public function setDistinct()
  927. {
  928. $this->removeSelectModifier(self::ALL);
  929. $this->addSelectModifier(self::DISTINCT);
  930. return $this;
  931. }
  932. /**
  933. * Adds a modifier to the SQL statement.
  934. * e.g. self::ALL, self::DISTINCT, 'SQL_CALC_FOUND_ROWS', 'HIGH_PRIORITY', etc.
  935. *
  936. * @param string $modifier The modifier to add
  937. *
  938. * @return Criteria Modified Criteria object (for fluent API)
  939. */
  940. public function addSelectModifier($modifier)
  941. {
  942. //only allow the keyword once
  943. if (!$this->hasSelectModifier($modifier)) {
  944. $this->selectModifiers[] = $modifier;
  945. }
  946. return $this;
  947. }
  948. /**
  949. * Removes a modifier to the SQL statement.
  950. * Checks for existence before removal
  951. *
  952. * @param string $modifier The modifier to add
  953. *
  954. * @return Criteria Modified Criteria object (for fluent API)
  955. */
  956. public function removeSelectModifier($modifier)
  957. {
  958. $this->selectModifiers = array_values(array_diff($this->selectModifiers, array($modifier)));
  959. return $this;
  960. }
  961. /**
  962. * Checks the existence of a SQL select modifier
  963. *
  964. * @param string $modifier The modifier to add
  965. *
  966. * @return bool
  967. */
  968. public function hasSelectModifier($modifier)
  969. {
  970. return in_array($modifier, $this->selectModifiers);
  971. }
  972. /**
  973. * Sets ignore case.
  974. *
  975. * @param boolean $b True if case should be ignored.
  976. * @return Criteria Modified Criteria object (for fluent API)
  977. */
  978. public function setIgnoreCase($b)
  979. {
  980. $this->ignoreCase = (boolean) $b;
  981. return $this;
  982. }
  983. /**
  984. * Is ignore case on or off?
  985. *
  986. * @return boolean True if case is ignored.
  987. */
  988. public function isIgnoreCase()
  989. {
  990. return $this->ignoreCase;
  991. }
  992. /**
  993. * Set single record? Set this to <code>true</code> if you expect the query
  994. * to result in only a single result record (the default behaviour is to
  995. * throw a PropelException if multiple records are returned when the query
  996. * is executed). This should be used in situations where returning multiple
  997. * rows would indicate an error of some sort. If your query might return
  998. * multiple records but you are only interested in the first one then you
  999. * should be using setLimit(1).
  1000. *
  1001. * @param boolean $b Set to TRUE if you expect the query to select just one record.
  1002. * @return Criteria Modified Criteria object (for fluent API)
  1003. */
  1004. public function setSingleRecord($b)
  1005. {
  1006. $this->singleRecord = (boolean) $b;
  1007. return $this;
  1008. }
  1009. /**
  1010. * Is single record?
  1011. *
  1012. * @return boolean True if a single record is being returned.
  1013. */
  1014. public function isSingleRecord()
  1015. {
  1016. return $this->singleRecord;
  1017. }
  1018. /**
  1019. * Set limit.
  1020. *
  1021. * @param limit An int with the value for limit.
  1022. * @return Criteria Modified Criteria object (for fluent API)
  1023. */
  1024. public function setLimit($limit)
  1025. {
  1026. // TODO: do we enforce int here? 32bit issue if we do
  1027. $this->limit = $limit;
  1028. return $this;
  1029. }
  1030. /**
  1031. * Get limit.
  1032. *
  1033. * @return int An int with the value for limit.
  1034. */
  1035. public function getLimit()
  1036. {
  1037. return $this->limit;
  1038. }
  1039. /**
  1040. * Set offset.
  1041. *
  1042. * @param int $offset An int with the value for offset. (Note this values is
  1043. * cast to a 32bit integer and may result in truncatation)
  1044. * @return Criteria Modified Criteria object (for fluent API)
  1045. */
  1046. public function setOffset($offset)
  1047. {
  1048. $this->offset = (int) $offset;
  1049. return $this;
  1050. }
  1051. /**
  1052. * Get offset.
  1053. *
  1054. * @return An int with the value for offset.
  1055. */
  1056. public function getOffset()
  1057. {
  1058. return $this->offset;
  1059. }
  1060. /**
  1061. * Add select column.
  1062. *
  1063. * @param string $name Name of the select column.
  1064. * @return Criteria Modified Criteria object (for fluent API)
  1065. */
  1066. public function addSelectColumn($name)
  1067. {
  1068. $this->selectColumns[] = $name;
  1069. return $this;
  1070. }
  1071. /**
  1072. * Set the query comment, that appears after the first verb in the SQL query
  1073. *
  1074. * @param string $comment The comment to add to the query, without comment sign
  1075. * @return Criteria Modified Criteria object (for fluent API)
  1076. */
  1077. public function setComment($comment = null)
  1078. {
  1079. $this->queryComment = $comment;
  1080. return $this;
  1081. }
  1082. /**
  1083. * Get the query comment, that appears after the first verb in the SQL query
  1084. *
  1085. * @return string The comment to add to the query, without comment sign
  1086. */
  1087. public function getComment()
  1088. {
  1089. return $this->queryComment;
  1090. }
  1091. /**
  1092. * Whether this Criteria has any select columns.
  1093. *
  1094. * This will include columns added with addAsColumn() method.
  1095. *
  1096. * @return boolean
  1097. * @see addAsColumn()
  1098. * @see addSelectColumn()
  1099. */
  1100. public function hasSelectClause()
  1101. {
  1102. return (!empty($this->selectColumns) || !empty($this->asColumns));
  1103. }
  1104. /**
  1105. * Get select columns.
  1106. *
  1107. * @return array An array with the name of the select columns.
  1108. */
  1109. public function getSelectColumns()
  1110. {
  1111. return $this->selectColumns;
  1112. }
  1113. /**
  1114. * Clears current select columns.
  1115. *
  1116. * @return Criteria Modified Criteria object (for fluent API)
  1117. */
  1118. public function clearSelectColumns()
  1119. {
  1120. $this->selectColumns = $this->asColumns = array();
  1121. return $this;
  1122. }
  1123. /**
  1124. * Get select modifiers.
  1125. *
  1126. * @return An array with the select modifiers.
  1127. */
  1128. public function getSelectModifiers()
  1129. {
  1130. return $this->selectModifiers;
  1131. }
  1132. /**
  1133. * Add group by column name.
  1134. *
  1135. * @param string $groupBy The name of the column to group by.
  1136. * @return A modified Criteria object.
  1137. */
  1138. public function addGroupByColumn($groupBy)
  1139. {
  1140. $this->groupByColumns[] = $groupBy;
  1141. return $this;
  1142. }
  1143. /**
  1144. * Add order by column name, explicitly specifying ascending.
  1145. *
  1146. * @param string $name The name of the column to order by.
  1147. * @return A modified Criteria object.
  1148. */
  1149. public function addAscendingOrderByColumn($name)
  1150. {
  1151. $this->orderByColumns[] = $name . ' ' . self::ASC;
  1152. return $this;
  1153. }
  1154. /**
  1155. * Add order by column name, explicitly specifying descending.
  1156. *
  1157. * @param string $name The name of the column to order by.
  1158. * @return Criteria Modified Criteria object (for fluent API)
  1159. */
  1160. public function addDescendingOrderByColumn($name)
  1161. {
  1162. $this->orderByColumns[] = $name . ' ' . self::DESC;
  1163. return $this;
  1164. }
  1165. /**
  1166. * Get order by columns.
  1167. *
  1168. * @return array An array with the name of the order columns.
  1169. */
  1170. public function getOrderByColumns()
  1171. {
  1172. return $this->orderByColumns;
  1173. }
  1174. /**
  1175. * Clear the order-by columns.
  1176. *
  1177. * @return Criteria Modified Criteria object (for fluent API)
  1178. */
  1179. public function clearOrderByColumns()
  1180. {
  1181. $this->orderByColumns = array();
  1182. return $this;
  1183. }
  1184. /**
  1185. * Clear the group-by columns.
  1186. *
  1187. * @return Criteria
  1188. */
  1189. public function clearGroupByColumns()
  1190. {
  1191. $this->groupByColumns = array();
  1192. return $this;
  1193. }
  1194. /**
  1195. * Get group by columns.
  1196. *
  1197. * @return array
  1198. */
  1199. public function getGroupByColumns()
  1200. {
  1201. return $this->groupByColumns;
  1202. }
  1203. /**
  1204. * Get Having Criterion.
  1205. *
  1206. * @return Criterion A Criterion object that is the having clause.
  1207. */
  1208. public function getHaving()
  1209. {
  1210. return $this->having;
  1211. }
  1212. /**
  1213. * Remove an object from the criteria.
  1214. *
  1215. * @param string $key A string with the key to be removed.
  1216. * @return mixed The removed value.
  1217. */
  1218. public function remove($key)
  1219. {
  1220. if ( isset ( $this->map[$key] ) ) {
  1221. $removed = $this->map[$key];
  1222. unset ( $this->map[$key] );
  1223. if ( $removed instanceof Criterion ) {
  1224. return $removed->getValue();
  1225. }
  1226. return $removed;
  1227. }
  1228. }
  1229. /**
  1230. * Build a string representation of the Criteria.
  1231. *
  1232. * @return string A String with the representation of the Criteria.
  1233. */
  1234. public function toString()
  1235. {
  1236. $sb = "Criteria:";
  1237. try {
  1238. $params = array();
  1239. $sb .= "\nSQL (may not be complete): "
  1240. . BasePeer::createSelectSql($this, $params);
  1241. $sb .= "\nParams: ";
  1242. $paramstr = array();
  1243. foreach ($params as $param) {
  1244. $paramstr[] = $param['table'] . '.' . $param['column'] . ' => ' . var_export($param['value'], true);
  1245. }
  1246. $sb .= implode(", ", $paramstr);
  1247. } catch (Exception $exc) {
  1248. $sb .= "(Error: " . $exc->getMessage() . ")";
  1249. }
  1250. return $sb;
  1251. }
  1252. /**
  1253. * Returns the size (count) of this criteria.
  1254. * @return int
  1255. */
  1256. public function size()
  1257. {
  1258. return count($this->map);
  1259. }
  1260. /**
  1261. * This method checks another Criteria to see if they contain
  1262. * the same attributes and hashtable entries.
  1263. * @return boolean
  1264. */
  1265. public function equals($crit)
  1266. {
  1267. if ($crit === null || !($crit instanceof Criteria)) {
  1268. return false;
  1269. } elseif ($this === $crit) {
  1270. return true;
  1271. } elseif ($this->size() === $crit->size()) {
  1272. // Important: nested criterion objects are checked
  1273. $criteria = $crit; // alias
  1274. if ($this->offset === $criteria->getOffset()
  1275. && $this->limit === $criteria->getLimit()
  1276. && $this->ignoreCase === $criteria->isIgnoreCase()
  1277. && $this->singleRecord === $criteria->isSingleRecord()
  1278. && $this->dbName === $criteria->getDbName()
  1279. && $this->selectModifiers === $criteria->getSelectModifiers()
  1280. && $this->selectColumns === $criteria->getSelectColumns()
  1281. && $this->asColumns === $criteria->getAsColumns()
  1282. && $this->orderByColumns === $criteria->getOrderByColumns()
  1283. && $this->groupByColumns === $criteria->getGroupByColumns()
  1284. && $this->aliases === $criteria->getAliases()
  1285. ) // what about having ??
  1286. {
  1287. foreach ($criteria->keys() as $key) {
  1288. if ($this->containsKey($key)) {
  1289. $a = $this->getCriterion($key);
  1290. $b = $criteria->getCriterion($key);
  1291. if (!$a->equals($b)) {
  1292. return false;
  1293. }
  1294. } else {
  1295. return false;
  1296. }
  1297. }
  1298. $joins = $criteria->getJoins();
  1299. if (count($joins) != count($this->joins)) {
  1300. return false;
  1301. }
  1302. foreach ($joins as $key => $join) {
  1303. if (!$join->equals($this->joins[$key])) {
  1304. return false;
  1305. }
  1306. }
  1307. return true;
  1308. } else {
  1309. return false;
  1310. }
  1311. }
  1312. return false;
  1313. }
  1314. /**
  1315. * Add the content of a Criteria to the current Criteria
  1316. * In case of conflict, the current Criteria keeps its properties
  1317. *
  1318. * @param Criteria $criteria The criteria to read properties from
  1319. * @param string $operator The logical operator used to combine conditions
  1320. * Defaults to Criteria::LOGICAL_AND, also accapts Criteria::LOGICAL_OR
  1321. * This parameter is deprecated, use _or() instead
  1322. *
  1323. * @return Criteria The current criteria object
  1324. */
  1325. public function mergeWith(Criteria $criteria, $operator = null)
  1326. {
  1327. // merge limit
  1328. $limit = $criteria->getLimit();
  1329. if($limit != 0 && $this->getLimit() == 0) {
  1330. $this->limit = $limit;
  1331. }
  1332. // merge offset
  1333. $offset = $criteria->getOffset();
  1334. if($offset != 0 && $this->getOffset() == 0) {
  1335. $this->offset = $offset;
  1336. }
  1337. // merge select modifiers
  1338. $selectModifiers = $criteria->getSelectModifiers();
  1339. if ($selectModifiers && ! $this->selectModifiers){
  1340. $this->selectModifiers = $selectModifiers;
  1341. }
  1342. // merge select columns
  1343. $this->selectColumns = array_merge($this->getSelectColumns(), $criteria->getSelectColumns());
  1344. // merge as columns
  1345. $commonAsColumns = array_intersect_key($this->getAsColumns(), $criteria->getAsColumns());
  1346. if (!empty($commonAsColumns)) {
  1347. throw new PropelException('The given criteria contains an AsColumn with an alias already existing in the current object');
  1348. }
  1349. $this->asColumns = array_merge($this->getAsColumns(), $criteria->getAsColumns());
  1350. // merge orderByColumns
  1351. $orderByColumns = array_merge($this->getOrderByColumns(), $criteria->getOrderByColumns());
  1352. $this->orderByColumns = array_unique($orderByColumns);
  1353. // merge groupByColumns
  1354. $groupByColumns = array_merge($this->getGroupByColumns(), $criteria->getGroupByColumns());
  1355. $this->groupByColumns = array_unique($groupByColumns);
  1356. // merge where conditions
  1357. if ($operator == Criteria::LOGICAL_OR) {
  1358. $this->_or();
  1359. }
  1360. $isFirstCondition = true;
  1361. foreach ($criteria->getMap() as $key => $criterion) {
  1362. if ($isFirstCondition && $this->defaultCombineOperator == Criteria::LOGICAL_OR) {
  1363. $this->addOr($criterion, null, null, false);
  1364. $this->defaultCombineOperator == Criteria::LOGICAL_AND;
  1365. } elseif ($this->containsKey($key)) {
  1366. $this->addAnd($criterion);
  1367. } else {
  1368. $this->add($criterion);
  1369. }
  1370. $isFirstCondition = false;
  1371. }
  1372. // merge having
  1373. if ($having = $criteria->getHaving()) {
  1374. if ($this->getHaving()) {
  1375. $this->addHaving($this->getHaving()->addAnd($having));
  1376. } else {
  1377. $this->addHaving($having);
  1378. }
  1379. }
  1380. // merge alias
  1381. $commonAliases = array_intersect_key($this->getAliases(), $criteria->getAliases());
  1382. if (!empty($commonAliases)) {
  1383. throw new PropelException('The given criteria contains an alias already existing in the current object');
  1384. }
  1385. $this->aliases = array_merge($this->getAliases(), $criteria->getAliases());
  1386. // merge join
  1387. $this->joins = array_merge($this->getJoins(), $criteria->getJoins());
  1388. return $this;
  1389. }
  1390. /**
  1391. * This method adds a prepared Criterion object to the Criteria as a having clause.
  1392. * You can get a new, empty Criterion object with the
  1393. * getNewCriterion() method.
  1394. *
  1395. * <p>
  1396. * <code>
  1397. * $crit = new Criteria();
  1398. * $c = $crit->getNewCriterion(BasePeer::ID, 5, Criteria::LESS_THAN);
  1399. * $crit->addHaving($c);
  1400. * </code>
  1401. *
  1402. * @param having A Criterion object
  1403. *
  1404. * @return A modified Criteria object.
  1405. */
  1406. public function addHaving(Criterion $having)
  1407. {
  1408. $this->having = $having;
  1409. return $this;
  1410. }
  1411. /**
  1412. * If a criterion for the requested column already exists, the condition is "AND"ed to the existing criterion (necessary for Propel 1.4 compatibility).
  1413. * If no criterion for the requested column already exists, the condition is "AND"ed to the latest criterion.
  1414. * If no criterion exist, the condition is added a new criterion
  1415. *
  1416. * Any comparison can be used.
  1417. *
  1418. * Supports a number of different signatures:
  1419. * - addAnd(column, value, comparison)
  1420. * - addAnd(column, value)
  1421. * - addAnd(Criterion)
  1422. *
  1423. * @return Criteria A modified Criteria object.
  1424. */
  1425. public function addAnd($p1, $p2 = null, $p3 = null, $preferColumnCondition = true)
  1426. {
  1427. $criterion = ($p1 instanceof Criterion) ? $p1 : new Criterion($this, $p1, $p2, $p3);
  1428. $key = $criterion->getTable() . '.' . $criterion->getColumn();
  1429. if ($preferColumnCondition && $this->containsKey($key)) {
  1430. // FIXME: addAnd() operates preferably on existing conditions on the same column
  1431. // this may cause unexpected results, but it's there for BC with Propel 14
  1432. $this->getCriterion($key)->addAnd($criterion);
  1433. } else {
  1434. // simply add the condition to the list - this is the expected behavior
  1435. $this->add($criterion);
  1436. }
  1437. return $this;
  1438. }
  1439. /**
  1440. * If a criterion for the requested column already exists, the condition is "OR"ed to the existing criterion (necessary for Propel 1.4 compatibility).
  1441. * If no criterion for the requested column already exists, the condition is "OR"ed to the latest criterion.
  1442. * If no criterion exist, the condition is added a new criterion
  1443. *
  1444. * Any comparison can be used.
  1445. *
  1446. * Supports a number of different signatures:
  1447. * - addOr(column, value, comparison)
  1448. * - addOr(column, value)
  1449. * - addOr(Criterion)
  1450. *
  1451. * @return Criteria A modified Criteria object.
  1452. */
  1453. public function addOr($p1, $p2 = null, $p3 = null, $preferColumnCondition = true)
  1454. {
  1455. $rightCriterion = ($p1 instanceof Criterion) ? $p1 : new Criterion($this, $p1, $p2, $p3);
  1456. $key = $rightCriterion->getTable() . '.' . $rightCriterion->getColumn();
  1457. if ($preferColumnCondition && $this->containsKey($key)) {
  1458. // FIXME: addOr() operates preferably on existing conditions on the same column
  1459. // this may cause unexpected results, but it's there for BC with Propel 14
  1460. $leftCriterion = $this->getCriterion($key);
  1461. } else {
  1462. // fallback to the latest condition - this is the expected behavior
  1463. $leftCriterion = $this->getLastCriterion();
  1464. }
  1465. if ($leftCriterion !== null) {
  1466. // combine the given criterion with the existing one with an 'OR'
  1467. $leftCriterion->addOr($rightCriterion);
  1468. } else {
  1469. // nothing to do OR / AND with, so make it first condition
  1470. $this->add($rightCriterion);
  1471. }
  1472. return $this;
  1473. }
  1474. /**
  1475. * Overrides Criteria::add() to use the default combine operator
  1476. * @see Criteria::add()
  1477. *
  1478. * @param string|Criterion $p1 The column to run the comparison on (e.g. BookPeer::ID), or Criterion object
  1479. * @param mixed $value
  1480. * @param string $operator A String, like Criteria::EQUAL.
  1481. * @param boolean $preferColumnCondition If true, the condition is combined with an existing condition on the same column
  1482. * (necessary for Propel 1.4 compatibility).
  1483. * If false, the condition is combined with the last existing condition.
  1484. *
  1485. * @return Criteria A modified Criteria object.
  1486. */
  1487. public function addUsingOperator($p1, $value = null, $operator = null, $preferColumnCondition = true)
  1488. {
  1489. if ($this->defaultCombineOperator == Criteria::LOGICAL_OR) {
  1490. $this->defaultCombineOperator = Criteria::LOGICAL_AND;
  1491. return $this->addOr($p1, $value, $operator, $preferColumnCondition);
  1492. } else {
  1493. return $this->addAnd($p1, $value, $operator, $preferColumnCondition);
  1494. }
  1495. }
  1496. // Fluid operators
  1497. public function _or()
  1498. {
  1499. $this->defaultCombineOperator = Criteria::LOGICAL_OR;
  1500. return $this;
  1501. }
  1502. public function _and()
  1503. {
  1504. $this->defaultCombineOperator = Criteria::LOGICAL_AND;
  1505. return $this;
  1506. }
  1507. // Fluid Conditions
  1508. /**
  1509. * Returns the current object if the condition is true,
  1510. * or a PropelConditionalProxy instance otherwise.
  1511. * Allows for conditional statements in a fluid interface.
  1512. *
  1513. * @param bool $cond
  1514. *
  1515. * @return PropelConditionalProxy|Criteria
  1516. */
  1517. public function _if($cond)
  1518. {
  1519. $this->conditionalProxy = new PropelConditionalProxy($this, $cond, $this->conditionalProxy);
  1520. return $this->conditionalProxy->getCriteriaOrProxy();
  1521. }
  1522. /**
  1523. * Returns a PropelConditionalProxy instance.
  1524. * Allows for conditional statements in a fluid interface.
  1525. *
  1526. * @param bool $cond ignored
  1527. *
  1528. * @return PropelConditionalProxy|Criteria
  1529. */
  1530. public function _elseif($cond)
  1531. {
  1532. if (!$this->conditionalProxy) {
  1533. throw new PropelException('_elseif() must be called after _if()');
  1534. }
  1535. return $this->conditionalProxy->_elseif($cond);
  1536. }
  1537. /**
  1538. * Returns a PropelConditionalProxy instance.
  1539. * Allows for conditional statements in a fluid interface.
  1540. *
  1541. * @return PropelConditionalProxy|Criteria
  1542. */
  1543. public function _else()
  1544. {
  1545. if (!$this->conditionalProxy) {
  1546. throw new PropelException('_else() must be called after _if()');
  1547. }
  1548. return $this->conditionalProxy->_else();
  1549. }
  1550. /**
  1551. * Returns the current object
  1552. * Allows for conditional statements in a fluid interface.
  1553. *
  1554. * @return Criteria
  1555. */
  1556. public function _endif()
  1557. {
  1558. if (!$this->conditionalProxy) {
  1559. throw new PropelException('_endif() must be called after _if()');
  1560. }
  1561. $this->conditionalProxy = $this->conditionalProxy->getParentProxy();
  1562. if ($this->conditionalProxy) {
  1563. return $this->conditionalProxy->getCriteriaOrProxy();
  1564. }
  1565. // reached last level
  1566. return $this;
  1567. }
  1568. /**
  1569. * Ensures deep cloning of attached objects
  1570. */
  1571. public function __clone()
  1572. {
  1573. foreach ($this->map as $key => $criterion) {
  1574. $this->map[$key] = clone $criterion;
  1575. }
  1576. foreach ($this->joins as $key => $join) {
  1577. $this->joins[$key] = clone $join;
  1578. }
  1579. if (null !== $this->having) {
  1580. $this->having = clone $this->having;
  1581. }
  1582. }
  1583. }