Unable to load collection of objects into a collection of user objects

I am currently having issues loading a collection of objects into an array of objects based on my user class. Currently the error I keep getting is: "Fatal error: Call to a member function getSongName() on a non-object " How do I convert the backendless collection into an array of class objects?

Current Code:

$songs = Backendless::$Persistence->of("Songs")->find($data_query_or_relation_depth = null);
$songArray = $songs->getAsClass();
foreach ($songArray as $song)
{
    echo $song->getSongName();
}

As for the Song Class we can see that everything is set up correctly
Songs.php

<?php

namespace  juiceMusic;

class Songs
{
    private $songName;
    private $link;
    private $songLocation;
    private $trackArtwork;
    private $relatedAlbum;

    public function __construct($SongName, $Link, $SongLocation, $TrackArtwork, $RelatedAlbum)
    {
        $this->songName = $SongName;
        $this->link = $Link;
        $this->songLocation = $SongLocation;
        $this->trackArtwork = $TrackArtwork;
        if ($RelatedAlbum != null) {
            $this->relatedAlbum = $RelatedAlbum;
        }
    }

    /*   GETTERS AND SETTERS */
    public function getSongName()
    {
        return $this->songName;
    }
    public function setSongName($SongName)
    {
        $this->songName = $SongName;
    }
    public function  getLink()
    {
        return $this->link;
    }
    public function  setLink($link)
    {
        $this->link = $link;
    }
    public function setSongLocation($location)
    {
        $this->songLocation = $location;
    }
    public function getSongLocation()
    {
        return $this->songLocation;
    }

    public function getRelatedAlbum()
    {
        return $this->relatedAlbum;
    }

    public function setRelatedAlbum($relatedAlbum)
    {
        $this->relatedAlbum = $relatedAlbum;
    }

    public function getTrackArtwork()
    {
        return $this->trackArtwork;
    }

    public function setTrackArtwork($trackArtwork)
    {
        $this->trackArtwork = $trackArtwork;
    }
}

?>

Hi Zack,

I am not a PHP expert by any means. It appears the code does not find your Songs class. Could it be the “namespace” reference in the code? Have you tried debugging the SDK code to see where it fails?

Mark

In the file that was calling and creating the backendless collection I have the following uses for the namespace. Everything works fine elsewhere except for when trying to retrieve object as my custom class.

include "../../../assets/php/backendless/autoload.php";
include "../../../assets/php/classes/Songs.php";
include "../../../assets/php/classes/Album.php";
use backendless\Backendless;

use juiceMusic\Songs;
use juiceMusic\Album;

when you call “getAsClass()” the code somehow needs to figure out that it is actually the Songs class that the objects should be materialized as. How would it know to pick that specific class, especially if it belongs to a namespace?

I found a work around. I just take the data and reload into classes as if I’m recreating the objects, except this time I add the object id to the item for editing purposes.

foreach ($albumArray as $album)
{
    $albumObject = new Album($album['AlbumName']);
    $songsCount = count($album['songsInAlbum']);
    $albumObject->setObjectID($album['objectId']);
   for ($i = 0; $i < $songsCount; $i++)
    {
        $songName = $album['songsInAlbum'][$i]['SongName'];
        $songLocation = $album['songsInAlbum'][$i]['SongLocation'];
        $objectId = $album['songsInAlbum'][$i]['objectId'];

        $song = new Songs($songName, "",$songLocation,null,null);
        $song->setObjectId($objectId);
        $albumObject->addSongToAlbum($song);
    }
    /*
       $song = new Songs($song['SongName'], "",$song['SongLocation'],null,null);
       $albumObject->addSongToAlbum($song);
    */
   array_push($albums, $albumObject);
}