iraira
iraira

Reputation: 325

Silverstripe 3.2: Get ID from certain Folder

I want to get the ID of a certain folder to pass it to another function.

Here is my code:

public function getFolderParentID() {
    $folderID = File::get()
            ->filter(array(
                'Filename' => 'assets/myfolder/folder/',
             ))
             ->limit(1);

    return $folderID->ID;
}

This does not return anything.

I have also tried $folderID = Folder::get() but this does not work either.

How do I get the ID of a folder by it's path name?

Upvotes: 2

Views: 162

Answers (1)

3dgoo
3dgoo

Reputation: 15794

Call first() instead of limit(1) like so:

public function getFolderParentID() {
    $folder = File::get()
            ->filter(array(
                'Filename' => 'assets/myfolder/folder/',
             ))->first();

    if ($folder) {
        return $folder->ID;
    }

    return false;
}

The reason the original code was not returning anything is because File::get()->limit(1) will return a DataList of File objects, not a single File object. We need to get the File object out of the list to then ask for it's ID. File::get()->first() will return the File object.

Upvotes: 2

Related Questions