0

For my custom payment module I have a database table where I need to retrieve data from, but I have issues using collections. For example I have a column named "order_id", I load the model that I have in my module and get the collection using $model->getCollection(); What's the correct way to get the data now?

When I use $collection->getData()["order_id"], I get an array with fields like ["order_id"]=> string(9) "145000021".

How can I get just the value from the table?

1 Answer 1

1

A collection returns a collection/list of all found records/items/rows. It doesn't matter if the result is only with one item. So you have to use foreach to iterate over the collection result, i.e.:

$items = $collection->getData();
foreach ($items as $item) {
    $order_id = $item->getOrderId();
    //do something with $order_id
}

or if you like to get only the first item, you can use directly this:

$order_id = $collection->getFirstItem()->getOrderId();
6
  • Yes I get that, but it return values like string(9) "145000021" instead of 145000021. Oct 3, 2015 at 17:28
  • How did you get this? By echo or var_dump or...? Please paste the var_dump result.
    – michael
    Oct 3, 2015 at 18:00
  • the var_dump result is the array of the selected row of course: array(1) { ["order_id"]=> string(9) "145000021" } Oct 3, 2015 at 18:20
  • I can't understand how exactly it returns string(9) "145000021" but you can try to cast the result, for example: (int) $order_id or (string) $order_id.
    – michael
    Oct 3, 2015 at 19:00
  • Casting doesn't work. The field in the database table isn't even a string. Oct 3, 2015 at 19:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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