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

Version, Ice 1.6.2 #276

Merged
merged 3 commits into from
Sep 12, 2020
Merged
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
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
-------------------------------------------------------------------
Sat Sep 12 06:11:32 UTC 2020

- Ice 1.6.2
* Model, get related record if field is null fix #275
* Pagination, calculate if data is array or total is specified #273
* Url, fixed getStatic() #193

-------------------------------------------------------------------
Sun May 10 14:32:39 UTC 2020

Expand Down
12 changes: 11 additions & 1 deletion ice/mvc/model.zep
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,16 @@ abstract class Model extends Arr implements \Serializable
return this->loadOne(filters);
}

/**
* Check whether model is loaded.
*
* @return boolean
*/
public function loaded() -> boolean
{
return this->isLoaded ? true : false;
}

/**
* Get the last Db error.
*
Expand Down Expand Up @@ -707,7 +717,7 @@ abstract class Model extends Arr implements \Serializable
let filters[referencedField] = this->{field},
result = create_instance_params(referenceModel, [filters, null, options]);

if !result->count() {
if !result->loaded() {
return false;
}

Expand Down
33 changes: 21 additions & 12 deletions ice/pagination.zep
Original file line number Diff line number Diff line change
Expand Up @@ -39,32 +39,41 @@ class Pagination extends Arr
*/
public function calculate() -> <Pagination>
{
var items, data;
int limit, page, pages, total, previous, next;
var items, data, total;
int limit, page, pages, previous, next;

let items = this->get("data", []);
let total = this->get("total");

if typeof items != "array" && !(items instanceof Arr) {
throw new Exception("Invalid data for pagination");
}
if typeof total == "null" {
// No total number specified, we need to count items in data
let data = this->get("data", []);

if items instanceof Arr {
let data = items->all();
} else {
let data = items;
// Check if we can paginate the data
if typeof data == "object" && (data instanceof Arr) {
// Convert to array
let items = data->all();
} elseif typeof data == "array" {
let items = data;
} else {
throw new Exception("Invalid data for pagination");
}

let total = count(items);
}

let limit = (int) this->get("limit", 10),
page = (int) this->get("page", 1),
total = count(items),
pages = (int) ceil(total / intval(limit ? limit : 1));

// Make sure page is >= 1
if page <= 0 {
let page = 1;
}

this->set("items", array_slice(data, limit * (page - 1), limit));
if !this->has("items") && items {
// Set items on the current page only
this->set("items", array_slice(items, limit * (page - 1), limit));
}

if page < pages {
let next = page + 1;
Expand Down