5

I now know that you can enter data into an entry's field by using $event->params['entry']->getContent()->handle

But let's say I have a foreach loop and I want to enter each line into an entry that is a table field. So for example:

foreach ($apigrab['properties'] as $specs) {
    $event->params['entry']->getContent()->specs == ??
}

Obviously each table heading has it's own handle and I don't imagine specs.handle would work in this case, and also how would you prevent it from just overwriting the same line over and over again? Would I have to count how many first and tell it specifically where to go?

Thanks in advance.

  • To clarify, are you saying that your "specs" field is a table field type? – Lindsey D Jul 18 '14 at 1:11
  • @LindseyD Yep that's what I mean. – Chris Jul 18 '14 at 5:01
4
+50

When saving data to a Table field, here's the catch... Your field data is being condensed into JSON and stored in a single database table cell.

So ultimately, you'll want to have your Table field data arranged in an array, then use json_encode to prep it for entry into the database.

Here's an example of data for a Table field... This is a hypothetical field containing products, with a Product Name and prices for Small, Medium, and Large...

[
  {
    "col1": "Product #1",
    "col2": "5",
    "col3": "10",
    "col4": "15"
  },
  {
    "col1": "Product #2",
    "col2": "7",
    "col3": "14",
    "col4": "21"
  },
  {
    "col1": "Product #3",
    "col2": "7",
    "col3": "14",
    "col4": "21"
  }
]

An important note... The column names you established when setting up your Table field are not relevant here. Instead, each columnn will be referred to as "col1", "col2", "col3", etc.

  • Is this still a viable solution? I'm using similare json structure but it doesn't work. – Alessio Breviglieri Oct 9 '17 at 15:38
  • Yes, this should still work. If you are having a similar issue, I recommend you ask a new question... – Lindsey D Oct 9 '17 at 23:15
  • Just a note in case anyone finds this - you do not want to use json_encode - use the actual array to set the value. – Dan Ott Mar 7 '18 at 13:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.