hristonev / sportmonks-client-bundle

Easily talk to an SportMonks API in PHP
MIT License
14 stars 7 forks source link

calling fixtures pagination in foreach loop #10

Closed kashmiry closed 5 years ago

kashmiry commented 5 years ago

I am doing this call inside a foreach loop to return each competition matches. This call works just fine if it's outside a foreach loop. When in foreach I only get the first competition array but then the array is not reseted! it keeps returning the same results for the first loop, I tried to unset the variables but that does not seem to work. any idea?

foreach($comps as $comp){
    $comp_api = $comp->comp_id;
    $from_date = new datetime($start_date);
    $to_date = new datetime($end_date);
    $matches_setup = ['query' => ['leagues' => $comp_id]];

    #loop matches
    $matches = array();
    do {
        $matches = array_merge($matches, $api->fixtures()->between()->period($from_date, $to_date, null, $matches_setup));
    } while ($monk_framework->fixtures()->nextPage($matches_setup));
    $results = $matches;

    /*
    * part where data is inserted to db
    */
}

Is there something wrong with how am doing it?

hristonev commented 5 years ago

Loop needs to be do...while, instead while. Your first query must be what you want. After you can call next method.

kashmiry commented 5 years ago

@hristonev Correct I placed a wrong loop in the page, I am indeed using a do...while, I have edited the code. It works fine if not in a foreach loop. also in the foreach if I take it out of the do...while loop it works by getting only the first page of each competition. seems like an error from nextPage() when used in foreach?

hristonev commented 5 years ago

It's not array :) to iterate it in foreach. Every iterator call next method and return API callback. If you need I can place your code snippets. You need to understand OOP.

kashmiry commented 5 years ago

@hristonev Indeed my knowledge is not the best in OOP, can you please explain more how can I achieve what I am trying to do? I've been stuck at it for awhile

hristonev commented 5 years ago

What you need to achieve?

2018-08-20 22:40 GMT+03:00 Kash notifications@github.com:

@hristonev https://github.com/hristonev Indeed my knowledge is not the best in OOP, can you please explain more how can I achieve what I am trying to do? I've been stuck at it for awhile

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/hristonev/sportmonks-client-bundle/issues/10#issuecomment-414438030, or mute the thread https://github.com/notifications/unsubscribe-auth/AFivBET5MhaIeC1WOCrgGAidCasEyktuks5uSxCrgaJpZM4WEVy0 .

kashmiry commented 5 years ago

I want to fetch the fixtures of each of the competitions I have. I have them in an array like in the code above. I run the loop in foreach as shown above I wan't to understand what am doing wrong, and how to do it in a proper way. my problem is the first competitions matches are returned correctly, but when the loop moves to the second array of competitions, I have the same results as the first competition. the api keeps returning the same thing, though I want it to each time return the fixtures of the requested league.

hristonev commented 5 years ago
        $data = [];
        try{
            do{
                $data = array_merge($data, $this->client->fixtures()->between()->period(
                    $startDate,
                    $endDate
                ));
            }while($this->client->fixtures()->between()->nextPage());
        }catch (MissingParameterException $exception){
            return [
                'error' => true,
                'error_msg' => $exception->getMessage()
            ];
        }

        $this->rateLimit = $this->client->getRateLimit();
        $this->rateLimitRemaining = $this->client->getRateLimitRemaining();

        return $data;

My code for getting fixtures. You pass additional parameter league, but that can't be a problem. nextPage method do not have any parameters. nextPage only affects page parameter in query. And merges that param to your params(league filter).

kashmiry commented 5 years ago

@hristonev I took some time away from this, it really got to me.

I already configured nextPage() to accept params, review the last edit to master. my issue seems that self::$param is not being reseted when I go through another loop of league. Meaning: 1- code loops all league 1 matches with no problem. 2- code goes to league 2 starts the loop but self::$param in FixturesBetween returns the past params with league 1 and params of the last page in the previous loop.

How can I make second loop, league 2 starts with a fresh self::$param. This whole thing is because I want to loop each league matches every day to find updates and changes.

hristonev commented 5 years ago

Maybe I understand your needs... In first on my mind, there are two ways to make calls with new params. First recreate Client if is new instance there will be new static props. Other is addon method in ResourceAbstract clearQueryParams (unset all static params below query)... Its a clue but very basic. Right now I cant make tests over this bundle.

kashmiry commented 5 years ago

I was able to fix it, thank you for the hint! it helped. What worked for me is adding self::$param = array(); in FixturesBetween.php in the end It might not be the most efficient solution but it's working with me now with no issues.

This is how my FixturesBetween.php looks like

<?php
namespace SportMonks\API\Resources;

use SportMonks\API\Traits\Resource\Find;
use SportMonks\API\Traits\Resource\NextPage;
use SportMonks\API\Traits\Utility\InitTrait;

/**
 * Class FixturesBetween
 * @package SportMonks\API\Resources
 */
class FixturesBetween extends ResourceAbstract
{
    use InitTrait;

    use NextPage;

    use Find {
        find as traitFind;
    }

    /**
     * @param \DateTime $from
     * @param \DateTime $to
     * @param integer | null $teamId
     * @param array $params
     *
     * @return null | array
     */
    public function period(\DateTime $from, \DateTime $to, $teamId = null, $params = [])
    {
        if(is_null($teamId)) {
            $this->setRoute('fixtures_period', 'fixtures/between/{from}/{to}');
        }else{
            $this->setRoute('fixtures_period', 'fixtures/between/{from}/{to}/{team_id}');
        }
        $this->setRouteParameters([
            'from' => $from->format('Y-m-d'),
            'to' => $to->format('Y-m-d'),
            'team_id' => $teamId
        ]);
        $params = array_merge($params, self::$param);

        #reset params for next call
        self::$param = array();

        return $this->traitFind(null, $params, 'fixtures_period');
    }
}