Jump to content

Recommended Posts

Posted

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.

  • Like 1
Posted

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
Posted

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.

Posted

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
Posted

Thanks Ryan,

I have been trying to avoid using SQL directly in PW - just figured there was something in the API I was missing, but it seems that in some cases it will be more efficient.

Posted

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
Posted

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
Posted

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
Posted

Hey diogo - cool idea and a great example of how easy it is to create a module. Looking forward to finding a little time to start creating my own soon.

Thanks again.

  • 5 years later...
Posted (edited)

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
  • 6 years later...
Posted

Needed the exact same thing today and came up with this solution that allows using regular page selectors:

  public function getSheetToCreatePdf(): SheetPage|NullPage
  {
    // make sure to only find pages that have been modified since creation
    wire()->addHookAfter("PageFinder::getQuery", function (HookEvent $event) {
      $query = $event->return;
      $query->where("pages.modified > pages.created");
      $event->removeHook(null);
    });

    // find the oldest page that has no PDF and make sure to flush the cache
    return wire()->pages->getFresh([
      'template' => 'sheet',
      'pdf.count' => 0,
      'has_parent' => '/papers',
      'sort' => 'id',
    ]);
  }

This is called from an endless reactPHP loop that automatically creates PDFs when new pages are modified 🙂 

  • Like 2

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
  • Recently Browsing   0 members

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