Jump to content

Possible to select modified>created


adrian
 Share

Recommended Posts

Hi everyone,

Trying to simply find pages where the modified date is greater than the created date.

I would think this should work:

$pages->find("modified>created");
 

but no luck.

Thanks for any suggestions.

Link to comment
Share on other sites

Try this:

$allPages = $pages->find("anySelector");

foreach($allPages as $p) if($p->modified == $p->created) $allPages->remove($p); //I'm using == instead of <= because modified would never be smaller than created

$allPages should hold your desired results now

  • Like 2
Link to comment
Share on other sites

Thanks diogo - a nice solution - I hadn't thought about the remove option.

Only catch is that my find makes use of a limit selector. Obviously I can break the foreach loop after my limit requirement, but it will mean grabbing all the pages that match and then not using most of them in the foreach - just seems wasteful.

Link to comment
Share on other sites

Selectors have to have a defined value after the operator. There is no defined value in "modified>created", as those are two fields. You can go straight to SQL to accomplish this one:

$pageIDs = array();
$result = $db->query("SELECT id FROM pages WHERE modified>created"); 
while($row = $result->fetch_row()) $pageIDs[] = $row[0]; 
$pageArray = $pages->getById($pageIDs); // $pageArray contains what you want
  • Like 6
Link to comment
Share on other sites

You could also create a module that would hook to save and update a field holding the value of modified minus created. Then you could use $pages->find("modified-created>0");

  • Like 3
Link to comment
Share on other sites

Selectors have to have a defined value after the operator. There is no defined value in "modified>created", as those are two fields. You can go straight to SQL to accomplish this one:

$pageIDs = array();
$result = $db->query("SELECT id FROM pages WHERE modified>created"); 
while($row = $result->fetch_row()) $pageIDs[] = $row[0]; 
$pageArray = $pages->getById($pageIDs); // $pageArray contains what you want

Awesome! I had no idea it was so easy to drop straight to SQL. You continue to surprise me with practical, useful solutions!

Best,

Rick

  • Like 1
Link to comment
Share on other sites

Here is a draft of the module that I talked about above

<?php //ModifiedMinusCreated.module

class ModifiedMinusCreated extends WireData implements Module {

public static function getModuleInfo() {

    return array(
        'title' => 'Modified minus created',
        'version' => 100,
        'summary' => 'only a rough draft',
        'singular' => true,
        'autoload' => true,
    );
}

public function init() {
    $this->pages->addHookBefore('save', $this, 'subtract');
}

public function subtract($event) {
    $page = $event->arguments[0];
    if(!$page->fieldgroup->modifiedminuscreated) return;
    $page->modifiedminuscreated = $page->modified - $page->created;
}

Now you just have to create the integer field "modifiedminuscreated" and add it to the templates you want.

edited: changed the code under Soma's suggestion ;)

  • Like 2
Link to comment
Share on other sites

  • 5 years later...

Still it would be great to have options like "modified>created"  or "modified=created" in selectors . 

That would make it very comfortable. Maybe whith putting the second selector into some brackets, to mark it as selector and not value. 

  "modified={created}"

So possibly allowing sub selects in selectors. 

 

Edit: sub Selectors are already implemented ..

http://processwire.com/api/selectors/

 

Edited by NorbertH
sorry most of this is already implemented
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...