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.

Data Model Questions /

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

Save property to ManyManyList


Go to End


5 Posts   2108 Views

Avatar
Drumstick

Community Member, 20 Posts

13 August 2013 at 7:27am

Hi

I am a little confused. I have a CustomerproductPage, a Custom ProcuctfotoForm, a DataObject Customerproduct, a DataObjec Extension for many_many Images (Customerproduct_ProdcutphotoExtension) and another CustomerproductDetail Page for showing details, saving, editing.

Now I have a method like this on my DataObject Customerproduct

public function Productphoto() {
return $this->getManyManyComponents(
'Productphoto',
'',
"\"Customerproduct_Productphoto\".\"ID\" ASC"
);

}

Actuall, this is ok..I can receive on two pages the relation Data. But this is the problem. I just can save on one Page to the relation the Images but on the other page not. It does just show, means I get by sql query the relation data, but I can't save it

on the CustomerproductPage (custom form of the page) it works, I have this:

$productobj = DataObject::get_by_id('Customerproduct', $productID);
$productobj->Productphoto()->CustomerproductID = $productID;
$productobj->Productphoto()->ImageID = $fileID;
$arr = array($productobj->ID,$productobj->Productphoto()->ImageID = $fileID);
$productobj->Productphoto()->addMany($arr);

But on the CustomerproductDetail Page I try to do the same, it does not work.

I receive also by Debug the right Data from this manymanylist
$list = new ManyManyList('Customerproduct_Productphoto','Customerproduct', 'ID', 'CustomerproductID');

My question is now, how can I save on the second Page, the detail Page, my Data which I receive through a form?

When I try this by Debug...it says the following;

$product = Customerproduct::get()
->where("\"ID\" = '$id'");

// $list = new ManyManyList('Customerproduct_Productphoto','Customerproduct', 'ID', 'CustomerproductID');

$test = $product->Productphoto()->ImageID;

Debug::show($test);

[User Error] Uncaught Exception: Object->__call(): the method 'productphoto' does not exist on 'DataList'

Can anyone help?

Thanks a lot!

Avatar
Drumstick

Community Member, 20 Posts

13 August 2013 at 8:12am

Edited: 13/08/2013 8:13am

Now it does on both pages not work anymore. I have to remark this is for frontend. In the backend it is no problem. But the clients shall have the possibility to load images to a product.

I am not sure how the correct way is to code this manymany relations

Avatar
martimiz

Forum Moderator, 1391 Posts

13 August 2013 at 8:14pm

At a quick glance I see:

$product = Customerproduct::get() 
         ->where("\"ID\" = '$id'");

xxx::get() always returns a DataList (a set of DataObjects) even if it only contains one object. To actually get the object itself you could

$product = Customerproduct::get()->byID($id);

Avatar
Drumstick

Community Member, 20 Posts

13 August 2013 at 11:49pm

Thanks very much for your answer. That was a problem too.

I found out that the other problem is acutally not the relation. It is because in my second page, it does not save the ID of the File Image.

I have several other forms, with Ajax, without Ajax, they work, but here on his page it does not work anymore. I changed it to a has_one relation, just one picuture, but the same. It doesn't save me the ID of the attached Image. It looks all the same like other who are working. This is realy confusing.

I flushed a lot of times..I don't think it is the cache.

How can I get in another way the ID? This here works not anymore, and just with saveIntoForm works also not, I have no ID of the picture.

$up =new Upload();
$img = Object::create('Image');

$up->loadIntoFile($photo, $img, 'Kundenbilder/Kundenbilder'.$memberID);
$record = $img->ID;
$fileID = $record;

$productobj->ProductphotoID = $fileID ;
$productobj->write();

Thanks for your help!

Avatar
Drumstick

Community Member, 20 Posts

14 August 2013 at 6:38am

Hi

I am not sure...but can it be, that when I have two forms, one as a custom form, the other as an form on a page with the same name of uploadfield, that it doesn't send the record ID of the insert in the File Class?

Now I tried my custom form in the second page, means, took the same...and see now..it saves me the ID of the Photo in my Customerproduct Class.

Is there a rule or something that you can't use the same name of uploadfield in different named forms?

Tanks for your answer.