Matt C.
Well-known member
Would anyone know how to save json data into an entity? I know how to do it when there is a form, but I can't quite figure how to do it when I'm requesting data.
Here is how I'm retrieving the json data
And here is my entity.
Any help would be appreciated. Thank you.
Here is how I'm retrieving the json data
PHP:
<?php
namespace AH\DevTracker\Pub\Controller;
class DevTracker extends \XF\Pub\Controller\AbstractController
{
const DATA_URL = "https://api.kokarn.com/anthem/posts";
public function actionIndex()
{
$client = \XF::app()->http()->client();
$success = false;
try
{
$request = $client->get(self::DATA_URL);
$data = (string) $request->getBody();
$response = json_decode($data, true);
foreach($response['data'] as $entry => $entity)
{
$id = $entity['id'];
$timestamp = $entity['timestamp'];
$service = $entity['account']['service'];
$nick = $entity['account']['developer']['nick'];
$role = $entity['account']['developer']['role'];
$content = $entity['content'];
$url = $entity['url'];
}
$success = true;
}
catch (Exception $e)
{
}
$viewParams = [
'entity' => $entity,
];
return $this->view('AH\DevTracker:View', 'ah_dev_tracker_index', $viewParams);
}
public function entrySaveProcess()
{
}
}
And here is my entity.
Code:
namespace AH\DevTracker\Entity;
use XF\Mvc\Entity\Entity;
use XF\Mvc\Entity\Structure;
class Entry extends Entity
{
public static function getStructure(Structure $structure)
{
$structure->table = 'xf_ah_dev_tracker_entry';
$structure->shortName = 'AH\DevTracker:Entry';
$structure->primaryKey = 'entry_id';
$structure->columns = [
'entry_id' => ['type' => self::STR],
'timestamp' => ['type' => self::UINT],
'service' => ['type' => self::STR,
'allowedValues' => [
'twitter', 'reddit'
]
],
'nick' => ['type' => self::STR, 'maxLength' => 50],
'role' => ['type' => self::STR, 'maxLength' => 50],
'content' => ['type' => self::STR],
'url' => ['type' => self::STR]
];
return $structure;
}
}
Any help would be appreciated. Thank you.