• This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn more.

XF 2.0 Cron Query

Mythotical

Well-known member
#1
So this is where I have ended up after days of trying.

What I am doing is using JSON to gather data from another site then inserting that into the database. This will happen only once, then there will be a daily check that will check for specific updates to the data and update the entry once a day.

Here is what I have for insert new entry:
PHP:
                $lastModified = str_replace("float(", "", $results['lastModified']);
                $lastModified = str_replace(")", "", $lastModified);
                $lastModified = $lastModified/1000-28800;
                $lastModified = str_replace("float(", "", $lastModified);
                $lastModified = str_replace(")", "", $lastModified);
                
                $lastModify = $this->filter($results['lastModified'], 'int');
                $battlegroup = $this->filter($results['battlegroup'], 'str');
                $side = $this->filter('side', 'str');
                $achievementPoints = $this->filter($results['achievementPoints'], 'int');

                if ($options->xenwow_dailyCheck == 0 AND empty($guildData))
                {
                    $newResults = \XF::em()->create('GoblinTimes\xenWoW:Info');
                    $db->beginTransaction();
                    $newResults->guild_lastModified = $lastModify;
                    $newResults->guild_bg = $battlegroup;
                    $newResults->guild_side = $side;
                    $newResults->guild_achievePoints = $achievementPoints;
                    $newResults->save();
                    $db->commit();
                    return $newResults;
                    
                } else {
                    if($lastModified > $guildData['guild_lastModified'] OR $results['achievementPoints'] > $guildData['guild_achievePoints'])
                    {
                        return \XF::em()->find('GoblinTimes\xenWoW:Guild', $guildData['guild_id']);
                    } else {
                        return false;
                    }
$results is the returned JSON information, $newResults is the new variable to commit to the database but not sure where I'm wrong.

Thanks
 

katsulynx

Well-known member
#3
Beside the fact that you've never initialized $db in your code, it's not necessary to call those two functions on it. Until you're actually calling save() on your entity, you're not performing any database operations.

Do your first five locs have to be this difficult? I'm pretty sure php will have a simple function to parse that format for you.
 

Mythotical

Well-known member
#5
Ok so $db is defined and I have yet to find a simple function to parse everything in one swoop. Another issue is that it will only process the first entry in the foreach loop instead of processing all of the items until the end.
 

katsulynx

Well-known member
#6
I have yet to find a simple function to parse everything in one swoop.
PHP:
$lastModified = str_replace("float(", "", $results['lastModified']);
                $lastModified = str_replace(")", "", $lastModified);
                $lastModified = $lastModified/1000-28800;
                $lastModified = str_replace("float(", "", $lastModified);
                $lastModified = str_replace(")", "", $lastModified);
I wonder a bit where this code actually comes from. Specifically, I don't see a reason to remove the "float(" ")" twice, once before and once after the math calculation, except for the case that you've printed them using "var_dump()" or similar and live under the impression that this is actually part of the value, which it is not.

Ok so $db is defined
Also I still can't see the necessity of $db being their in your code. The application will handle all database stuff for you when you call "save()" on your created entity, there's no need for you to do anything yourself other than call "save()" on the object.

Another issue is that it will only process the first entry in the foreach loop instead of processing all of the items until the end.
You haven't posted any loop in your first post, so I can't help you out with that one.
 

Mythotical

Well-known member
#7
The code I posted is the entire loop itself.

I have it working now. I have to do the lastModified math calculation twice due to how I receive the timestamp from a JSON query from a remote server.