Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

How to link file uploaded with Filefield to class field "Image" ?


Go to End


7 Posts   874 Views

Avatar
Liutauras

Community Member, 9 Posts

27 February 2015 at 4:01am

Edited: 27/02/2015 4:02am

So I have managed to get the file uploader working but I do not know how to link the resulting file with a an object field "Image". I can see the file appearing on the server but I can not establish a link between the file and dataobject instance field. I have tried using uploadfield but it just crashes so I decided to stick with a filefield.

Avatar
Liutauras

Community Member, 9 Posts

27 February 2015 at 4:05am

Forgot to call write.......

Avatar
Liutauras

Community Member, 9 Posts

27 February 2015 at 5:15am

Nope still a problem,


                        if(isset($data["LIDs"])) {
                                echo $member->LinkToID->URL."|";
                                $upload = new Upload();
                                $f = new Image();
                                $f->OwnerID = $member->ID;
                                $f->setFilename("IDS_".$member->ID);
                                $upload->loadIntoFile($data["LIDs"],$f,"IDS");
                                $f->write();
                                if($upload->isError()){
                                        var_dump($up->getErrors());
                                }else{
                                        Filesystem::sync();
                                        $member->LinkToID = $f;
                                        echo $member->LinkToID->URL."|";
                                        echo $f->URL."|";
                                        echo "|";
                                        $member->write();
                                        echo $member->LinkToID->URL."|";
                                }
                        }

File uploads, It seem to link but when I try write it is not saved afterwards....

Avatar
JonoM

Community Member, 130 Posts

28 February 2015 at 10:50am

Hi Liutauras, if you just do this (for now) does the image upload with the original filename and do you get a record in the DB?

if(isset($data["LIDs"])) {
	$upload = new Upload();
	$image = new Image();
	$upload->loadIntoFile($data["LIDs"],$image,"IDS");
} else {
	echo "No File Data";
}

Avatar
Liutauras

Community Member, 9 Posts

1 March 2015 at 1:11am

The image get uploaded under files tabs in the admin panel and if I use echo $image->ID; I get the id of the file so it is stored in db too.

Avatar
Bagzli

Community Member, 71 Posts

1 March 2015 at 5:08am

This is a wild guess but have you tried image->Link?

Avatar
Liutauras

Community Member, 9 Posts

2 March 2015 at 1:39am

Just tried the ->Link, didn't work. Thanks for suggestion cause I'm of ideas on this one.