XF 2.0 Saving json data into entity

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
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.
 
I tried this:

Code:
foreach($response['data'] as $entry => $entity)
{
    $id = $entity['id'];
    $service = $entity['account']['service'];
    $timestamp = $entity['timestamp'];
    $nick = $entity['account']['developer']['nick'];
    $role = $entity['account']['developer']['role'];
    $content = $entity['content'];
    $url = $entity['url'];

    $newEntry = \XF::app()->em()->create('AH\DevTracker:Entry');
    $newEntry->entry_id = $id;
    $newEntry->service = $service;
    $newEntry->timestamp = $timestamp;
    $newEntry->nick = $nick;
    $newEntry->role = $role;
    $newEntry->content = $content;
    $newEntry->url = $url;
    $newEntry->save();
}

But I get this error message:

InvalidArgumentException: Column 'entry_id' is read only, can only be set with forceSet in src/XF/Mvc/Entity/Entity.php at line 550

Does anyone know how to fix this? Thank you.
 
I tried this:

Code:
foreach($response['data'] as $entry => $entity)
{
    $id = $entity['id'];
    $service = $entity['account']['service'];
    $timestamp = $entity['timestamp'];
    $nick = $entity['account']['developer']['nick'];
    $role = $entity['account']['developer']['role'];
    $content = $entity['content'];
    $url = $entity['url'];

    $newEntry = \XF::app()->em()->create('AH\DevTracker:Entry');
    $newEntry->entry_id = $id;
    $newEntry->service = $service;
    $newEntry->timestamp = $timestamp;
    $newEntry->nick = $nick;
    $newEntry->role = $role;
    $newEntry->content = $content;
    $newEntry->url = $url;
    $newEntry->save();
}

But I get this error message:

InvalidArgumentException: Column 'entry_id' is read only, can only be set with forceSet in src/XF/Mvc/Entity/Entity.php at line 550

Does anyone know how to fix this? Thank you.

Okay, I fixed that error, now I'm getting "Please enter a valid value.". Am I not grabbing the json values correctly?
 
Back
Top Bottom