advanced.indexing.txt 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. Title: Indexing
  2. Author: Dariusz Górecki <darek.krk@gmail.com>
  3. ---
  4. Now you can define indexes for yours collections in easy way!
  5. Suite will check for existing of indexes only once, at the first class use, (per script-calls)
  6. if it not find any of declared indexes it will create them
  7. Only thing you need is to define the `indexes()` method in yours model class, see the example:
  8. ~~~
  9. [php]
  10. class Client extends EMongoDocument
  11. {
  12. // ....
  13. public function indexes()
  14. {
  15. return array(
  16. // index name is not important, you may write whatever you want, just must be unique
  17. 'index1_name'=>array(
  18. // key array holds list of fields for index
  19. // you may define multiple keys for index and multikey indexes
  20. // each key must have a sorting direction SORT_ASC or SORT_DESC
  21. 'key'=>array(
  22. 'field_name'=>EMongoCriteria::SORT_ASC
  23. 'field_name.embeded_field'=>EMongoCriteria::SORT_DESC
  24. ),
  25. // unique, if indexed field must be unique, define a unique key
  26. 'unique'=>true,
  27. ),
  28. );
  29. }
  30. // ....
  31. }
  32. ~~~
  33. If you whant to disable index existing checking on every page load,
  34. because of perfomance reasons (recomended for production)
  35. put `$this->ensureIndexes = false;` into yours `init()` method in model class.
  36. ~~~
  37. [php]
  38. class Client extends EMongoDocument
  39. {
  40. // ....
  41. public function init()
  42. {
  43. $this->ensureIndexes = false;
  44. }
  45. // ....
  46. }
  47. ~~~