adrian Posted March 13, 2013 Posted March 13, 2013 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. 1
diogo Posted March 13, 2013 Posted March 13, 2013 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 2
adrian Posted March 13, 2013 Author Posted March 13, 2013 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.
ryan Posted March 14, 2013 Posted March 14, 2013 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 6
adrian Posted March 14, 2013 Author Posted March 14, 2013 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.
diogo Posted March 14, 2013 Posted March 14, 2013 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"); 3
DZoneRick Posted March 15, 2013 Posted March 15, 2013 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 1
diogo Posted March 15, 2013 Posted March 15, 2013 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 2
adrian Posted March 16, 2013 Author Posted March 16, 2013 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.
NorbertH Posted October 9, 2018 Posted October 9, 2018 (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 October 9, 2018 by NorbertH sorry most of this is already implemented
bernhard Posted August 5 Posted August 5 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 🙂 2
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now