I can't get createdAt value form Parse(Back4App) by using PHP - parse-platform

Despite I use $object->getCreatedAt() function I cant fetch the createdAt column's values. Need help. Thanks in advance for all.
$tableFetch = new Database(); // This is my custom class structure
$subscriptions = $tableFetch->tableFetch("Subscribe");
for ($i = 0; $i < count($subscriptions); $i++) {
$object = $subscriptions[$i];
$createdAt = $object->getCreatedAt();
}
Edit: I can get the information of array but cant slice it with keys.

I found the answer.
After creating DateTime Object need to specify format as below.
$createdAt->format('Y-m-d H:i:s');

Related

Laravel sortByDesc chain with multiple chain

I am trying to sort eloquent collection using sortByDesc. It works for first sorting but chaining with more columns doesn't give proper result. Here is the query and code I am trying
$data = Divrank::where('division_id', $id)->with(['team.player1', 'team.player2'])->with('meta')->orderBy('position', 'asc')->get();
foreach($data as $k=>$t){
if ($t->meta) { // it has ranks informations
$t->totalSets = $t->meta->totalSets;
$t->totalGames = $t->meta->totalGames;
$t->points = $t->meta->points;
} else {
$t->totalSets = 0;
$t->totalGames = 0;
$t->points = 0;
}
unset($data[$k]->meta);
}
//return $data;
return $data->sortByDesc('points')->sortByDesc('totalSets')->sortByDesc('totalGames');
If I remove ->sortByDesc('totalSets')->sortByDesc('totalGames') then it shows correct formate.
Any ideas how to achieve this? Thank you.
Ok, got it, I need to do reverse sorting. $data->sortByDesc('totalGames')->sortByDesc('totalSets')->sortByDesc('points'); this works. It will sort first with games, then sets and then points..

convert Christian calendar to shamsi

i want convert date and time to persian calendar with this package :
https://github.com/hekmatinasser/verta/
this package convert to shamsi but in for loop doesnt work,
this code is run:
return new Verta($show[$i]['created_at']);
in this code show date and time to pesrian date time format
but this code doesnt work:
$ishow['message'][$i]['time'] =new Verta($show[$i]['created_at']);
in this code show with christian date time format.
my main problem is in for loop show 'created_at' with chiristian format
how can i use it?
I am interested to try the library so I tested it and it's worked with New Verta($item->created_at) or Verta::instance($item->created_at). My code is like this:
foreach ($items as $item){
$shamsi[] = Verta::instance($item->created_at);
}
dd($shamsi);
It seems your $ishow is not updated from the old form. let's try code below:
$ishow = [];
foreach ($show as $item){
$ishow['message'][]['time'] = Verta::instance($item->created_at);
}
return $ishow;
or with for loop:
$ishow = [];
for ($i = 0; $i < count($show); $i++) {
$ishow['message'][$i]['time'] =new Verta($show[$i]['created_at']);
}
return $ishow;

error foreach in View Codeigniter : Illegal string offset

Please help, i use foreach to return data from database, but it only return the last data (array always update with last data). So i modified the code but i got error : Illegal string offset in foreach.
here is my controller
foreach($dataDesa as $desa)
{
$namaDesa = json_decode($this->curl->simple_get($desa->alamat_api.'/info_desa?ID_DESA=1'));
$datakonten[$namaDesa] = array(
'proyek' =>json_decode($this->curl->simple_get($desa->alamat_api.'/proyek_pertanian')),
'nama_desa' =>json_decode($this->curl->simple_get($desa->alamat_api.'/info_desa?ID_DESA=1')),
'lelang' =>json_decode($this->curl->simple_get($desa->alamat_api.'/pelelangan'))
);
}
$datakonten['getAllDesa'] = $this->M_desa->getAllDesa($idStatus);
$nama_Desa = array();
foreach($datakonten['getAllDesa'] as $row)
{
$nama_Desa[] = $row->nama_desa;
}
$datakonten['nama_desa']=$nama_Desa;
$data['content'] = $this->load->view('public/konten/v_home',$datakonten,TRUE);
$this->load->view('public/halaman/v_home',$data);
and here is my view
$i=0;
foreach($nama_desa[$i]['proyek'] as $rows)
{
$nama = $rows->nama_proyek;
i++;
}
i've tested $nama_desa[0] and $nama_desa[1] and they have value returned (the value is "Kranon" and "Wulung") and i use the value like $Kranon['proyek'] and theres no error and returned the value that i want, but when i combined it with $nama_desa[$i]['proyek'] i got this error.
`
Please help, thanks in advance
You only push $row->nama_desa to $nama_desa. and try to get ['proyek'] from $nama_desa?
As you explained above, you getting value with this $nama_desa[1] but you trying to access this key $nama_desa[$i]['proyek'] which means its looking for $nama_desa[1]['proyek'] which is not exist.
Just do
print_r($name_desa);die;
You will find there is no key present with the name of 'proyek' on $name_desa[1] (key '1' i have only given for example. It can be any number of key)
I guess you will get your value by accessing this $nama_desa['proyek'] OR You will get idea with print result.
I have just found my solution.
I always getting an error whenever i want to pass variable that refers another value into foreach, so I modified my controller so I can pass the value into foreach in View directly. And I moved kind of "get-data-from-API" from Controller into View.
My Controller :
$datakonten['getAllDesa'] = $this->M_desa->getAllDesa($idStatus);
$data['content'] = $this->load>view('public/konten/v_home',$datakonten,TRUE);
My View :
foreach($getAllDesa as $rows)
{
$proyek = json_decode($this->curl->simple_get($rows->alamat_api_lelang));
foreach($proyek as $baris)
{
$namaProyek = $baris->nama_proyek;
?>
Nama Proyek : <?php echo $namaProyek;?></br>
<?php
}
}

Symfony2 Best practices - sorting of entities

I am creating an application in Symfony2. This is the first time I develop using a framework and one of my first projects. It is a student project.
In this project, I want my collections of entities to be sorted somewhere before reaching the view. This can be done in this way:
In getters on the entities on the many-to-one relations, with comparator methods on the many-side that is used by the usort() method in the getter on the one-side. Below I have a method that also fills in gaps in a collection of "Day" entities (in a form of diary), but the point is that it sorts the days with usort.
In User entity class:
public function getDaysWithNulls()
{
$days = $this->getDays()->toArray();
//get the first day and find out how many days have passed
usort($days, array("\Pan100\MoodLogBundle\Entity\Day", "daySorter"));
$firstEntry = $days[0];
$interval = $firstEntry->getDate()->diff(new \DateTime());
$numberOfDaysBack = $interval->d;
//create an array consisting of the number of days back
$daysToShow = array();
for ($i=0; $i < $numberOfDaysBack ; $i++) {
$date = new \DateTime();
$date->sub(new \DateInterval('P' . $i . 'D'));
$daysToShow[] = $date;
}
$daysToReturn = array();
foreach ($daysToShow as $day) {
//figure out if this day has an entity, if not set an empty Day object
$dayEntityToProcess = new \Pan100\MoodLogBundle\Entity\Day();
$dayEntityToProcess->setDate($day);
foreach ($days as $dayEntity) {
//check if there is a day entity
if($day->format('Y-m-d') == $dayEntity->getDate()->format('Y-m-d')) {
$dayEntityToProcess = $dayEntity;
}
}
$daysToReturn[] = $dayEntityToProcess;
}
//return a collection
return new \Doctrine\Common\Collections\ArrayCollection($daysToReturn);
}
usort uses this in the Day entity class:
static function daySorter($dayEntity1, $dayEntity2) {
$interval = $dayEntity1->getDate()->diff($dayEntity2->getDate());
if($interval->invert == 1) {
return +1;
}
else if ($interval->invert == 0) {
return 0;
}
else return -1;
}
My question is: is this the best practice for sorting and returning sorted collections, or should sorting happen somewhere else?
I did think this was a tedoius way of doing it. So I searched the web a bit, and read some more, and found that I can create custom repositories.
I will do it this way instead:
http://symfony.com/doc/2.1/book/doctrine.html#custom-repository-classes
EDIT: found out the sorting is better done in annotations:
/**
* #ORM\OneToMany(targetEntity="Day", mappedBy="user_id")
* #ORM\OrderBy({"date" = "DESC"})
**/
protected $days;

How to force model attribute with certain data type

There is "date_stamp" field in the database table. Field type is bigint(20).
I get this value in my code.
$dateStamp = $model->getDateStamp();
I need $dateStamp to be int or long. But It's string. How can I force it to be int or long?
As I need quick solution, I have maden new function in the model.
public function getFloatDateStamp(){
$dateStamp = (float) $this->getDateStamp();
return $dateStamp;
}

Resources