Jump to:

3460 Posts in 1064 Topics by 739 members

Data Model Questions

SilverStripe Forums » Data Model Questions » Save property to ManyManyList

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

Page: 1
Go to End
Author Topic: 435 Views
  • Drumstick
    Avatar
    Community Member
    20 Posts

    Save property to ManyManyList Link to this post

    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!

  • Drumstick
    Avatar
    Community Member
    20 Posts

    Re: Save property to ManyManyList Link to this post

    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

  • martimiz
    Avatar
    Forum Moderator
    1086 Posts

    Re: Save property to ManyManyList Link to this post

    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);

  • Drumstick
    Avatar
    Community Member
    20 Posts

    Re: Save property to ManyManyList Link to this post

    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!

  • Drumstick
    Avatar
    Community Member
    20 Posts

    Re: Save property to ManyManyList Link to this post

    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.

    435 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.