2

I am developing a web application in which I am using core php and Parse.com as a database. I referred https://www.parse.com/docs/php_guide#objects-saving .to set a single value to key set() is used and it is mentioned that " Arrays and Associative Arrays require separate methods to set them on a ParseObject. "

<?php
require 'vendor/autoload.php';
	   
use Parse\ParseClient;
 
ParseClient::initialize('APP ID','REST API ID', 'MASTER ID');


use Parse\ParseObject;
use Parse\ParseQuery;
use Parse\ParseUser;
use Parse\ParseInstallation;

class ParseScripts
{
    public $object;
    public $table;
    
    
    public function __construct($parse_class=null) 
    {
        $this->object=new ParseObject($parse_class);
        $this->table=$parse_class;
    }
    
    public function setTable($table)
    {
        $o=new ParseScripts($table);
        return $o;
    }
    
    public function insert($data=  array())
    {
        
        echo "<br>data<pre><br>";
                print_r($data);
//                $k=array_keys($data);
 //               print_r($k[0]);
                
        $this->object=new ParseObject($this->table);
   //     $this->object->setAssociativeArray("$k[0]", $data);
        
        
        foreach ($data as $key => $value) 
        {
            $this->object->setAssociativeArray("$key", $value);
          //  echo "key<br>".$key; echo "<br><br>value<br>";print_r($value);
        }
      
        try 
        {
            $this->object->save();
            echo "object inserted successfully";
        } catch (ParseException $exc) 
        {
            echo $exc->getTraceAsString();
        }
           
           
    }
        
}

$ob=new ParseScripts("qt_topic");
$ob->setTable("qt_topic");
$info=  array('topic_name'=>array('Yii 6','Yii 7'));

$ob->insert($info);
?>
 

In above example if I used setArray() inside foreach it gives error that " Key must be string given array use set()" and if I used set method then it gives error " use setArray or setAssociative array to set the array. It will be better to insert array as a value instead of calling inset() more times for each value. please help me to solve this problem.

1 Answer 1

1

I know this may be a bit late but I actually ran into this problem just now.

Basically, if you have already created the ParseObject from previous saves to it, it will return a TypeError, as the type is already set on the parse end.

If at all possible, you should just delete the Parse Object from the Core Page on Parse. If you can't do this, I'd suggest dropping the column and creating a new one by looping over the column and wrapping the current values as arrays.

See here for a basic example from the Parse forums.

// Create an array to store the old values in.
$old_values_array = [];

$query = new ParseQuery("GameScore");
$query->exists("SomeColumnYouKnowIsAlwaysSet");
$results = $query->find();

// Store the values in the array by looping over them


for ($i = 0; $i < count($results); $i++) { 
    $object = $results[$i];
    array_push($old_values_array,$object->get('TheColumnYouWantToChange'));
}

Now loop over the $old_values array and reinsert it with an update

// Create the object.
for ($i = 0; $i < count($results); $i++) { 
    $object = $results[$i];
    $object->setArray('someNewColumnName', [$old_values_array[i]]);

    // Save It
    $object->save();
}

Hope this helps!

P.S. It's pretty rough, just showing you the basic logic behind it. I haven't tested this at all.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.