Jump to content
Guy Incognito

Searching a ProFields Table from within ready.php hook code

Recommended Posts

I've created a simple sports league fixture generator in a template called 'League'. Teams are added as page references then a fixture list is created as a ProFields table by hooking page save to add new rows to the table.

The bit I need help with is that I'm trying to check a fixture doesn't already exist before adding it to the table (e.g. if a new team is added to the league). I'm trying to do this with a PW selector to filter the fixtures table and check whether Team A vs Team B already exists in the table. Then on the next line checking no fixture was found by using count().

However as soon as I add the selector the script no longer adds any rows to the table. If I take it out, it all works fine (albeit with duplicate fixtures each time the page is saved). I've also tested the selector in a page template and it filters as expected. It's late here (UK)... I'm probably doing something stupid! Any ideas?

<?php

//Hook page save to generate league fixture lists
$wire->addHookAfter("Pages::saved(template=league)", function ($event) {

    //Get which page has been saved
    $page = $event->arguments(0);

    $noFixturesAdded = 0;

        //For each team in league cycle through and add home fixtures
        foreach ($page->teams_in_league as $teamA) {

            foreach ($page->teams_in_league as $teamB) {
                
                //Check if fixture already exists
                $existingFixtures = $page->fixtures("team_a=$teamA,team_b=$teamB");

                //Check team A is not the same as team B as you can't play yourself
                //Then add row to fixture table
                if ($teamB != $teamA && $existingFixtures->count() < 1 ) {
                    $fixture = $page->fixtures->makeBlankItem();
                    $fixture->team_a = $teamA->id;
                    $fixture->team_b = $teamB->id;
                    $page->fixtures->add($fixture);
                    $noFixturesAdded ++;
                }

            }

        }

        //Save updates to table
        $page->save('fixtures');

        $message = "League saved. $noFixturesAdded new fixtures were added";
        $this->message($message);
        
});

 

Share this post


Link to post
Share on other sites
3 hours ago, Guy Incognito said:

$existingFixtures = $page->fixtures("team_a=$teamA,team_b=$teamB");

I think it should be: 

$page->fixtures->find("team_a=$teamA,team_b=$teamB");

 

  • Like 2
  • Thanks 1

Share this post


Link to post
Share on other sites
6 hours ago, elabx said:

I think it should be: 


$page->fixtures->find("team_a=$teamA,team_b=$teamB");

 

Thank you - I knew I was doing something stupid... burning the midnight oil!

  • Haha 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By Macaco
      It's a bilingual site. There are two pages: "Artists" and "Events" each with a "Page Reference" field connecting each other.
      - Artists has a field where one can choose events available.
      - Events has a field where you can either choose artists available or create new ones.
      The problems: 
      - When I create an "Artist" page and select events from the list, it doesn't update the collection of participating artists on the "Event" page.
      - When I create an artist from the "Event" page. The field 'artist page > settings > language' is not "Active" for the second language.  When the artist page is created manually,"Active" is on by default.
      I know this all have to do with hooks, but I'm don't fully understand the logics.
    • By VeiJari
      Hello forum! 
      I started to write my first hook for Processwire but I'm pretty confused how you should write these. My idea is to hook after publishing in init.php (called before templates)  for a certain template. 
       
      Here's the code: 

      Trying to reset the checkbox (ajasta) and then saving it so it shows unchecked in the admin page when publishing "article" page. 
      But the code isn't doing anything. Not even dumping anything
      What seems to be the problem? And have you made a similar hook for this usage or am I doing it totally wrong? 😄
      Thanks for the support in advance!
    • By celfred
      Hello,
      I am getting nuts trying to understand hooks and I hope someone in the community will be able to help. This is deiving me crazy ! I have tested tens of possibilities to eventually reduce my code to this :
        bd('outside');   $wire->addHookAfter('Page::render', function($event) {     bd('inside');   }); And if someone could tell me why my bd('inside'); never triggers... I would be infinitely grateful !
      EDIT : Forgot to say : this piece of code is in my _init.php included in my template (but I've also tried in my site/ready.php for no better results...)
    • By NorbertH
      Is there a hook to do something right after cloning a page ?
      I want the page to be saved right after cloning it either from the button in the tree or from a lister, because saving the page triggers several calculations that are not triggered by just cloning the page.
       
      Thanks in advance !
    • By tarkvsg
      Hi!
      tell me, pls, how to execute this equality (from ready.php)
      $wire->addHookAfter('InputfieldPage::getSelectablePages', function($event) {     if($event->object->hasField == 'FIELDNAME')     {         .....     } } - $hasField The Field object associated with this Inputfield (from definition parent class for InputfieldPage)
      the left side equation is the object entity, and the right side - the string 
      How it is?
×
×
  • Create New...