I am currently developing an application with PHP using backendless API. I need to get certain data from the table in backendless and store it into an arrays. In Android, I could achieve this using getter. But in PHP, I am confused because I am still new to it.
So this is my code which I try to get the data:
<?php
namespace Hospinet;
use backendless\Backendless;
use backendless\services\persistence\BackendlessDataQuery;
use backendless\model\BackendlesCollection;
use Hospinet\Pasien;
include "backendless/autoload.php";
include "pasien.php";
Backendless::initApp('appid', 'REST key', 'v1');
$Pasien = new Pasien();
$result_collection = Backendless::$Persistence->of("Pasien")->find($data_query_or_relation_depth = null);
echo $result_collection->Id_Pasien; //Is this right ?
?>
This is my Pasien.php:
<?php
namespace Hospinet;
class Pasien
{
private $Tgl_Lahir;
private $Tgl_Masuk;
private $Perawatan;
private $No_Kamar;
private $Nama_Pasien;
private $No_Asuransi;
private $Jenis_Asuransi;
private $Jenis_Kamar;
private $Id_RS;
private $Id_Dokter;
private $Id_Pasien;
private $Gender;
public function construct( $Tgl_Lahir, $Tgl_Masuk, $Perawatan, $No_Kamar, $Nama_Pasien, $No_Asuransi, $Jenis_Asuransi, $Jenis_Kamar, $Id_RS, $Id_Dokter, $Id_Pasien, $Gender ) {
$this->Tgl_Lahir = $Tgl_Lahir;
$this->Tgl_Masuk = $Tgl_Masuk;
$this->Perawatan = $Perawatan;
$this->No_Kamar = $No_Kamar;
$this->Nama_Pasien = $Nama_Pasien;
$this->No_Asuransi = $No_Asuransi;
$this->Jenis_Asuransi = $Jenis_Asuransi;
$this->Jenis_Kamar = $Jenis_Kamar;
$this->Id_RS = $Id_RS;
$this->Id_Dokter = $Id_Dokter;
$this->Id_Pasien = $Id_Pasien;
$this->Gender = $Gender;
}
public function getTgl_Lahir() {
return $this->Tgl_Lahir;
}
public function setTgl_Lahir( $Tgl_Lahir ) {
$this->Tgl_Lahir = $Tgl_Lahir;
}
public function getTgl_Masuk() {
return $this->Tgl_Masuk;
}
public function setTgl_Masuk( $Tgl_Masuk ) {
$this->Tgl_Masuk = $Tgl_Masuk;
}
public function getPerawatan() {
return $this->Perawatan;
}
public function setPerawatan( $Perawatan ) {
$this->Perawatan = $Perawatan;
}
public function getNo_Kamar() {
return $this->No_Kamar;
}
public function setNo_Kamar( $No_Kamar ) {
$this->No_Kamar = $No_Kamar;
}
public function getNama_Pasien() {
return $this->Nama_Pasien;
}
public function setNama_Pasien( $Nama_Pasien ) {
$this->Nama_Pasien = $Nama_Pasien;
}
public function getNo_Asuransi() {
return $this->No_Asuransi;
}
public function setNo_Asuransi( $No_Asuransi ) {
$this->No_Asuransi = $No_Asuransi;
}
public function getJenis_Asuransi() {
return $this->Jenis_Asuransi;
}
public function setJenis_Asuransi( $Jenis_Asuransi ) {
$this->Jenis_Asuransi = $Jenis_Asuransi;
}
public function getJenis_Kamar() {
return $this->Jenis_Kamar;
}
public function setJenis_Kamar( $Jenis_Kamar ) {
$this->Jenis_Kamar = $Jenis_Kamar;
}
public function getId_RS() {
return $this->Id_RS;
}
public function setId_RS( $Id_RS ) {
$this->Id_RS = $Id_RS;
}
public function getId_Dokter() {
return $this->Id_Dokter;
}
public function setId_Dokter( $Id_Dokter ) {
$this->Id_Dokter = $Id_Dokter;
}
public function getId_Pasien() {
return $this->Id_Pasien;
}
public function setId_Pasien( $Id_Pasien ) {
$this->Id_Pasien = $Id_Pasien;
}
public function getGender() {
return $this->Gender;
}
public function setGender( $Gender ) {
$this->Gender = $Gender;
}
}
Could you please explain it step-by-step how to get the data then store it into an array? Thanks
Sorry for the confusion, the ->data is not a correct method.
I just ran some tests, you can use $result_collection->getAsArray() to retrieve the results. Probably you were receiving an error with this method because you cannot use echo to print the array. You should use print_r function:
Thanks, it worked. But what I actually asked is how I could use for example the function getId_Pasien in Pasien.php to only get the Id_Pasien column from backendless (Where in Pasien.php there are SETTER and GETTER for each column name)
Well, it appears there’s a lot of issues with this. The responses can only be cast to your custom objects when the following is true:
You add Backendless::mapTableToClass( 'Pasier', Pasier::class )
Your class is not in a namespace
It will only work for findFirst, findLast, findById.
So in your case there's nothing I can advice more since your case does not fit 2 and 3.
If you like (and we'd appreciate that), you can contribute to [url=https://github.com/Backendless/PHP-SDK]our PHP SDK on GitHub[/url] and add the functionality you need - this may be not too hard, it's just some mapping are missing inside and the namespace is not handled properly. Unfortunately we don't have a PHP engineer in our team so our support for PHP is limited.
In the object you got back, you need to get the “data” element (which is an array) and then get the first element of the array (it will be an object). The “Nama_Pasien” property of that object contains the “Agustinus Kuncoro” value