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.

General Questions /

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

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

[Solved] creating an archive


Reply


7 Posts   258 Views

Avatar
voodoochile

Community Member, 52 Posts

19 June 2014 at 12:03am

Hi All

i am really stuck, have spent many hours searching for answers and i really need some help.

I have a data object called quote, this has several functions that return a result on the fly, this works well.
The problem i have is 2 parts, Part 1 is creating a new record for the archive, this is triggered by a status id in the quote.
in my quote Dataobject i have the following code.

public function Createarchive()
{
   $sid = $this->getStatusID();
   
   if($sid===2)
   {
      $Archive = Archive::get()->filter(array('QuoteID' => $this->ID));
      if (!$Archive) {
         $Archive = new Archive();
         $Archive->QuoteID = $this->ID;
          $Archive->write();   
      }
      }
}

this is not working, iam unsure as to why.

Part 2 is writing the each function result into a field in the archive dataobject, in the archive dataobject there is a field for each function in the quote data object.
i am unsure asto the best way to get the function result and write it to the archive.

any help is appereciated

Thanks

Avatar
martimiz

Forum Moderator, 1106 Posts

19 June 2014 at 6:51am

Edited: 19/06/2014 6:51am

Just to make sure you didn't miss this: the following will return a DataList:

$Archive = Archive::get()->filter(array('QuoteID' => $this->ID));

To get the actual DataObject, do:

$Archive = Archive::get()->filter(array('QuoteID' => $this->ID))->first();

Have you tested if your code ever gets to the if(!$Archive) bit?

Once you get the write going, if I understand this correctly, it should be easy to do something like

$Archive->someField = $this->someFunction();

Avatar
voodoochile

Community Member, 52 Posts

20 June 2014 at 10:45pm

Hi Martimiz

Thanks for the quick reply, i finally had time to play around with this some more.
I know that it is getting the status ID as i am using that elsewhere, i had this work once when i had it in onafterwrite function but this screwed something else up.
i don't really know how to test where it is getting to but i would say that if is not getting to the if(!Archive) part wihich would indicate that the

$Archive = Archive::get()->filter(array('QuoteID' => $this->ID))->first();

is not working for some reason.
hopefully i will get time to explore this further over the weekend.

Avatar
martimiz

Forum Moderator, 1106 Posts

20 June 2014 at 11:03pm

Edited: 20/06/2014 11:04pm

I usually just place an echo "here"; within the if-statement to see if it gets there. It will appear at the top of your screen somewhere :) I'm not sure that (!$Archive) would work, as the query probabely always returns an object, be it empty...

Avatar
voodoochile

Community Member, 52 Posts

21 June 2014 at 6:44pm

Once again Thanks for your help on this i have managed to get it working, i removed the If Statement checking for the statusID and bingo if an archive doesn't exist then it will create a record for it and with a little fiddling around i have managed to get the function writing to the record.

Avatar
camfindlay

Forum Moderator, 171 Posts

22 June 2014 at 7:38pm

Sound like this is solved :) nice work, @voodoochile can you edit your original thread to include [solved] at the beginning. It helps others with similar issues know whether a thread is likely to solve their issue too.

Avatar
voodoochile

Community Member, 52 Posts

22 June 2014 at 8:07pm

@camfindlay no problem, thanks for the prompt, i will know to do this in the future.