Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 833 lines (749 sloc) 26.029 kb
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
1 <?php
18038f3 @nateabele Adding documentation to `model\Document` and `source\MongoDb` classes…
nateabele authored
2 /**
3 * Lithium: the most rad php framework
4 *
14de7bf @gwoo Happy 2012!
gwoo authored
5 * @copyright Copyright 2012, Union of RAD (http://union-of-rad.org)
18038f3 @nateabele Adding documentation to `model\Document` and `source\MongoDb` classes…
nateabele authored
6 * @license http://opensource.org/licenses/bsd-license.php The BSD License
7 */
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
8
9 namespace lithium\data\source;
10
00ce46c @nateabele Implemented core exceptions. Implemented `\core\Libraries::instance()…
nateabele authored
11 use Mongo;
12 use MongoId;
13 use MongoCode;
20d3736 @nateabele Refactoring collection and entity classes to use `cast()` method of t…
nateabele authored
14 use MongoDate;
00ce46c @nateabele Implemented core exceptions. Implemented `\core\Libraries::instance()…
nateabele authored
15 use MongoRegex;
cfd2867 @nateabele Adding support for `'code'` and `'binary'` types to MongoDB adapter.
nateabele authored
16 use MongoBinData;
00ce46c @nateabele Implemented core exceptions. Implemented `\core\Libraries::instance()…
nateabele authored
17 use lithium\util\Inflector;
18 use lithium\core\NetworkException;
19 use Exception;
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
20
18038f3 @nateabele Adding documentation to `model\Document` and `source\MongoDb` classes…
nateabele authored
21 /**
22 * A data source adapter which allows you to connect to the MongoDB database engine. MongoDB is an
23 * Open Source distributed document database which bridges the gap between key/value stores and
24 * relational databases. To learn more about MongoDB, see here:
25 * [http://www.mongodb.org/](http://www.mongodb.org/).
26 *
27 * Rather than operating on records and record sets, queries against MongoDB will return nested sets
28 * of `Document` objects. A `Document`'s fields can contain both simple and complex data types
29 * (i.e. arrays) including other `Document` objects.
30 *
31 * After installing MongoDB, you can connect to it as follows:
2dddf3e @nateabele Removing completed `@todo`s, and reformatting paths to strip redundan…
nateabele authored
32 * {{{
33 * // config/bootstrap/connections.php:
34 * Connections::add('default', array('type' => 'MongoDb', 'database' => 'myDb'));
35 * }}}
18038f3 @nateabele Adding documentation to `model\Document` and `source\MongoDb` classes…
nateabele authored
36 *
37 * By default, it will attempt to connect to a Mongo instance running on `localhost` on port
25d3314 @nateabele Updating docblock in MongoDB adapter to reflect API changes.
nateabele authored
38 * 27017. See `__construct()` for details on the accepted configuration settings.
18038f3 @nateabele Adding documentation to `model\Document` and `source\MongoDb` classes…
nateabele authored
39 *
244646c @nateabele Adding test for `\data\Source`. Refactoring classes in `\data` into "…
nateabele authored
40 * @see lithium\data\entity\Document
18038f3 @nateabele Adding documentation to `model\Document` and `source\MongoDb` classes…
nateabele authored
41 * @see lithium\data\Connections::add()
42 * @see lithium\data\source\MongoDb::__construct()
43 */
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
44 class MongoDb extends \lithium\data\Source {
45
624b2a2 @jperras Fixing coding standards violations in MongoDB source documentation.
jperras authored
46 /**
2ac8bcb @jperras Adding missing class attribute definitions to Mongo source.
jperras authored
47 * The Mongo class instance.
48 *
49 * @var object
50 */
b6783d4 @nateabele Refactoring `MongoDb` adapter to make `$connection` property more con…
nateabele authored
51 public $server = null;
2ac8bcb @jperras Adding missing class attribute definitions to Mongo source.
jperras authored
52
53 /**
54 * The MongoDB object instance.
55 *
56 * @var object
57 */
b6783d4 @nateabele Refactoring `MongoDb` adapter to make `$connection` property more con…
nateabele authored
58 public $connection = null;
2ac8bcb @jperras Adding missing class attribute definitions to Mongo source.
jperras authored
59
60 /**
624b2a2 @jperras Fixing coding standards violations in MongoDB source documentation.
jperras authored
61 * Classes used by this class.
62 *
63 * @var array
64 */
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
65 protected $_classes = array(
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
66 'entity' => 'lithium\data\entity\Document',
67 'array' => 'lithium\data\collection\DocumentArray',
68 'set' => 'lithium\data\collection\DocumentSet',
69 'result' => 'lithium\data\source\mongo_db\Result',
70 'exporter' => 'lithium\data\source\mongo_db\Exporter',
26c2192 @davidpersson QA: Removing trailing comma in arrays, lowercasing some keywords.
davidpersson authored
71 'relationship' => 'lithium\data\model\Relationship'
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
72 );
73
18038f3 @nateabele Adding documentation to `model\Document` and `source\MongoDb` classes…
nateabele authored
74 /**
0badda6 @jperras Implemented conditional operator parsing to MongoDB source.
jperras authored
75 * Map of typical SQL-like operators to their MongoDB equivalents.
76 *
77 * @var array Keys are SQL-like operators, value is the MongoDB equivalent.
78 */
79 protected $_operators = array(
20d3736 @nateabele Refactoring collection and entity classes to use `cast()` method of t…
nateabele authored
80 '<' => '$lt',
81 '>' => '$gt',
82 '<=' => '$lte',
83 '>=' => '$gte',
84 '!=' => array('single' => '$ne', 'multiple' => '$nin'),
85 '<>' => array('single' => '$ne', 'multiple' => '$nin'),
86 'or' => '$or',
87 '||' => '$or',
88 'not' => '$not',
26c2192 @davidpersson QA: Removing trailing comma in arrays, lowercasing some keywords.
davidpersson authored
89 '!' => '$not'
0badda6 @jperras Implemented conditional operator parsing to MongoDB source.
jperras authored
90 );
91
92 /**
b6783d4 @nateabele Refactoring `MongoDb` adapter to make `$connection` property more con…
nateabele authored
93 * A closure or anonymous function which receives an instance of this class, a collection name
55ec7fc @nateabele Updating documentation for custom Mongo schema handlers in `\data\sou…
nateabele authored
94 * and associated meta information, and returns an array defining the schema for an associated
95 * model, where the keys are field names, and the values are arrays defining the type
96 * information for each field. At a minimum, type arrays must contain a `'type'` key. For more
97 * information on schema definitions, and an example schema callback implementation, see the
98 * `$_schema` property of the `Model` class.
b6783d4 @nateabele Refactoring `MongoDb` adapter to make `$connection` property more con…
nateabele authored
99 *
55ec7fc @nateabele Updating documentation for custom Mongo schema handlers in `\data\sou…
nateabele authored
100 * @see lithium\data\Model::$_schema
20d3736 @nateabele Refactoring collection and entity classes to use `cast()` method of t…
nateabele authored
101 * @var Closure
b6783d4 @nateabele Refactoring `MongoDb` adapter to make `$connection` property more con…
nateabele authored
102 */
103 protected $_schema = null;
104
105 /**
196fb8a @nateabele Implementing data value casting at object-write time, refactoring res…
nateabele authored
106 * An array of closures that handle casting values to specific types.
107 *
108 * @var array
109 */
110 protected $_handlers = array();
111
112 /**
b6783d4 @nateabele Refactoring `MongoDb` adapter to make `$connection` property more con…
nateabele authored
113 * List of configuration keys which will be automatically assigned to their corresponding
114 * protected class properties.
115 *
116 * @var array
117 */
a2d0076 @nateabele Fixing overwritten property that prevented custom classes from being …
nateabele authored
118 protected $_autoConfig = array('schema', 'handlers', 'classes' => 'merge');
b6783d4 @nateabele Refactoring `MongoDb` adapter to make `$connection` property more con…
nateabele authored
119
120 /**
18038f3 @nateabele Adding documentation to `model\Document` and `source\MongoDb` classes…
nateabele authored
121 * Instantiates the MongoDB adapter with the default connection information.
122 *
624b2a2 @jperras Fixing coding standards violations in MongoDB source documentation.
jperras authored
123 * @see lithium\data\Connections::add()
b6783d4 @nateabele Refactoring `MongoDb` adapter to make `$connection` property more con…
nateabele authored
124 * @see lithium\data\source\MongoDb::$_schema
ad3ccf3 @nateabele Cleanup and documentation for misc. `\data` classes. Refactored `Docu…
nateabele authored
125 * @link http://php.net/manual/en/mongo.construct.php PHP Manual: Mongo::__construct()
18038f3 @nateabele Adding documentation to `model\Document` and `source\MongoDb` classes…
nateabele authored
126 * @param array $config All information required to connect to the database, including:
20d3736 @nateabele Refactoring collection and entity classes to use `cast()` method of t…
nateabele authored
127 * - `'database'` _string_: The name of the database to connect to. Defaults to `null`.
0a244f2 @nateabele Standardizing on connection syntax to use `'host' => 'host:port'` in…
nateabele authored
128 * - `'host'` _string_: The IP or machine name where Mongo is running, followed by a
129 * colon, and the port number. Defaults to `'localhost:27017'`.
f111551 @nateabele Implementing fix for backwards-compatibility break in Mongo PECL exte…
nateabele authored
130 * - `'persistent'` _mixed_: Determines a persistent connection to attach to. See the
131 * `$options` parameter of
132 * [`Mongo::__construct()`](http://www.php.net/manual/en/mongo.construct.php) for
133 * more information. Defaults to `false`, meaning no persistent connection is made.
b6783d4 @nateabele Refactoring `MongoDb` adapter to make `$connection` property more con…
nateabele authored
134 * - `'timeout'` _integer_: The number of milliseconds a connection attempt will wait
135 * before timing out and throwing an exception. Defaults to `100`.
136 * - `'schema'` _closure_: A closure or anonymous function which returns the schema
137 * information for a model class. See the `$_schema` property for more information.
6524ad6 @nateabele Fixing issue in `\data\model\Query::export()`, where query data would…
nateabele authored
138 * - `'gridPrefix'` _string_: The default prefix for MongoDB's `chunks` and `files`
139 * collections. Defaults to `'fs'`.
f111551 @nateabele Implementing fix for backwards-compatibility break in Mongo PECL exte…
nateabele authored
140 * - `'replicaSet'` _boolean_: See the documentation for `Mongo::__construct()`. Defaults
141 * to `false`.
18038f3 @nateabele Adding documentation to `model\Document` and `source\MongoDb` classes…
nateabele authored
142 *
143 * Typically, these parameters are set in `Connections::add()`, when adding the adapter to the
144 * list of active connections.
145 */
f80098d @nateabele Adding type hinting to constructor `$config` arrays and all applicabl…
nateabele authored
146 public function __construct(array $config = array()) {
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
147 $defaults = array(
f111551 @nateabele Implementing fix for backwards-compatibility break in Mongo PECL exte…
nateabele authored
148 'persistent' => false,
4582e0f @nateabele Adding support for authenticated connections for MongoDB.
nateabele authored
149 'login' => null,
150 'password' => null,
f8b0bcc @jperras Updating \`data\source\MongoDb\`'s default connection string to use the
jperras authored
151 'host' => Mongo::DEFAULT_HOST . ':' . Mongo::DEFAULT_PORT,
20d3736 @nateabele Refactoring collection and entity classes to use `cast()` method of t…
nateabele authored
152 'database' => null,
cd443f2 @nateabele Implementing fixes for `\data\source\MongoDb::order()`, adding test c…
nateabele authored
153 'timeout' => 100,
f111551 @nateabele Implementing fix for backwards-compatibility break in Mongo PECL exte…
nateabele authored
154 'replicaSet' => false,
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
155 'schema' => null,
26c2192 @davidpersson QA: Removing trailing comma in arrays, lowercasing some keywords.
davidpersson authored
156 'gridPrefix' => 'fs'
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
157 );
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
158 parent::__construct($config + $defaults);
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
159 }
160
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
161 protected function _init() {
162 parent::_init();
163
164 $this->_operators += array(
165 'like' => function($key, $value) { return new MongoRegex($value); }
166 );
196fb8a @nateabele Implementing data value casting at object-write time, refactoring res…
nateabele authored
167
168 $this->_handlers += array(
169 'id' => function($v) {
170 return is_string($v) && preg_match('/^[0-9a-f]{24}$/', $v) ? new MongoId($v) : $v;
171 },
172 'date' => function($v) {
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
173 $v = is_numeric($v) ? intval($v) : strtotime($v);
6552390 @nateabele Fixing issue with `MongoDate` objects not being created with correct …
nateabele authored
174 return (!$v || time() == $v) ? new MongoDate() : new MongoDate($v);
196fb8a @nateabele Implementing data value casting at object-write time, refactoring res…
nateabele authored
175 },
176 'regex' => function($v) { return new MongoRegex($v); },
177 'integer' => function($v) { return (integer) $v; },
178 'float' => function($v) { return (float) $v; },
cfd2867 @nateabele Adding support for `'code'` and `'binary'` types to MongoDB adapter.
nateabele authored
179 'boolean' => function($v) { return (boolean) $v; },
180 'code' => function($v) { return new MongoCode($v); },
26c2192 @davidpersson QA: Removing trailing comma in arrays, lowercasing some keywords.
davidpersson authored
181 'binary' => function($v) { return new MongoBinData($v); }
196fb8a @nateabele Implementing data value casting at object-write time, refactoring res…
nateabele authored
182 );
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
183 }
184
18038f3 @nateabele Adding documentation to `model\Document` and `source\MongoDb` classes…
nateabele authored
185 /**
186 * Ensures that the server connection is closed and resources are freed when the adapter
187 * instance is destroyed.
188 *
189 * @return void
190 */
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
191 public function __destruct() {
192 if ($this->_isConnected) {
193 $this->disconnect();
194 }
195 }
196
e8e5c5b @nateabele Implementing auto-configuration for MongoDB, `MongoDb::create()`. Ref…
nateabele authored
197 /**
c6b4276 @nateabele Second draft of model relationships. Still not really working. Added …
nateabele authored
198 * With no parameter, checks to see if the `mongo` extension is installed. With a parameter,
199 * queries for a specific supported feature.
c4a4d0d @pointlessjon Implemented check for required extensions via ::enabled() for mysql a…
pointlessjon authored
200 *
c6b4276 @nateabele Second draft of model relationships. Still not really working. Added …
nateabele authored
201 * @param string $feature Test for support for a specific feature, i.e. `"transactions"` or
202 * `"arrays"`.
203 * @return boolean Returns `true` if the particular feature (or if MongoDB) support is enabled,
204 * otherwise `false`.
c4a4d0d @pointlessjon Implemented check for required extensions via ::enabled() for mysql a…
pointlessjon authored
205 */
c6b4276 @nateabele Second draft of model relationships. Still not really working. Added …
nateabele authored
206 public static function enabled($feature = null) {
207 if (!$feature) {
208 return extension_loaded('mongo');
209 }
210 $features = array(
211 'arrays' => true,
212 'transactions' => false,
213 'booleans' => true,
26c2192 @davidpersson QA: Removing trailing comma in arrays, lowercasing some keywords.
davidpersson authored
214 'relationships' => true
c6b4276 @nateabele Second draft of model relationships. Still not really working. Added …
nateabele authored
215 );
216 return isset($features[$feature]) ? $features[$feature] : null;
c4a4d0d @pointlessjon Implemented check for required extensions via ::enabled() for mysql a…
pointlessjon authored
217 }
218
219 /**
244646c @nateabele Adding test for `\data\Source`. Refactoring classes in `\data` into "…
nateabele authored
220 * Configures a model class by overriding the default dependencies for `'set'` and
221 * `'entity'` , and sets the primary key to `'_id'`, in keeping with Mongo's conventions.
e8e5c5b @nateabele Implementing auto-configuration for MongoDB, `MongoDb::create()`. Ref…
nateabele authored
222 *
624b2a2 @jperras Fixing coding standards violations in MongoDB source documentation.
jperras authored
223 * @see lithium\data\Model::$_meta
224 * @see lithium\data\Model::$_classes
e8e5c5b @nateabele Implementing auto-configuration for MongoDB, `MongoDb::create()`. Ref…
nateabele authored
225 * @param string $class The fully-namespaced model class name to be configured.
226 * @return Returns an array containing keys `'classes'` and `'meta'`, which will be merged with
227 * their respective properties in `Model`.
228 */
229 public function configureClass($class) {
b51972c @nateabele Beginning refactoring of... stuff.
nateabele authored
230 return array(
20d3736 @nateabele Refactoring collection and entity classes to use `cast()` method of t…
nateabele authored
231 'meta' => array('key' => '_id', 'locked' => false),
b40a91a @nateabele Removing default schema configuration from `configureClass()` method …
nateabele authored
232 'schema' => array()
b51972c @nateabele Beginning refactoring of... stuff.
nateabele authored
233 );
e8e5c5b @nateabele Implementing auto-configuration for MongoDB, `MongoDb::create()`. Ref…
nateabele authored
234 }
235
624b2a2 @jperras Fixing coding standards violations in MongoDB source documentation.
jperras authored
236 /**
ad3ccf3 @nateabele Cleanup and documentation for misc. `\data` classes. Refactored `Docu…
nateabele authored
237 * Connects to the Mongo server. Matches up parameters from the constructor to create a Mongo
238 * database connection.
624b2a2 @jperras Fixing coding standards violations in MongoDB source documentation.
jperras authored
239 *
ad3ccf3 @nateabele Cleanup and documentation for misc. `\data` classes. Refactored `Docu…
nateabele authored
240 * @see lithium\data\source\MongoDb::__construct()
241 * @link http://php.net/manual/en/mongo.construct.php PHP Manual: Mongo::__construct()
f111551 @nateabele Implementing fix for backwards-compatibility break in Mongo PECL exte…
nateabele authored
242 * @return boolean Returns `true` the connection attempt was successful, otherwise `false`.
624b2a2 @jperras Fixing coding standards violations in MongoDB source documentation.
jperras authored
243 */
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
244 public function connect() {
f111551 @nateabele Implementing fix for backwards-compatibility break in Mongo PECL exte…
nateabele authored
245 $cfg = $this->_config;
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
246 $this->_isConnected = false;
fab9309 @pointlessjon MongoDb::connect() now expects exception to handle unavailable connec…
pointlessjon authored
247
f111551 @nateabele Implementing fix for backwards-compatibility break in Mongo PECL exte…
nateabele authored
248 $host = is_array($cfg['host']) ? join(',', $cfg['host']) : $cfg['host'];
249 $login = $cfg['login'] ? "{$cfg['login']}:{$cfg['password']}@" : '';
250 $connection = "mongodb://{$login}{$host}" . ($login ? "/{$cfg['database']}" : '');
251 $options = array(
252 'connect' => true, 'timeout' => $cfg['timeout'], 'replicaSet' => $cfg['replicaSet']
253 );
4582e0f @nateabele Adding support for authenticated connections for MongoDB.
nateabele authored
254
fab9309 @pointlessjon MongoDb::connect() now expects exception to handle unavailable connec…
pointlessjon authored
255 try {
f111551 @nateabele Implementing fix for backwards-compatibility break in Mongo PECL exte…
nateabele authored
256 if ($persist = $cfg['persistent']) {
257 $options['persist'] = $persist === true ? 'default' : $persist;
258 }
259 $this->server = new Mongo($connection, $options);
260
261 if ($this->connection = $this->server->{$cfg['database']}) {
2b535bd @pointlessjon refactored MongoDb::connect() to catch errors on bad db name as well;…
pointlessjon authored
262 $this->_isConnected = true;
263 }
81ee38a @nateabele `MongoDb::connect()` now throws a `NetworkException` when the databas…
nateabele authored
264 } catch (Exception $e) {
265 throw new NetworkException("Could not connect to the database.", 503, $e);
266 }
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
267 return $this->_isConnected;
268 }
269
624b2a2 @jperras Fixing coding standards violations in MongoDB source documentation.
jperras authored
270 /**
271 * Disconnect from the Mongo server.
272 *
c4f14ec @jhuntwork Don't call Mongo->close()
jhuntwork authored
273 * Don't call the Mongo->close() method. The driver documentation states this should not
274 * be necessary since it auto disconnects when out of scope.
275 * With version 1.2.7, when using replica sets, close() can cause a segmentation fault.
276 *
277 * @return boolean True
624b2a2 @jperras Fixing coding standards violations in MongoDB source documentation.
jperras authored
278 */
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
279 public function disconnect() {
b6783d4 @nateabele Refactoring `MongoDb` adapter to make `$connection` property more con…
nateabele authored
280 if ($this->server && $this->server->connected) {
c4f14ec @jhuntwork Don't call Mongo->close()
jhuntwork authored
281 $this->_isConnected = false;
b6783d4 @nateabele Refactoring `MongoDb` adapter to make `$connection` property more con…
nateabele authored
282 unset($this->connection, $this->server);
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
283 }
284 return true;
285 }
286
cf93125 Docblocks for lithium\data.
psychic authored
287 /**
288 * Returns the list of collections in the currently-connected database.
289 *
290 * @param string $class The fully-name-spaced class name of the model object making the request.
291 * @return array Returns an array of objects to which models can connect.
292 */
0d22c1f @nateabele WARNING: API change - renaming `\data\Source::entities()` to `\data\S…
nateabele authored
293 public function sources($class = null) {
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
294 $this->_checkConnection();
295 $conn = $this->connection;
296 return array_map(function($col) { return $col->getName(); }, $conn->listCollections());
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
297 }
298
cf93125 Docblocks for lithium\data.
psychic authored
299 /**
b6783d4 @nateabele Refactoring `MongoDb` adapter to make `$connection` property more con…
nateabele authored
300 * Gets the column 'schema' for a given MongoDB collection. Only returns a schema if the
301 * `'schema'` configuration flag has been set in the constructor.
cf93125 Docblocks for lithium\data.
psychic authored
302 *
b6783d4 @nateabele Refactoring `MongoDb` adapter to make `$connection` property more con…
nateabele authored
303 * @see lithium\data\source\MongoDb::$_schema
326ecba @nateabele Refactoring `\data` to change all references of `'table'` to `'source'`.
nateabele authored
304 * @param mixed $entity Would normally specify a collection name.
cf93125 Docblocks for lithium\data.
psychic authored
305 * @param array $meta
326ecba @nateabele Refactoring `\data` to change all references of `'table'` to `'source'`.
nateabele authored
306 * @return array Returns an associative array describing the given collection's schema.
cf93125 Docblocks for lithium\data.
psychic authored
307 */
b6783d4 @nateabele Refactoring `MongoDb` adapter to make `$connection` property more con…
nateabele authored
308 public function describe($entity, array $meta = array()) {
309 if (!$schema = $this->_schema) {
310 return array();
311 }
312 return $schema($this, $entity, $meta);
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
313 }
314
ca6a58d @niel Updating dockblocks.
niel authored
315 /**
316 * Quotes identifiers.
317 *
318 * MongoDb does not need identifiers quoted, so this method simply returns the identifier.
319 *
320 * @param string $name The identifier to quote.
321 * @return string The quoted identifier.
322 */
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
323 public function name($name) {
324 return $name;
325 }
326
327 /**
328 * A method dispatcher that allows direct calls to native methods in PHP's `Mongo` object. Read
329 * more here: http://php.net/manual/class.mongo.php
330 *
331 * For example (assuming this instance is stored in `Connections` as `'mongo'`):
332 * {{{// Manually repairs a MongoDB instance
333 * Connections::get('mongo')->repairDB($db); // returns null
334 * }}}
335 *
336 * @param string $method The name of native method to call. See the link above for available
337 * class methods.
338 * @param array $params A list of parameters to be passed to the native method.
ca6a58d @niel Updating dockblocks.
niel authored
339 * @return mixed The return value of the native method specified in `$method`.
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
340 */
341 public function __call($method, $params) {
81ee38a @nateabele `MongoDb::connect()` now throws a `NetworkException` when the databas…
nateabele authored
342 if ((!$this->server) && !$this->connect()) {
343 return null;
344 }
b6783d4 @nateabele Refactoring `MongoDb` adapter to make `$connection` property more con…
nateabele authored
345 return call_user_func_array(array(&$this->server, $method), $params);
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
346 }
347
cf93125 Docblocks for lithium\data.
psychic authored
348 /**
6423a4d @jperras Updating MongoDB datasource to throw exceptions for
jperras authored
349 * Normally used in cases where the query is a raw string (as opposed to a `Query` object),
350 * to database must determine the correct column names from the result resource. Not
cf93125 Docblocks for lithium\data.
psychic authored
351 * applicable to this data source.
352 *
353 * @param mixed $query
354 * @param resource $resource
355 * @param object $context
356 * @return array
357 */
7786ba1 @nateabele Renaming `source\Database::columns()` to `source\Database::schema()`.
nateabele authored
358 public function schema($query, $resource = null, $context = null) {
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
359 return array();
7786ba1 @nateabele Renaming `source\Database::columns()` to `source\Database::schema()`.
nateabele authored
360 }
361
cf93125 Docblocks for lithium\data.
psychic authored
362 /**
363 * Create new document
364 *
365 * @param string $query
97c5e7c @Howard3 Doing a general QA and Docblock
Howard3 authored
366 * @param array $options
cf93125 Docblocks for lithium\data.
psychic authored
367 * @return boolean
59cb735 @nateabele Ensuring `@filter` tags are present in docblocks that support filteri…
nateabele authored
368 * @filter
cf93125 Docblocks for lithium\data.
psychic authored
369 */
643accb @nateabele Updating data branch to 0.6.
nateabele authored
370 public function create($query, array $options = array()) {
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
371 $defaults = array('safe' => false, 'fsync' => false);
372 $options += $defaults;
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
373 $this->_checkConnection();
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
374
e8e5c5b @nateabele Implementing auto-configuration for MongoDB, `MongoDb::create()`. Ref…
nateabele authored
375 $params = compact('query', 'options');
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
376 $_config = $this->_config;
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
377 $_exp = $this->_classes['exporter'];
e8e5c5b @nateabele Implementing auto-configuration for MongoDB, `MongoDb::create()`. Ref…
nateabele authored
378
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
379 return $this->_filter(__METHOD__, $params, function($self, $params) use ($_config, $_exp) {
380 $query = $params['query'];
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
381 $options = $params['options'];
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
382
1cde591 @nateabele Ensuring `MongoDb::create()` uses the correct data export procedure.
nateabele authored
383 $args = $query->export($self, array('keys' => array('source', 'data')));
384 $data = $_exp::get('create', $args['data']);
385 $source = $args['source'];
386
387 if ($source == "{$_config['gridPrefix']}.files" && isset($data['create']['file'])) {
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
388 $result = array('ok' => true);
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
389 $data['create']['_id'] = $self->invokeMethod('_saveFile', array($data['create']));
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
390 } else {
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
391 $result = $self->connection->{$source}->insert($data['create'], $options);
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
392 }
8243bc8 @nateabele Fixing issue with data sources overwriting model configuration, refac…
nateabele authored
393
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
394 if ($result === true || isset($result['ok']) && (boolean) $result['ok'] === true) {
395 if ($query->entity()) {
b58092f @nateabele Renaming `Entity::update()` to `Entity::sync()` to better reflect fun…
nateabele authored
396 $query->entity()->sync($data['create']['_id']);
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
397 }
8243bc8 @nateabele Fixing issue with data sources overwriting model configuration, refac…
nateabele authored
398 return true;
399 }
400 return false;
e8e5c5b @nateabele Implementing auto-configuration for MongoDB, `MongoDb::create()`. Ref…
nateabele authored
401 });
402 }
403
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
404 protected function _saveFile($data) {
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
405 $uploadKeys = array('name', 'type', 'tmp_name', 'error', 'size');
406 $grid = $this->connection->getGridFS();
407 $file = null;
408 $method = null;
409
410 switch (true) {
411 case (is_array($data['file']) && array_keys($data['file']) == $uploadKeys):
412 if (!$data['file']['error'] && is_uploaded_file($data['file']['tmp_name'])) {
413 $method = 'storeFile';
414 $file = $data['file']['tmp_name'];
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
415 $data['filename'] = $data['file']['name'];
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
416 }
417 break;
418 case (is_string($data['file']) && file_exists($data['file'])):
419 $method = 'storeFile';
420 $file = $data['file'];
421 break;
422 case $data['file']:
423 $method = 'storeBytes';
424 $file = $data['file'];
425 break;
426 }
427
428 if (!$method || !$file) {
429 return;
430 }
431
432 if (isset($data['_id'])) {
433 $data += (array) get_object_vars($grid->get($data['_id']));
434 $grid->delete($data['_id']);
435 }
436 unset($data['file']);
196fb8a @nateabele Implementing data value casting at object-write time, refactoring res…
nateabele authored
437 return $grid->{$method}($file, $data);
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
438 }
439
cf93125 Docblocks for lithium\data.
psychic authored
440 /**
441 * Read from document
442 *
443 * @param string $query
97c5e7c @Howard3 Doing a general QA and Docblock
Howard3 authored
444 * @param array $options
cf93125 Docblocks for lithium\data.
psychic authored
445 * @return object
59cb735 @nateabele Ensuring `@filter` tags are present in docblocks that support filteri…
nateabele authored
446 * @filter
cf93125 Docblocks for lithium\data.
psychic authored
447 */
f80098d @nateabele Adding type hinting to constructor `$config` arrays and all applicabl…
nateabele authored
448 public function read($query, array $options = array()) {
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
449 $this->_checkConnection();
b51972c @nateabele Beginning refactoring of... stuff.
nateabele authored
450 $defaults = array('return' => 'resource');
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
451 $options += $defaults;
b51972c @nateabele Beginning refactoring of... stuff.
nateabele authored
452
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
453 $params = compact('query', 'options');
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
454 $_config = $this->_config;
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
455
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
456 return $this->_filter(__METHOD__, $params, function($self, $params) use ($_config) {
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
457 $query = $params['query'];
458 $options = $params['options'];
b6783d4 @nateabele Refactoring `MongoDb` adapter to make `$connection` property more con…
nateabele authored
459 $args = $query->export($self);
326ecba @nateabele Refactoring `\data` to change all references of `'table'` to `'source'`.
nateabele authored
460 $source = $args['source'];
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
461
b6783d4 @nateabele Refactoring `MongoDb` adapter to make `$connection` property more con…
nateabele authored
462 if ($group = $args['group']) {
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
463 $result = $self->invokeMethod('_group', array($group, $args, $options));
464 $config = array('class' => 'set') + compact('query') + $result;
b51972c @nateabele Beginning refactoring of... stuff.
nateabele authored
465 return $self->item($query->model(), $config['data'], $config);
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
466 }
467 $collection = $self->connection->{$source};
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
468
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
469 if ($source == "{$_config['gridPrefix']}.files") {
470 $collection = $self->connection->getGridFS();
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
471 }
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
472 $result = $collection->find($args['conditions'], $args['fields']);
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
473
474 if ($query->calculate()) {
475 return $result;
476 }
196fb8a @nateabele Implementing data value casting at object-write time, refactoring res…
nateabele authored
477 $resource = $result->sort($args['order'])->limit($args['limit'])->skip($args['offset']);
478 $result = $self->invokeMethod('_instance', array('result', compact('resource')));
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
479 $config = compact('result', 'query') + array('class' => 'set');
b51972c @nateabele Beginning refactoring of... stuff.
nateabele authored
480 return $self->item($query->model(), array(), $config);
e8e5c5b @nateabele Implementing auto-configuration for MongoDB, `MongoDb::create()`. Ref…
nateabele authored
481 });
482 }
483
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
484 protected function _group($group, $args, $options) {
485 $conditions = $args['conditions'];
486 $group += array('$reduce' => $args['reduce'], 'initial' => $args['initial']);
487 $command = array('group' => $group + array('ns' => $args['source'], 'cond' => $conditions));
488
489 $stats = $this->connection->command($command);
490 $data = isset($stats['retval']) ? $stats['retval'] : null;
491 unset($stats['retval']);
492 return compact('data', 'stats');
493 }
494
cf93125 Docblocks for lithium\data.
psychic authored
495 /**
496 * Update document
497 *
498 * @param string $query
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
499 * @param array $options
cf93125 Docblocks for lithium\data.
psychic authored
500 * @return boolean
59cb735 @nateabele Ensuring `@filter` tags are present in docblocks that support filteri…
nateabele authored
501 * @filter
cf93125 Docblocks for lithium\data.
psychic authored
502 */
f80098d @nateabele Adding type hinting to constructor `$config` arrays and all applicabl…
nateabele authored
503 public function update($query, array $options = array()) {
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
504 $defaults = array('upsert' => false, 'multiple' => true, 'safe' => false, 'fsync' => false);
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
505 $options += $defaults;
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
506 $this->_checkConnection();
507
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
508 $params = compact('query', 'options');
509 $_config = $this->_config;
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
510 $_exp = $this->_classes['exporter'];
6423a4d @jperras Updating MongoDB datasource to throw exceptions for
jperras authored
511
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
512 return $this->_filter(__METHOD__, $params, function($self, $params) use ($_config, $_exp) {
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
513 $options = $params['options'];
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
514 $query = $params['query'];
ae47f61 @nateabele Ensuring `MongoDb::update()` always uses atomic operators when necess…
nateabele authored
515 $args = $query->export($self, array('keys' => array('conditions', 'source', 'data')));
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
516 $source = $args['source'];
517 $data = $args['data'];
518
519 if ($query->entity()) {
520 $data = $_exp::get('update', $data);
521 }
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
522
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
523 if ($source == "{$_config['gridPrefix']}.files" && isset($data['update']['file'])) {
524 $args['data']['_id'] = $self->invokeMethod('_saveFile', array($data['update']));
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
525 }
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
526 $update = $query->entity() ? $_exp::toCommand($data) : $data;
8243bc8 @nateabele Fixing issue with data sources overwriting model configuration, refac…
nateabele authored
527
fb07436 @nateabele Ensuring `MongoDb::update()` always uses atomic operators when necess…
nateabele authored
528 if ($options['multiple'] && !preg_grep('/^\$/', array_keys($update))) {
529 $update = array('$set' => $update);
530 }
3020a0b @nateabele Missing `$options` parameter pass-down from `MongoDb::update()`.
nateabele authored
531 if ($self->connection->{$source}->update($args['conditions'], $update, $options)) {
b58092f @nateabele Renaming `Entity::update()` to `Entity::sync()` to better reflect fun…
nateabele authored
532 $query->entity() ? $query->entity()->sync() : null;
8243bc8 @nateabele Fixing issue with data sources overwriting model configuration, refac…
nateabele authored
533 return true;
534 }
535 return false;
536 });
537 }
538
cf93125 Docblocks for lithium\data.
psychic authored
539 /**
540 * Delete document
541 *
542 * @param string $query
97c5e7c @Howard3 Doing a general QA and Docblock
Howard3 authored
543 * @param array $options
cf93125 Docblocks for lithium\data.
psychic authored
544 * @return boolean
59cb735 @nateabele Ensuring `@filter` tags are present in docblocks that support filteri…
nateabele authored
545 * @filter
cf93125 Docblocks for lithium\data.
psychic authored
546 */
f80098d @nateabele Adding type hinting to constructor `$config` arrays and all applicabl…
nateabele authored
547 public function delete($query, array $options = array()) {
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
548 $this->_checkConnection();
3671583 @nateabele Implementing `$options` flags in `\data\source\MongoDb::delete()`.
nateabele authored
549 $defaults = array('justOne' => false, 'safe' => false, 'fsync' => false);
550 $options = array_intersect_key($options + $defaults, $defaults);
02f0d1f Use the GridFS remove function for models that use the fs.files sourc…
Marc Ghorayeb authored
551 $_config = $this->_config;
b60fa67 @mackstar Syntax fixes
mackstar authored
552 $params = compact('query', 'options');
6423a4d @jperras Updating MongoDB datasource to throw exceptions for
jperras authored
553
b60fa67 @mackstar Syntax fixes
mackstar authored
554 return $this->_filter(__METHOD__, $params, function($self, $params) use ($_config) {
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
555 $query = $params['query'];
556 $options = $params['options'];
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
557 $args = $query->export($self, array('keys' => array('source', 'conditions')));
02f0d1f Use the GridFS remove function for models that use the fs.files sourc…
Marc Ghorayeb authored
558 $source = $args['source'];
559
560 if ($source == "{$_config['gridPrefix']}.files") {
561 return $self->invokeMethod('_deleteFile', array($args['conditions']));
562 }
563
3671583 @nateabele Implementing `$options` flags in `\data\source\MongoDb::delete()`.
nateabele authored
564 return $self->connection->{$args['source']}->remove($args['conditions'], $options);
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
565 });
566 }
567
02f0d1f Use the GridFS remove function for models that use the fs.files sourc…
Marc Ghorayeb authored
568 protected function _deleteFile($conditions, $options = array()) {
569 $defaults = array('safe' => true);
570 $options += $defaults;
571
572 $grid = $this->connection->getGridFS();
b60fa67 @mackstar Syntax fixes
mackstar authored
573
02f0d1f Use the GridFS remove function for models that use the fs.files sourc…
Marc Ghorayeb authored
574 return $grid->remove($conditions, $options);
575 }
576
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
577 /**
885c124 @jperras Adding some missing MongoDB docblocks.
jperras authored
578 * Executes calculation-related queries, such as those required for `count`.
579 *
580 * @param string $type Only accepts `count`.
581 * @param mixed $query The query to be executed.
582 * @param array $options Optional arguments for the `read()` query that will be executed
583 * to obtain the calculation result.
584 * @return integer Result of the calculation.
585 */
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
586 public function calculation($type, $query, array $options = array()) {
587 $query->calculate($type);
588
589 switch ($type) {
590 case 'count':
591 return $this->read($query, $options)->count();
592 }
593 }
594
885c124 @jperras Adding some missing MongoDB docblocks.
jperras authored
595 /**
596 * Document relationships.
597 *
598 * @param string $class
599 * @param string $type Relationship type, e.g. `belongsTo`.
600 * @param string $name
c6b4276 @nateabele Second draft of model relationships. Still not really working. Added …
nateabele authored
601 * @param array $config
885c124 @jperras Adding some missing MongoDB docblocks.
jperras authored
602 * @return array
603 */
c6b4276 @nateabele Second draft of model relationships. Still not really working. Added …
nateabele authored
604 public function relationship($class, $type, $name, array $config = array()) {
18b6a8c @nateabele Refactoring MongoDB changeset calculation for update operations, to i…
nateabele authored
605 $key = Inflector::camelize($type == 'belongsTo' ? $class::meta('name') : $name, false);
5e8b377 @nateabele Updating model relationship handling to use fully-namespaced class na…
nateabele authored
606
18b6a8c @nateabele Refactoring MongoDB changeset calculation for update operations, to i…
nateabele authored
607 $config += compact('name', 'type', 'key');
c6b4276 @nateabele Second draft of model relationships. Still not really working. Added …
nateabele authored
608 $config['from'] = $class;
8bd78fa @nateabele Refactoring model relationship handling, implementing as object. Firs…
nateabele authored
609 $relationship = $this->_classes['relationship'];
610
611 $defaultLinks = array(
612 'hasOne' => $relationship::LINK_EMBEDDED,
613 'hasMany' => $relationship::LINK_EMBEDDED,
614 'belongsTo' => $relationship::LINK_CONTAINED
615 );
c6b4276 @nateabele Second draft of model relationships. Still not really working. Added …
nateabele authored
616 $config += array('link' => $defaultLinks[$type]);
617 return new $relationship($config);
1769f9c @nateabele Refactoring data layer to move model relationship initialization from…
nateabele authored
618 }
619
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
620 /**
1540b69 @nateabele Fixing status value check in `MongoDb::create()` for compatibility wi…
nateabele authored
621 * Formats `group` clauses for MongoDB.
885c124 @jperras Adding some missing MongoDB docblocks.
jperras authored
622 *
623 * @param string|array $group The group clause.
624 * @param object $context
625 * @return array Formatted `group` clause.
626 */
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
627 public function group($group, $context) {
628 if (!$group) {
629 return;
630 }
a7c3d96 @nateabele Fixing grouping queries in MongoDB.
nateabele authored
631 if (is_string($group) && strpos($group, 'function') === 0) {
632 return array('$keyf' => new MongoCode($group));
633 }
634 $group = (array) $group;
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
635
a7c3d96 @nateabele Fixing grouping queries in MongoDB.
nateabele authored
636 foreach ($group as $i => $field) {
637 if (is_int($i)) {
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
638 $group[$field] = true;
639 unset($group[$i]);
640 }
641 }
a7c3d96 @nateabele Fixing grouping queries in MongoDB.
nateabele authored
642 return array('key' => $group);
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
643 }
644
c20f5ae @jperras Adding $nin (NOT IN in SQL-speak) condition parsing for MongoDB source.
jperras authored
645 /**
20d3736 @nateabele Refactoring collection and entity classes to use `cast()` method of t…
nateabele authored
646 * Maps incoming conditions with their corresponding MongoDB-native operators.
c20f5ae @jperras Adding $nin (NOT IN in SQL-speak) condition parsing for MongoDB source.
jperras authored
647 *
648 * @param array $conditions Array of conditions
649 * @param object $context Context with which this method was called; currently
650 * inspects the return value of `$context->type()`.
651 * @return array Transformed conditions
652 */
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
653 public function conditions($conditions, $context) {
20d3736 @nateabele Refactoring collection and entity classes to use `cast()` method of t…
nateabele authored
654 $schema = array();
655 $model = null;
656
657 if (!$conditions) {
658 return array();
659 }
660 if ($code = $this->_isMongoCode($conditions)) {
661 return $code;
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
662 }
20d3736 @nateabele Refactoring collection and entity classes to use `cast()` method of t…
nateabele authored
663 if ($context) {
664 $model = $context->model();
665 $schema = $context->schema();
666 }
667 return $this->_conditions($conditions, $model, $schema, $context);
668 }
669
670 protected function _conditions($conditions, $model, $schema, $context) {
671 $castOpts = compact('schema') + array('first' => true, 'arrays' => false);
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
672
673 foreach ($conditions as $key => $value) {
20d3736 @nateabele Refactoring collection and entity classes to use `cast()` method of t…
nateabele authored
674 if ($key === '$or' || $key === 'or' || $key === '||') {
675 foreach ($value as $i => $or) {
676 $value[$i] = $this->_conditions($or, $model, $schema, $context);
677 }
678 unset($conditions[$key]);
679 $conditions['$or'] = $value;
680 continue;
681 }
682 if (is_object($value)) {
683 continue;
684 }
685 if (!is_array($value)) {
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
686 $conditions[$key] = $this->cast(null, array($key => $value), $castOpts);
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
687 continue;
688 }
20d3736 @nateabele Refactoring collection and entity classes to use `cast()` method of t…
nateabele authored
689 $current = key($value);
690 $isOpArray = (isset($this->_operators[$current]) || $current[0] === '$');
c20f5ae @jperras Adding $nin (NOT IN in SQL-speak) condition parsing for MongoDB source.
jperras authored
691
20d3736 @nateabele Refactoring collection and entity classes to use `cast()` method of t…
nateabele authored
692 if (!$isOpArray) {
693 $data = array($key => $value);
694 $conditions[$key] = array('$in' => $this->cast($model, $data, $castOpts));
0badda6 @jperras Implemented conditional operator parsing to MongoDB source.
jperras authored
695 continue;
696 }
20d3736 @nateabele Refactoring collection and entity classes to use `cast()` method of t…
nateabele authored
697 $operations = array();
698
699 foreach ($value as $op => $val) {
700 if (is_object($result = $this->_operator($model, $key, $op, $val, $schema))) {
701 $operations = $result;
702 break;
703 }
552f9e5 @nateabele Fixing MongoDB conditions queries with multiple operators against a s…
nateabele authored
704 $operations += $this->_operator($model, $key, $op, $val, $schema);
e8e5c5b @nateabele Implementing auto-configuration for MongoDB, `MongoDb::create()`. Ref…
nateabele authored
705 }
552f9e5 @nateabele Fixing MongoDB conditions queries with multiple operators against a s…
nateabele authored
706 $conditions[$key] = $operations;
e8e5c5b @nateabele Implementing auto-configuration for MongoDB, `MongoDb::create()`. Ref…
nateabele authored
707 }
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
708 return $conditions;
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
709 }
710
20d3736 @nateabele Refactoring collection and entity classes to use `cast()` method of t…
nateabele authored
711 protected function _isMongoCode($conditions) {
712 if ($conditions instanceof MongoCode) {
713 return array('$where' => $conditions);
714 }
715 if (is_string($conditions)) {
716 return array('$where' => new MongoCode($conditions));
717 }
718 }
719
720 protected function _operator($model, $key, $op, $value, $schema) {
721 $castOpts = compact('schema') + array('first' => true, 'arrays' => false);
722
723 switch (true) {
724 case !isset($this->_operators[$op]):
725 return array($op => $this->cast($model, array($key => $value), $castOpts));
726 case is_callable($this->_operators[$op]):
727 return $this->_operators[$op]($key, $value);
728 case is_array($this->_operators[$op]):
729 $format = (is_array($value)) ? 'multiple' : 'single';
730 $operator = $this->_operators[$op][$format];
731 break;
732 default:
733 $operator = $this->_operators[$op];
734 break;
735 }
736 return array($operator => $value);
737 }
738
885c124 @jperras Adding some missing MongoDB docblocks.
jperras authored
739 /**
740 * Return formatted identifiers for fields.
741 *
742 * MongoDB does nt require field identifer escaping; as a result,
743 * this method is not implemented.
744 *
745 * @param array $fields Fields to be parsed
746 * @param object $context
747 * @return array Parsed fields array
748 */
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
749 public function fields($fields, $context) {
750 return $fields ?: array();
751 }
752
885c124 @jperras Adding some missing MongoDB docblocks.
jperras authored
753 /**
754 * Return formatted clause for limit.
755 *
756 * MongoDB does nt require limit identifer formatting; as a result,
757 * this method is not implemented.
758 *
759 * @param mixed $limit The `limit` clause to be formatted
760 * @param object $context
761 * @return mixed Formatted `limit` clause.
762 */
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
763 public function limit($limit, $context) {
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
764 return $limit ?: 0;
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
765 }
766
885c124 @jperras Adding some missing MongoDB docblocks.
jperras authored
767 /**
768 * Return formatted clause for order.
769 *
770 * @param mixed $order The `order` clause to be formatted
771 * @param object $context
772 * @return mixed Formatted `order` clause.
773 */
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
774 public function order($order, $context) {
cd443f2 @nateabele Implementing fixes for `\data\source\MongoDb::order()`, adding test c…
nateabele authored
775 switch (true) {
776 case !$order:
777 return array();
778 case is_string($order):
779 return array($order => 1);
780 case is_array($order):
781 foreach ($order as $key => $value) {
782 if (!is_string($key)) {
783 unset($order[$key]);
784 $order[$value] = 1;
785 continue;
786 }
787 if (is_string($value)) {
788 $order[$key] = strtoupper($value) == 'ASC' ? 1 : -1;
789 }
790 }
791 break;
792 }
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
793 return $order ?: array();
794 }
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
795
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
796 public function cast($entity, array $data, array $options = array()) {
18b6a8c @nateabele Refactoring MongoDB changeset calculation for update operations, to i…
nateabele authored
797 $defaults = array('schema' => null, 'first' => false);
20d3736 @nateabele Refactoring collection and entity classes to use `cast()` method of t…
nateabele authored
798 $options += $defaults;
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
799 $model = null;
18b6a8c @nateabele Refactoring MongoDB changeset calculation for update operations, to i…
nateabele authored
800 $exists = false;
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
801
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
802 if (!$data) {
803 return $data;
804 }
318a6dd @nateabele Adding test case for `\data\Entity`.
nateabele authored
805
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
806 if (is_string($entity)) {
807 $model = $entity;
808 $entity = null;
809 $options['schema'] = $options['schema'] ?: $model::schema();
18b6a8c @nateabele Refactoring MongoDB changeset calculation for update operations, to i…
nateabele authored
810 } elseif ($entity) {
811 $options['schema'] = $options['schema'] ?: $entity->schema();
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
812 $model = $entity->model();
18b6a8c @nateabele Refactoring MongoDB changeset calculation for update operations, to i…
nateabele authored
813
acb4261 @m4rcsch fixed whitespace issues, docblock errors and line length errors
m4rcsch authored
814 if ($entity instanceof $this->_classes['entity']) {
18b6a8c @nateabele Refactoring MongoDB changeset calculation for update operations, to i…
nateabele authored
815 $exists = $entity->exists();
816 }
b51972c @nateabele Beginning refactoring of... stuff.
nateabele authored
817 }
eaa5770 @nateabele Rewriting change-tracking in data entities. Original state and change…
nateabele authored
818 $schema = $options['schema'] ?: array('_id' => array('type' => 'id'));
196fb8a @nateabele Implementing data value casting at object-write time, refactoring res…
nateabele authored
819 unset($options['schema']);
20d3736 @nateabele Refactoring collection and entity classes to use `cast()` method of t…
nateabele authored
820
18b6a8c @nateabele Refactoring MongoDB changeset calculation for update operations, to i…
nateabele authored
821 $exporter = $this->_classes['exporter'];
822 $options += compact('model', 'exists') + array('handlers' => $this->_handlers);
39e131a @nateabele Updating CRUD integration test, and fixing CRUD operations for CouchD…
nateabele authored
823 return parent::cast($entity, $exporter::cast($data, $schema, $this, $options), $options);
47cedcd @nateabele Enabling filtering for `\data\Model::create()`. Refactoring data laye…
nateabele authored
824 }
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
825
826 protected function _checkConnection() {
827 if (!$this->_isConnected && !$this->connect()) {
00ce46c @nateabele Implemented core exceptions. Implemented `\core\Libraries::instance()…
nateabele authored
828 throw new NetworkException("Could not connect to the database.");
f7ae4c1 @nateabele Refactoring base classes in `\data`, implementing one-to-many relatio…
nateabele authored
829 }
830 }
ef55ddb @nateabele First pass at MongoDB adapter support. Allowed `Collection::first()` …
nateabele authored
831 }
832
833 ?>
Something went wrong with that request. Please try again.