Welcome Guest, Not a member yet? Register   Sign In
How can I obtain column names for any object with Datamapper
#1

[eluser]The Mask[/eluser]
Hi,
If $param below is the name of any of my models, how can I list all of the column names in my view?

$t = new $param;
$t->get();
$this->data[ 'result' ] = $t->all;

$this->load->view( 'result_view', $this->data );

And then in my view, how can I list all of the columns relating to the model?
i.e.
foreach ( $result as $row )
{
echo ???
}

Thanks
#2

[eluser]imn.codeartist[/eluser]
is that you whole code?? can you please post your code here first ??
#3

[eluser]The Mask[/eluser]
Yes, basically that's the bulk of it.

The problem is $param could be the name of any model so the properties will be different. I'd like a way to loop through them whatever model is used.
#4

[eluser]Asinox[/eluser]
Code:
foreach($result as $row):
  echo $row->column_name;
endforeach;
#5

[eluser]The Mask[/eluser]
But because this could be for any model, you wont know what the $row->column_names are.

I need a generic way to list all columns for any model.
#6

[eluser]BrianDHall[/eluser]
Surprisingly simple, once you know its there! Check out $object->fields

Code:
$o = new YourDMZModel();

foreach ($o->fields as $key, $field)
{
   echo "Field $key is $field<br>\n";
}

That's it - it actually works before you even use get(), or after - your pick.

Fields contains a simple indexed array (starting at [0], naturally) that contains the database columns. Datamapper performs a simple limited select * upon instantiation of a new object, thus getting all the form field names and loading them into $fields for you.

Just that easy!
#7

[eluser]The Mask[/eluser]
Thanks Brian.
I knew there had to be something like that but couldn't find it.
Cheers
#8

[eluser]bonatoc[/eluser]
Thanks Brian.
#9

[eluser]WanWizard[/eluser]
[quote author="BrianDHall" date="1257979853"]Datamapper performs a simple limited select * upon instantiation of a new object, thus getting all the form field names and loading them into $fields for you.[/quote]
Or loads it from the cache file if you have activated this in your config.




Theme © iAndrew 2016 - Forum software by © MyBB