LastInsertId with Parse Database - php

I'm new in Parse and I come from mysql. In mysql, it's possible to get the last id inserted in the database width $db->lastInsertId(). I don't find how to do it with Parse db, can you help me please ?
How to get the last insert id in a Parse Database ?
Thank you

With the Parse PHP SDK, everything is Object-based. This means when you create and save the Parse object in your Parse storage, you can use the getObjectId() method of the ParseObject to retrieve the object ID after saving:
$gameScore = new ParseObject("GameScore");
$gameScore->set("score", 1337);
$gameScore->set("playerName", "Sean Plott");
$gameScore->set("cheatMode", false);
try {
$gameScore->save();
echo 'New object created with objectId: ' . $gameScore->getObjectId();
} catch (ParseException $ex) {
// Execute any logic that should take place if the save fails.
// error is a ParseException object with an error code and message.
echo 'Failed to create new object, with error message: ' + $ex->getMessage();
}
From the above example, the notable method we've used is $gameScore->getObjectId(). The respective output would be:
New object created with objectId: xWMyZ4YEGZ
This is different from the lastInsertId() method because the value obtained from getObjectId() will always be the same for a particular object after it has been saved to the Parse cloud.

Related

Google Analytics PHP client is returning empty response

I'm using the Google analytics PHP client library and implemented as described like in this documentation.
https://developers.google.com/analytics/devguides/config/mgmt/v3/quickstart/service-php.
Here is my last function which used for print the result ( other functions are exact as in the document above )
function printResults($results)
{
// Parses the response from the Core Reporting API and prints
// the profile name and total sessions.
if (count($results->getRows()) > 0) {
// Get the profile name.
$profileName = $results->getProfileInfo()->getProfileName();
// Get the entry for the first entry in the first row.
$rows = $results->getRows();
$sessions = $rows[0][0];
// Print the results.
print "First view (profile) found: $profileName\n";
print "Total sessions: $sessions\n";
} else {
print "No results found.\n";
}
}
Every other things are working perfectly. Yes I tested the credentials again.
Problem is when I run the code it ends up with this error message "
ErrorException: count(): Parameter must be an array or an object that implements Countable
When i try
dd($results->getRows());
I'm expecting the analytics data to be returned. But it gives me a NULL value.
(I'm using Laravel)
What can be the reason for these null value?

PHP throwing an error trying to get property of a non-object

This is an old chestnut but I cannot see the error in my code. I'm in Windows 10, VSCode and using the Codeigniter 3 framework. I am trying to delete a record but also add an alert saying that the record has been successfully deleted. So first I use the id and get the record, save it as session data, delete the record and then put up the alert using the saved data like this 'You have successfully deleted the database entry for John Smith'. Here is my controller method:
function member_delete($id) {
$member = $this->get_where($id);
$this->session->set_userdata('firstname',$member->firstname);
$this->session->set_userdata('lastname',$member->lastname);
if($this->_delete($id)){
$firstname = $this->session->userdata('firstname');
$lastname = $this->session->userdata('lastname');
$this->session->set_flashdata('success', 'You have successfully deleted the database entry for '.$firstname.' '.$lastname);
$this->members();
} else {
$this->session->set_flashdata('failure','There has been an error and the record has not been deleted');
$this->members();
}
}
This works fine and the alert is successfully posted but then when I then refresh the page PHP throws an (Severity:notice) error 'Trying to get the property of a non-object'. The error line is the second line of the method ie $method->firstname.
I have run var_dump on the variable $member and it is definitely an object, being returned with result->row().
Can anyone tell me what is going wrong?
You're fetching $member - but then you try to access properties without knowing $member was successfully filled in the previous line
So I'd start with :
$member = $this->get_where($id);
if ( is_a($member, "someClass") )
{
$this->session->set_userdata('firstname',$member->firstname);
$this->session->set_userdata('lastname',$member->lastname);
} else
// process failure
You said the var_dump() returns an object - but does it return the object you're expecting ? Do all expected properties exist ?
Secondly, who or what is calling this function ?
If you do a refresh of the page, obviously member_delete() is called with a value of $id which shouldn't be there anymore (as you deleted it the last cycle).
So either $id is persistent for some reason, or something is invoking it with obsolete data

What is a pagination cursor & how can I use it

I am trying to retrieve a list of customers using the square PHP SDK. If I leave the $cursor blank, I can see a list of all of my customer information. How do I use the pagination cursor to receive a customer by their name? Sample code below:
<?php
require_once(__DIR__ . '/vendor/autoload.php');
// Configure OAuth2 access token for authorization: oauth2
SquareConnect\Configuration::getDefaultConfiguration()->setAccessToken('YOUR_ACCESS_TOKEN');
$api_instance = new SquareConnect\Api\CustomersApi();
// string | A pagination cursor returned by a previous call to this endpoint.
//Provide this to retrieve the next set of results for your original query.
//See [Paginating results](#paginatingresults) for more information.
$cursor = "cursor_example";
try {
$result = $api_instance->listCustomers($cursor);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling CustomersApi->listCustomers: '
, $e->getMessage(), PHP_EOL;
}
?>
The pagination cursor does not allow you to query for customers by name. If you have more than one "page" of customer, then it allows for you to get more customers by providing a token representing the next page.
If you have enough customers to fill multiple pages, a cursor would be returned in the response.

SOAP-ERROR: When using Enterprise Client for SalesForce

I am attempting to pass a custom field via a SOAP Client to SalesForce, here's my existing code:
try {
$mySforceConnection = new SforceEnterpriseClient();
$mySoapClient = $mySforceConnection->createConnection(SOAP_CLIENT_BASEDIR.'/enterprise.wsdl.xml');
$mylogin = $mySforceConnection->login($USERNAME, $PASSWORD);
$leadConvert = new stdClass;
$leadConvert->leadId = $eLEADID;
$leadConvert->Custome_field = true;
$leadConvertArray = array($leadConvert);
echo "<pre>";
print_r($leadConvertArray);
echo "</pre>";
$leadConvertResponse = $mySforceConnection->convertLead($leadConvertArray);
echo "<pre>";
print_r($leadConvertResponse);
echo "</pre>";
}
catch (Exception $e) {
echo $e->faultstring;
}
The way it's being passed to SOAP is this:
Array
(
[0] => stdClass Object
(
[leadId] => XXXXXXXXXX
[Custom_field] => 1
)
)
And my error that I am getting is as follows:
SOAP-ERROR: Encoding: object has no 'convertedStatus' property
Do I have to set something in the WSDL in order for this to work? This is to attempt to update a leadID in SalesForce to have it be converted to account/opportunity.
Is there a better way for me to debug this and see what I am missing maybe from the WSDL?
Note: the error means that you "literally" don't have the parameter set in the request. To fix it, simply pass what it's complaining about.
New Issue:
After adding the request, I am getting the following error:
Element {urn:enterprise.soap.sforce.com}Custome_field invalid at this location
Not quite sure what that error means at all. Any thoughts?
For the error:
Element {urn:enterprise.soap.sforce.com}Custome_field invalid at this location
It appears you are trying to set a custom field on the Lead record to a boolean value.
Custom fields in Salesforce will typically end with the __c suffix.
Check the API name for the field in Salesforce and ensure the user who generated and is accessing the Enterprise API has field level access to it.
Then you code should be something like:
$leadConvert->Custom_field__c = true;
It also seems a bit odd that you are setting a custom field for a Lead and then passing it to the convertLead() web method. See the LeadConvert Arguments, which include convertedStatus.

Parsing HTML Table - PHP

I am trying to parse many HTML tables, with the URLs stored in the database. The current problem with my code is that it will fail on a different table every time. Here is the part of the code that gets the error:
while ($sqlrow = mysqli_fetch_row($res)) {
echo "Started Processing Table " . $tables . PHP_EOL;
$tables++;
$data = file_get_contents($sqlrow[1]);
$dom->loadHTML($data);
$dom->preserveWhiteSpace = false;
$teamtable = $dom->getElementById("reTeamTable");
$teamrows = $teamtable->getElementsByTagName('tr');
The lines that usually fails is either the "getElementById" command or the "getElementsByTagName" command. The error I am getting is: "PHP Fatal error: Call to a member function getElementsByTagName() on a non-object in /scouting/teamlist.php on line 20". I don't understand why this is getting an error on a different URL every time.
Its means that $dom doesnt find element with id="reTeamTable" ( $teamtable is null ). Before call getElementsByTagName , check $teamtable on empty.

Categories