Latest JAS v1.1.0
144 plugins online

Core / Data / Get / All

OK now the fun starts, getting "all" data is whole different beast altogether. It can do all that item and list can do and a hell of a lot more. It basically allows you to filter on any value of the module and more. It enables you to create a reasonably complex set of filters to get the exact data you want. Unlike with getting items and lists however you need to explicitly specify "setValue" to for the "value type" to become "set"... long story short, if it's not set explicitly, it won't be used in the filter.

In case you would like to know all backend users in a certain category:
Get all backend users in a category
PHP
$d=Data::cache("users", "view", "all");
$d->setValue("catnr", 1);    //set the category identifier
$d->setValue("backend", true);    //set the backend flag to true

if($d->get())
  while($d->next())
    print $d->v["username"];    //print out the username of all backend users in category #1


That wasn't too hard now was it? So try getting all users that were added as of the first of Jan 2016... you'll need the infamous "setOperator":
Get all new users after 2015
PHP
$d=Data::cache("users", "view", "all");
$d->setValue("added", "01-01-2016 00:00:00");    //we're assuming you still set your date time format to the default: d-m-Y H:i:s
$d->setOperator("added", ">=");    //added date time must be greater or equal than

if($d->get())
  while($d->next())
    print $d->v["username"];    //print out the username of all users that were added in or after 2016

Fairy snuff, now let's narrow it down to just 2016
Get all new users in 2016
PHP
$d=Data::cache("users", "view", "all");
$d->setValue("added", "01-01-2016 00:00:00", 0);    //set the first added date time
$d->setOperator("added", ">=", 0);    //set the first operator
$d->setValue("added", "01-01-2017 00:00:00", 1);    //set the second added date time
$d->setOperator("added", "<", 1);    //set the second operator, less than in this case

if($d->get())
  while($d->next())
    print $d->v["username"];    //print out the username of all users that were added in 2016


But wait, last time you used the entry numbers it was using OR but this is supposed to do AND? Yeah that's a fundamental difference compared to the item/list methods... you can set the "operants" and it defaults to AND. Let's first examine how the SQL looks for the example above:
(Simplified) Select query
SQL
SELECT *
FROM users
WHERE added>="01-01-2016 00:00:00" AND added<"01-01-2017 00:00:00"

For the sake of example we'll gather users that were not added in 2016, so we can use setOperant:
Get all new users not in 2016
PHP
$d=Data::cache("users", "view", "all");
$d->setValue("added", "01-01-2016 00:00:00", 0);
$d->setOperator("added", "<", 0);
$d->setOperant("added", "OR", 0);
$d->setValue("added", "01-01-2017 00:00:00", 1);
$d->setOperator("added", ">=", 1);

if($d->get())
  while($d->next())
    print $d->v["username"];    //print out the username of all users that were not added in 2016
(Simplified) Select query
SQL
SELECT *
FROM users
WHERE added<"01-01-2016 00:00:00" OR added>="01-01-2017 00:00:00"

And you can also change the operant between fields in the same entry
Get all new/updated users after 2015
PHP
$d=Data::cache("users", "view", "all");
$d->setValue("added", "01-01-2016 00:00:00");
$d->setOperator("added", ">=");
$d->setOperant("added", "OR");
$d->setValue("updated", "01-01-2016 00:00:00");
$d->setOperator("updated", ">=");

if($d->get())
  while($d->next())
    print $d->v["username"];    //print out the username of all users that were added or updated in or after 2016
(Simplified) Select query
SQL
SELECT *
FROM users
WHERE added>="01-01-2016 00:00:00" OR updated>="01-01-2016 00:00:00"

And this is where it becomes a bit tricky, since the operant is bound to a field how do you set the operant between to entries? You set the operant for the last field in the filter... last however doesn't mean the last setValue you did but the last field in the module for which its value type is set. As I said, tricky.

Anyway to give you an idea of how far you can take this, the blog plugin does a very nice job:
Show all published blog entries within some date range
PHP
$d=Data::cache("blog_entries", "view", "all");
$d->setValue("date", $from, 0);
$d->setOperator("date", ">=", 0);
$d->setOperant("date", "AND", 0);
$d->setValue("date", $till, 1);
$d->setOperator("date", "<");
$d->setOperant("date", "AND", 1);

//display published or unpublished if user has edit rights
$d->setValue("r", "00000000001", 2);
$d->setOperator("r", "&", 2); //& operator!
$d->setOperant("r", "OR", 2);
$d->setValue("published", true, 2);

if($d->get())
  while($d->next())
    print $d->v["subject"];    //print out all published entries between from and till, or also the published if the user has edit rights
(Simplified) Select query
SQL
SELECT *
FROM users
WHERE (date>="$from") AND (date<"$till") AND (r  & 1024 OR published="true")

You can take this a lot further but there are some limitation due to the rather "flat setup" of values, operators and operants. It's still just a 2 dimensional array of module fields (aka columns) and values (aka rows) which can do some trickery with operators and operants. The really complex stuff with multiple nested filters using a variety of fields can be done using filters.

Comments (0)