diff --git a/README.md b/README.md index 51b03fa..e05c84b 100755 --- a/README.md +++ b/README.md @@ -155,6 +155,13 @@ $user->posts()->create($arrayWithPostData); // and not like ParseQuery's `get` which finds an object by id. $posts = Post::where('createdAt', '<=', $date)->descending('score')->get(); +// passing an array of conditions +$posts = Post::where([ + ['status', '=', '1'], + ['subscribed', '!=', '1'] + ]) + ->get(); + $posts = Post::where([ 'creator' => $user, 'title' => $title diff --git a/composer.json b/composer.json index 5da3af6..e09b431 100755 --- a/composer.json +++ b/composer.json @@ -6,7 +6,7 @@ "require": { "php": ">=5.6", "laravel/framework": "^5.2", - "parse/php-sdk": "~1.2.0" + "parse/php-sdk": "~1.4.0" }, "autoload": { "psr-4": { diff --git a/src/ObjectModel.php b/src/ObjectModel.php index 2c3dd22..3b1aca5 100755 --- a/src/ObjectModel.php +++ b/src/ObjectModel.php @@ -103,9 +103,9 @@ public static function all($useMasterKey = null) $useMasterKey = static::$defaultUseMasterKey; } - return static::query($useMasterKey)->get(); + return static::query($useMasterKey)->getAll(); } - + /** * Set the default value for defaultUseMasterKey. This is intended to be used * as a global configuration, hence the value is set to "self" and not to "static". @@ -389,7 +389,7 @@ public function addUnique($key, $value) public function fetch($force = false) { if (!$this->hasBeenFetched() || $force) { - $this->parseObject->fetch(); + $this->parseObject->fetch($this->useMasterKey); } return $this; diff --git a/src/Query.php b/src/Query.php index d6cfd1c..b1a0607 100755 --- a/src/Query.php +++ b/src/Query.php @@ -19,6 +19,9 @@ class Query '<' => 'lessThan', '<=' => 'lessThanOrEqualTo', 'in' => 'containedIn', + '%%' => 'matches', + '.%' => 'startsWith', + '%.' => 'endsWith' ]; /** @@ -170,11 +173,34 @@ public function orQuery() * @return $this */ public function where($key, $operator = null, $value = null) - { + { if (is_array($key)) { $where = $key; - + foreach ($where as $key => $value) { + if (is_array($value)) { + + if (count($value) !== 3) { + continue; + } + + if (!array_key_exists($value[1], self::OPERATORS)) { + throw new Exception("Invalid operator: " . $value[1]); + } + + if ($value[2] instanceof ObjectModel) { + $value[2] = $value[2]->getParseObject(); + } + + if (self::OPERATORS[$value[1]] === 'matches') { + call_user_func([$this, self::OPERATORS[$value[1]]], $value[0], $value[2], 'i'); + } else { + call_user_func([$this, self::OPERATORS[$value[1]]], $value[0], $value[2]); + } + + continue; + } + if ($value instanceof ObjectModel) { $value = $value->getParseObject(); } @@ -192,7 +218,11 @@ public function where($key, $operator = null, $value = null) throw new Exception("Invalid operator: " . $operator); } - call_user_func([$this, self::OPERATORS[$operator]], $key, $value); + if (self::OPERATORS[$operator] === 'matches') { + call_user_func([$this, self::OPERATORS[$operator]], $key, $value, 'i'); + } else { + call_user_func([$this, self::OPERATORS[$operator]], $key, $value); + } } return $this; @@ -375,6 +405,39 @@ public function get($selectKeys = null) return $this->createModels($this->parseQuery->find($this->useMasterKey)); } + /** + * Get all records. + * + * + * + * @return Collection + */ + public function getAll() + { + $results = []; + + $query = $this->parseQuery + ->ascending('objectId') + ->limit(1000) + ->find($this->useMasterKey); + + $results = array_merge($results, $query); + + while (!empty($query)) { + $lastObjectId = end($query)->getObjectId(); + + $query = $this->parseQuery + ->greaterThan('objectId', $lastObjectId) + ->ascending('objectId') + ->limit(1000) + ->find($this->useMasterKey); + + $results = array_merge($results, $query); + } + + return $this->createModels($results); + } + /** * Allow to pass instances of either Query or ParseQuery. * diff --git a/src/Relations/BelongsToMany.php b/src/Relations/BelongsToMany.php index 3a5e1fd..5bf5f4a 100755 --- a/src/Relations/BelongsToMany.php +++ b/src/Relations/BelongsToMany.php @@ -76,13 +76,18 @@ public function save($others, $unique = true) protected function createItems() { - $items = $this->parentObject->getParseObject()->get($this->keyName); - + $query = $this->parentObject + ->getParseObject() + ->get($this->keyName) + ->getQuery(); + + $items = $query->find(); + if ($items) { $class = $this->embeddedClass; foreach ($items as $item) { - $this->collection[] = new $class($item); + $this->collection[] = new $class($item->fetch()); } } }