Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix get all records #19

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
6 changes: 3 additions & 3 deletions src/ObjectModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -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".
Expand Down Expand Up @@ -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;
Expand Down
69 changes: 66 additions & 3 deletions src/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ class Query
'<' => 'lessThan',
'<=' => 'lessThanOrEqualTo',
'in' => 'containedIn',
'%%' => 'matches',
'.%' => 'startsWith',
'%.' => 'endsWith'
];

/**
Expand Down Expand Up @@ -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();
}
Expand All @@ -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;
Expand Down Expand Up @@ -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.
*
Expand Down
11 changes: 8 additions & 3 deletions src/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}
Expand Down