CDbCriteria.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. <?php
  2. /**
  3. * CDbCriteria class file.
  4. *
  5. * @author Qiang Xue <qiang.xue@gmail.com>
  6. * @link http://www.yiiframework.com/
  7. * @copyright 2008-2013 Yii Software LLC
  8. * @license http://www.yiiframework.com/license/
  9. */
  10. /**
  11. * CDbCriteria represents a query criteria, such as conditions, ordering by, limit/offset.
  12. *
  13. * It can be used in AR query methods such as CActiveRecord::find and CActiveRecord::findAll.
  14. *
  15. * $criteria=new CDbCriteria();
  16. * $criteria->compare('status',Post::STATUS_ACTIVE);
  17. * $criteria->addInCondition('id',array(1,2,3,4,5,6));
  18. *
  19. * $posts = Post::model()->findAll($criteria);
  20. *
  21. * @author Qiang Xue <qiang.xue@gmail.com>
  22. * @package system.db.schema
  23. * @since 1.0
  24. */
  25. class CDbCriteria extends CComponent
  26. {
  27. const PARAM_PREFIX=':ycp';
  28. /**
  29. * @var integer the global counter for anonymous binding parameters.
  30. * This counter is used for generating the name for the anonymous parameters.
  31. */
  32. public static $paramCount=0;
  33. /**
  34. * @var mixed the columns being selected. This refers to the SELECT clause in an SQL
  35. * statement. The property can be either a string (column names separated by commas)
  36. * or an array of column names. Defaults to '*', meaning all columns.
  37. */
  38. public $select='*';
  39. /**
  40. * @var boolean whether to select distinct rows of data only. If this is set true,
  41. * the SELECT clause would be changed to SELECT DISTINCT.
  42. */
  43. public $distinct=false;
  44. /**
  45. * @var string query condition. This refers to the WHERE clause in an SQL statement.
  46. * For example, <code>age>31 AND team=1</code>.
  47. */
  48. public $condition='';
  49. /**
  50. * @var array list of query parameter values indexed by parameter placeholders.
  51. * For example, <code>array(':name'=>'Dan', ':age'=>31)</code>.
  52. */
  53. public $params=array();
  54. /**
  55. * @var integer maximum number of records to be returned. If less than 0, it means no limit.
  56. */
  57. public $limit=-1;
  58. /**
  59. * @var integer zero-based offset from where the records are to be returned. If less than 0, it means starting from the beginning.
  60. */
  61. public $offset=-1;
  62. /**
  63. * @var string how to sort the query results. This refers to the ORDER BY clause in an SQL statement.
  64. */
  65. public $order='';
  66. /**
  67. * @var string how to group the query results. This refers to the GROUP BY clause in an SQL statement.
  68. * For example, <code>'projectID, teamID'</code>.
  69. */
  70. public $group='';
  71. /**
  72. * @var string how to join with other tables. This refers to the JOIN clause in an SQL statement.
  73. * For example, <code>'LEFT JOIN users ON users.id=authorID'</code>.
  74. */
  75. public $join='';
  76. /**
  77. * @var string the condition to be applied with GROUP-BY clause.
  78. * For example, <code>'SUM(revenue)<50000'</code>.
  79. */
  80. public $having='';
  81. /**
  82. * @var mixed the relational query criteria. This is used for fetching related objects in eager loading fashion.
  83. * This property is effective only when the criteria is passed as a parameter to the following methods of CActiveRecord:
  84. * <ul>
  85. * <li>{@link CActiveRecord::find()}</li>
  86. * <li>{@link CActiveRecord::findAll()}</li>
  87. * <li>{@link CActiveRecord::findByPk()}</li>
  88. * <li>{@link CActiveRecord::findAllByPk()}</li>
  89. * <li>{@link CActiveRecord::findByAttributes()}</li>
  90. * <li>{@link CActiveRecord::findAllByAttributes()}</li>
  91. * <li>{@link CActiveRecord::count()}</li>
  92. * </ul>
  93. * The property value will be used as the parameter to the {@link CActiveRecord::with()} method
  94. * to perform the eager loading. Please refer to {@link CActiveRecord::with()} on how to specify this parameter.
  95. * @since 1.1.0
  96. */
  97. public $with;
  98. /**
  99. * @var string the alias name of the table. If not set, it means the alias is 't'.
  100. */
  101. public $alias;
  102. /**
  103. * @var boolean whether the foreign tables should be joined with the primary table in a single SQL.
  104. * This property is only used in relational AR queries for HAS_MANY and MANY_MANY relations.
  105. *
  106. * When this property is set true, only a single SQL will be executed for a relational AR query,
  107. * even if the primary table is limited and the relationship between a foreign table and the primary
  108. * table is many-to-one.
  109. *
  110. * When this property is set false, a SQL statement will be executed for each HAS_MANY relation.
  111. *
  112. * When this property is not set, if the primary table is limited or paginated,
  113. * a SQL statement will be executed for each HAS_MANY relation.
  114. * Otherwise, a single SQL statement will be executed for all.
  115. *
  116. * @since 1.1.4
  117. */
  118. public $together;
  119. /**
  120. * @var string the name of the AR attribute whose value should be used as index of the query result array.
  121. * Defaults to null, meaning the result array will be zero-based integers.
  122. * @since 1.1.5
  123. */
  124. public $index;
  125. /**
  126. * @var mixed scopes to apply
  127. *
  128. * This property is effective only when passing criteria to
  129. * the one of the following methods:
  130. * <ul>
  131. * <li>{@link CActiveRecord::find()}</li>
  132. * <li>{@link CActiveRecord::findAll()}</li>
  133. * <li>{@link CActiveRecord::findByPk()}</li>
  134. * <li>{@link CActiveRecord::findAllByPk()}</li>
  135. * <li>{@link CActiveRecord::findByAttributes()}</li>
  136. * <li>{@link CActiveRecord::findAllByAttributes()}</li>
  137. * <li>{@link CActiveRecord::count()}</li>
  138. * </ul>
  139. *
  140. * Can be set to one of the following:
  141. * <ul>
  142. * <li>One scope: $criteria->scopes='scopeName';</li>
  143. * <li>Multiple scopes: $criteria->scopes=array('scopeName1','scopeName2');</li>
  144. * <li>Scope with parameters: $criteria->scopes=array('scopeName'=>array($params));</li>
  145. * <li>Multiple scopes with parameters: $criteria->scopes=array('scopeName1'=>array($params1),'scopeName2'=>array($params2));</li>
  146. * <li>Multiple scopes with the same name: array(array('scopeName'=>array($params1)),array('scopeName'=>array($params2)));</li>
  147. * </ul>
  148. * @since 1.1.7
  149. */
  150. public $scopes;
  151. /**
  152. * Constructor.
  153. * @param array $data criteria initial property values (indexed by property name)
  154. */
  155. public function __construct($data=array())
  156. {
  157. foreach($data as $name=>$value)
  158. $this->$name=$value;
  159. }
  160. /**
  161. * Remaps criteria parameters on unserialize to prevent name collisions.
  162. * @since 1.1.9
  163. */
  164. public function __wakeup()
  165. {
  166. $map=array();
  167. $params=array();
  168. foreach($this->params as $name=>$value)
  169. {
  170. if(strpos($name,self::PARAM_PREFIX)===0)
  171. {
  172. $newName=self::PARAM_PREFIX.self::$paramCount++;
  173. $map[$name]=$newName;
  174. }
  175. else
  176. {
  177. $newName=$name;
  178. }
  179. $params[$newName]=$value;
  180. }
  181. if (!empty($map))
  182. {
  183. $sqlContentFieldNames=array(
  184. 'select',
  185. 'condition',
  186. 'order',
  187. 'group',
  188. 'join',
  189. 'having',
  190. );
  191. foreach($sqlContentFieldNames as $field)
  192. {
  193. if(is_array($this->$field))
  194. foreach($this->$field as $k=>$v)
  195. $this->{$field}[$k]=strtr($v,$map);
  196. else
  197. $this->$field=strtr($this->$field,$map);
  198. }
  199. }
  200. $this->params=$params;
  201. }
  202. /**
  203. * Appends a condition to the existing {@link condition}.
  204. * The new condition and the existing condition will be concatenated via the specified operator
  205. * which defaults to 'AND'.
  206. * The new condition can also be an array. In this case, all elements in the array
  207. * will be concatenated together via the operator.
  208. * This method handles the case when the existing condition is empty.
  209. * After calling this method, the {@link condition} property will be modified.
  210. * @param mixed $condition the new condition. It can be either a string or an array of strings.
  211. * @param string $operator the operator to join different conditions. Defaults to 'AND'.
  212. * @return static the criteria object itself
  213. */
  214. public function addCondition($condition,$operator='AND')
  215. {
  216. if(is_array($condition))
  217. {
  218. if($condition===array())
  219. return $this;
  220. $condition='('.implode(') '.$operator.' (',$condition).')';
  221. }
  222. if($this->condition==='')
  223. $this->condition=$condition;
  224. else
  225. $this->condition='('.$this->condition.') '.$operator.' ('.$condition.')';
  226. return $this;
  227. }
  228. /**
  229. * Appends a search condition to the existing {@link condition}.
  230. * The search condition and the existing condition will be concatenated via the specified operator
  231. * which defaults to 'AND'.
  232. * The search condition is generated using the SQL LIKE operator with the given column name and
  233. * search keyword.
  234. * @param string $column the column name (or a valid SQL expression)
  235. * @param string $keyword the search keyword. This interpretation of the keyword is affected by the next parameter.
  236. * @param boolean $escape whether the keyword should be escaped if it contains characters % or _.
  237. * When this parameter is true (default), the special characters % (matches 0 or more characters)
  238. * and _ (matches a single character) will be escaped, and the keyword will be surrounded with a %
  239. * character on both ends. When this parameter is false, the keyword will be directly used for
  240. * matching without any change.
  241. * @param string $operator the operator used to concatenate the new condition with the existing one.
  242. * Defaults to 'AND'.
  243. * @param string $like the LIKE operator. Defaults to 'LIKE'. You may also set this to be 'NOT LIKE'.
  244. * @return static the criteria object itself
  245. */
  246. public function addSearchCondition($column,$keyword,$escape=true,$operator='AND',$like='LIKE')
  247. {
  248. if($keyword=='')
  249. return $this;
  250. if($escape)
  251. $keyword='%'.strtr($keyword,array('%'=>'\%', '_'=>'\_', '\\'=>'\\\\')).'%';
  252. $condition=$column." $like ".self::PARAM_PREFIX.self::$paramCount;
  253. $this->params[self::PARAM_PREFIX.self::$paramCount++]=$keyword;
  254. return $this->addCondition($condition, $operator);
  255. }
  256. /**
  257. * Appends an IN condition to the existing {@link condition}.
  258. * The IN condition and the existing condition will be concatenated via the specified operator
  259. * which defaults to 'AND'.
  260. * The IN condition is generated by using the SQL IN operator which requires the specified
  261. * column value to be among the given list of values.
  262. * @param string $column the column name (or a valid SQL expression)
  263. * @param array $values list of values that the column value should be in
  264. * @param string $operator the operator used to concatenate the new condition with the existing one.
  265. * Defaults to 'AND'.
  266. * @return static the criteria object itself
  267. */
  268. public function addInCondition($column,$values,$operator='AND')
  269. {
  270. if(($n=count($values))<1)
  271. $condition='0=1'; // 0=1 is used because in MSSQL value alone can't be used in WHERE
  272. elseif($n===1)
  273. {
  274. $value=reset($values);
  275. if($value===null)
  276. $condition=$column.' IS NULL';
  277. else
  278. {
  279. $condition=$column.'='.self::PARAM_PREFIX.self::$paramCount;
  280. $this->params[self::PARAM_PREFIX.self::$paramCount++]=$value;
  281. }
  282. }
  283. else
  284. {
  285. $params=array();
  286. foreach($values as $value)
  287. {
  288. $params[]=self::PARAM_PREFIX.self::$paramCount;
  289. $this->params[self::PARAM_PREFIX.self::$paramCount++]=$value;
  290. }
  291. $condition=$column.' IN ('.implode(', ',$params).')';
  292. }
  293. return $this->addCondition($condition,$operator);
  294. }
  295. /**
  296. * Appends an NOT IN condition to the existing {@link condition}.
  297. * The NOT IN condition and the existing condition will be concatenated via the specified operator
  298. * which defaults to 'AND'.
  299. * The NOT IN condition is generated by using the SQL NOT IN operator which requires the specified
  300. * column value to be among the given list of values.
  301. * @param string $column the column name (or a valid SQL expression)
  302. * @param array $values list of values that the column value should not be in
  303. * @param string $operator the operator used to concatenate the new condition with the existing one.
  304. * Defaults to 'AND'.
  305. * @return static the criteria object itself
  306. * @since 1.1.1
  307. */
  308. public function addNotInCondition($column,$values,$operator='AND')
  309. {
  310. if(($n=count($values))<1)
  311. return $this;
  312. if($n===1)
  313. {
  314. $value=reset($values);
  315. if($value===null)
  316. $condition=$column.' IS NOT NULL';
  317. else
  318. {
  319. $condition=$column.'!='.self::PARAM_PREFIX.self::$paramCount;
  320. $this->params[self::PARAM_PREFIX.self::$paramCount++]=$value;
  321. }
  322. }
  323. else
  324. {
  325. $params=array();
  326. foreach($values as $value)
  327. {
  328. $params[]=self::PARAM_PREFIX.self::$paramCount;
  329. $this->params[self::PARAM_PREFIX.self::$paramCount++]=$value;
  330. }
  331. $condition=$column.' NOT IN ('.implode(', ',$params).')';
  332. }
  333. return $this->addCondition($condition,$operator);
  334. }
  335. /**
  336. * Appends a condition for matching the given list of column values.
  337. * The generated condition will be concatenated to the existing {@link condition}
  338. * via the specified operator which defaults to 'AND'.
  339. * The condition is generated by matching each column and the corresponding value.
  340. * @param array $columns list of column names and values to be matched (name=>value)
  341. * @param string $columnOperator the operator to concatenate multiple column matching condition. Defaults to 'AND'.
  342. * @param string $operator the operator used to concatenate the new condition with the existing one.
  343. * Defaults to 'AND'.
  344. * @return static the criteria object itself
  345. */
  346. public function addColumnCondition($columns,$columnOperator='AND',$operator='AND')
  347. {
  348. $params=array();
  349. foreach($columns as $name=>$value)
  350. {
  351. if($value===null)
  352. $params[]=$name.' IS NULL';
  353. else
  354. {
  355. $params[]=$name.'='.self::PARAM_PREFIX.self::$paramCount;
  356. $this->params[self::PARAM_PREFIX.self::$paramCount++]=$value;
  357. }
  358. }
  359. return $this->addCondition(implode(" $columnOperator ",$params), $operator);
  360. }
  361. /**
  362. * Adds a comparison expression to the {@link condition} property.
  363. *
  364. * This method is a helper that appends to the {@link condition} property
  365. * with a new comparison expression. The comparison is done by comparing a column
  366. * with the given value using some comparison operator.
  367. *
  368. * The comparison operator is intelligently determined based on the first few
  369. * characters in the given value. In particular, it recognizes the following operators
  370. * if they appear as the leading characters in the given value:
  371. * <ul>
  372. * <li><code>&lt;</code>: the column must be less than the given value.</li>
  373. * <li><code>&gt;</code>: the column must be greater than the given value.</li>
  374. * <li><code>&lt;=</code>: the column must be less than or equal to the given value.</li>
  375. * <li><code>&gt;=</code>: the column must be greater than or equal to the given value.</li>
  376. * <li><code>&lt;&gt;</code>: the column must not be the same as the given value.
  377. * Note that when $partialMatch is true, this would mean the value must not be a substring
  378. * of the column.</li>
  379. * <li><code>=</code>: the column must be equal to the given value.</li>
  380. * <li>none of the above: the column must be equal to the given value. Note that when $partialMatch
  381. * is true, this would mean the value must be the same as the given value or be a substring of it.</li>
  382. * </ul>
  383. *
  384. * Note that any surrounding white spaces will be removed from the value before comparison.
  385. * When the value is empty, no comparison expression will be added to the search condition.
  386. *
  387. * @param string $column the name of the column to be searched
  388. * @param mixed $value the column value to be compared with. If the value is a string, the aforementioned
  389. * intelligent comparison will be conducted. If the value is an array, the comparison is done
  390. * by exact match of any of the value in the array. If the string or the array is empty,
  391. * the existing search condition will not be modified.
  392. * @param boolean $partialMatch whether the value should consider partial text match (using LIKE and NOT LIKE operators).
  393. * Defaults to false, meaning exact comparison.
  394. * @param string $operator the operator used to concatenate the new condition with the existing one.
  395. * Defaults to 'AND'.
  396. * @param boolean $escape whether the value should be escaped if $partialMatch is true and
  397. * the value contains characters % or _. When this parameter is true (default),
  398. * the special characters % (matches 0 or more characters)
  399. * and _ (matches a single character) will be escaped, and the value will be surrounded with a %
  400. * character on both ends. When this parameter is false, the value will be directly used for
  401. * matching without any change.
  402. * @return static the criteria object itself
  403. * @since 1.1.1
  404. */
  405. public function compare($column, $value, $partialMatch=false, $operator='AND', $escape=true)
  406. {
  407. if(is_array($value))
  408. {
  409. if($value===array())
  410. return $this;
  411. return $this->addInCondition($column,$value,$operator);
  412. }
  413. else
  414. $value="$value";
  415. if(preg_match('/^(?:\s*(<>|<=|>=|<|>|=))?(.*)$/',$value,$matches))
  416. {
  417. $value=$matches[2];
  418. $op=$matches[1];
  419. }
  420. else
  421. $op='';
  422. if($value==='')
  423. return $this;
  424. if($partialMatch)
  425. {
  426. if($op==='')
  427. return $this->addSearchCondition($column,$value,$escape,$operator);
  428. if($op==='<>')
  429. return $this->addSearchCondition($column,$value,$escape,$operator,'NOT LIKE');
  430. }
  431. elseif($op==='')
  432. $op='=';
  433. $this->addCondition($column.$op.self::PARAM_PREFIX.self::$paramCount,$operator);
  434. $this->params[self::PARAM_PREFIX.self::$paramCount++]=$value;
  435. return $this;
  436. }
  437. /**
  438. * Adds a between condition to the {@link condition} property.
  439. *
  440. * The new between condition and the existing condition will be concatenated via
  441. * the specified operator which defaults to 'AND'.
  442. * If one or both values are empty then the condition is not added to the existing condition.
  443. * This method handles the case when the existing condition is empty.
  444. * After calling this method, the {@link condition} property will be modified.
  445. * @param string $column the name of the column to search between.
  446. * @param string $valueStart the beginning value to start the between search.
  447. * @param string $valueEnd the ending value to end the between search.
  448. * @param string $operator the operator used to concatenate the new condition with the existing one.
  449. * Defaults to 'AND'.
  450. * @return static the criteria object itself
  451. * @since 1.1.2
  452. */
  453. public function addBetweenCondition($column,$valueStart,$valueEnd,$operator='AND')
  454. {
  455. if($valueStart==='' || $valueEnd==='')
  456. return $this;
  457. $paramStart=self::PARAM_PREFIX.self::$paramCount++;
  458. $paramEnd=self::PARAM_PREFIX.self::$paramCount++;
  459. $this->params[$paramStart]=$valueStart;
  460. $this->params[$paramEnd]=$valueEnd;
  461. $condition="$column BETWEEN $paramStart AND $paramEnd";
  462. return $this->addCondition($condition,$operator);
  463. }
  464. /**
  465. * Merges with another criteria.
  466. * In general, the merging makes the resulting criteria more restrictive.
  467. * For example, if both criterias have conditions, they will be 'AND' together.
  468. * Also, the criteria passed as the parameter takes precedence in case
  469. * two options cannot be merged (e.g. LIMIT, OFFSET).
  470. * @param mixed $criteria the criteria to be merged with. Either an array or CDbCriteria.
  471. * @param string|boolean $operator the operator used to concatenate where and having conditions. Defaults to 'AND'.
  472. * For backwards compatibility a boolean value can be passed:
  473. * - 'false' for 'OR'
  474. * - 'true' for 'AND'
  475. */
  476. public function mergeWith($criteria,$operator='AND')
  477. {
  478. if(is_bool($operator))
  479. $operator=$operator ? 'AND' : 'OR';
  480. if(is_array($criteria))
  481. $criteria=new self($criteria);
  482. if($this->select!==$criteria->select)
  483. {
  484. if($this->select==='*')
  485. $this->select=$criteria->select;
  486. elseif($criteria->select!=='*')
  487. {
  488. $select1=is_string($this->select)?preg_split('/\s*,\s*/',trim($this->select),-1,PREG_SPLIT_NO_EMPTY):$this->select;
  489. $select2=is_string($criteria->select)?preg_split('/\s*,\s*/',trim($criteria->select),-1,PREG_SPLIT_NO_EMPTY):$criteria->select;
  490. $this->select=array_merge($select1,array_diff($select2,$select1));
  491. }
  492. }
  493. if($this->condition!==$criteria->condition)
  494. {
  495. if($this->condition==='')
  496. $this->condition=$criteria->condition;
  497. elseif($criteria->condition!=='')
  498. $this->condition="({$this->condition}) $operator ({$criteria->condition})";
  499. }
  500. if($this->params!==$criteria->params)
  501. $this->params=array_merge($this->params,$criteria->params);
  502. if($criteria->limit>=0)
  503. $this->limit=$criteria->limit;
  504. if($criteria->offset>=0)
  505. $this->offset=$criteria->offset;
  506. if($criteria->alias!==null)
  507. $this->alias=$criteria->alias;
  508. if($this->order!==$criteria->order)
  509. {
  510. if($this->order==='')
  511. $this->order=$criteria->order;
  512. elseif($criteria->order!=='')
  513. $this->order=$criteria->order.', '.$this->order;
  514. }
  515. if($this->group!==$criteria->group)
  516. {
  517. if($this->group==='')
  518. $this->group=$criteria->group;
  519. elseif($criteria->group!=='')
  520. $this->group.=', '.$criteria->group;
  521. }
  522. if($this->join!==$criteria->join)
  523. {
  524. if($this->join==='')
  525. $this->join=$criteria->join;
  526. elseif($criteria->join!=='')
  527. $this->join.=' '.$criteria->join;
  528. }
  529. if($this->having!==$criteria->having)
  530. {
  531. if($this->having==='')
  532. $this->having=$criteria->having;
  533. elseif($criteria->having!=='')
  534. $this->having="({$this->having}) $operator ({$criteria->having})";
  535. }
  536. if($criteria->distinct>0)
  537. $this->distinct=$criteria->distinct;
  538. if($criteria->together!==null)
  539. $this->together=$criteria->together;
  540. if($criteria->index!==null)
  541. $this->index=$criteria->index;
  542. if(empty($this->scopes))
  543. $this->scopes=$criteria->scopes;
  544. elseif(!empty($criteria->scopes))
  545. {
  546. $scopes1=(array)$this->scopes;
  547. $scopes2=(array)$criteria->scopes;
  548. foreach($scopes1 as $k=>$v)
  549. {
  550. if(is_integer($k))
  551. $scopes[]=$v;
  552. elseif(isset($scopes2[$k]))
  553. $scopes[]=array($k=>$v);
  554. else
  555. $scopes[$k]=$v;
  556. }
  557. foreach($scopes2 as $k=>$v)
  558. {
  559. if(is_integer($k))
  560. $scopes[]=$v;
  561. elseif(isset($scopes1[$k]))
  562. $scopes[]=array($k=>$v);
  563. else
  564. $scopes[$k]=$v;
  565. }
  566. $this->scopes=$scopes;
  567. }
  568. if(empty($this->with))
  569. $this->with=$criteria->with;
  570. elseif(!empty($criteria->with))
  571. {
  572. $this->with=(array)$this->with;
  573. foreach((array)$criteria->with as $k=>$v)
  574. {
  575. if(is_integer($k))
  576. $this->with[]=$v;
  577. elseif(isset($this->with[$k]))
  578. {
  579. $excludes=array();
  580. foreach(array('joinType','on') as $opt)
  581. {
  582. if(isset($this->with[$k][$opt]))
  583. $excludes[$opt]=$this->with[$k][$opt];
  584. if(isset($v[$opt]))
  585. $excludes[$opt]= ($opt==='on' && isset($excludes[$opt]) && $v[$opt]!==$excludes[$opt]) ?
  586. "($excludes[$opt]) AND $v[$opt]" : $v[$opt];
  587. unset($this->with[$k][$opt]);
  588. unset($v[$opt]);
  589. }
  590. $this->with[$k]=new self($this->with[$k]);
  591. $this->with[$k]->mergeWith($v,$operator);
  592. $this->with[$k]=$this->with[$k]->toArray();
  593. if (count($excludes)!==0)
  594. $this->with[$k]=CMap::mergeArray($this->with[$k],$excludes);
  595. }
  596. else
  597. $this->with[$k]=$v;
  598. }
  599. }
  600. }
  601. /**
  602. * @return array the array representation of the criteria
  603. */
  604. public function toArray()
  605. {
  606. $result=array();
  607. foreach(array('select', 'condition', 'params', 'limit', 'offset', 'order', 'group', 'join', 'having', 'distinct', 'scopes', 'with', 'alias', 'index', 'together') as $name)
  608. $result[$name]=$this->$name;
  609. return $result;
  610. }
  611. }