basic.arrays.embedded-documents.txt 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. Title: Array of Embedded Documents
  2. Author: Dariusz Górecki <darek.krk@gmail.com>
  3. ---
  4. *There is no way (that I know) where I can easily provide mechanism for this, you have to write Your own*
  5. This is how I accomplish it for now.
  6. Within suite package you should have and `extra` folder that contains (not only)
  7. `EEmbeddedArraysBehavior` class, we can use it to store an arrays of embedded documents.
  8. Example:
  9. ~~~
  10. [php]
  11. class User extends EMongoDocument
  12. {
  13. // ...
  14. // Add property for storing array
  15. public $addresses;
  16. // Add EEmbeddedArraysBehavior
  17. public function behaviors()
  18. {
  19. return array(
  20. 'embeddedArrays' => array(
  21. 'class'=>'ext.YiiMongoDbSuite.extra.EEmbeddedArraysBehavior',
  22. 'arrayPropertyName'=>'addresses', // name of property, that will be used as an array
  23. 'arrayDocClassName'=>'ClientAddress' // class name of embedded documents in array
  24. ),
  25. );
  26. }
  27. // ...
  28. }
  29. ~~~
  30. Now you can use property arrays as an array of embedded documents:
  31. ~~~
  32. [php]
  33. $user = new User();
  34. $user->addresses[0] = new ClientAddress();
  35. $user->addresses[0]->city = 'New York';
  36. $user->save(); // Behavior will automatically handle of validation, saving etc.
  37. // OR:
  38. $user = User::model()->find();
  39. foreach($user->addresses as $userAddress)
  40. {
  41. echo $userAddress->city;
  42. }
  43. ~~~