How to get fields (columns' names) of a table?

How to get fields (columns' names) of a table?

by v k -
Number of replies: 4

I created my own table in Moodle DB and it works fine. Say this table is almost a copy of 'quiz_attempts' and I called it 'mytable'.

How do I get list (array, object) of field names of this table?


I want smth like:

$TableFields = $DB->WhatFunction?('mytable', [list_of_needed parameters]);

//In other word I want to create a new object with all  values = NULL,

// So $TableFields is:

$TableFields->username = NULL;

$TableFields->quiz= NULL;

.......................................................

$TableFields->field_of_mytable= NULL;


I found get_indexes function, but after testing I see it doesn't meet my expectations..


Thanks!

Average of ratings: -
In reply to v k

Re: How to get fields (columns' names) of a table?

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Maybe it would be helpful to explain what it is you are trying to achieve, rather than the solution you are looking for, as that way it may be easier to propose a solution (at the moment, I'm at a loss to work out why you would want to create an object based on a table that you don't already know the structure of).

The closest I can come up with would be something like:

$obj = new stdClass();
foreach ($DB->get_columns('nameoftable') as $fieldname) {
    $obj->$fieldname = null;
}

But I still don't know why you'd want to do that ...

In reply to Davo Smith

Re: How to get fields (columns' names) of a table?

by v k -

Thank you for your help!

My goal is to create a web-service that returns fields' names and its' values by id of the record.

So, when the parameter 'id' is incorrect, I want the service to return me fields' names and NULL as values (in JSON format of course).


About get_columns: I var_dumped it and the output was not just columns headers (what I need), but the whole complicated info on the table. But I'll try it too, thank you!

In reply to v k

Re: How to get fields (columns' names) of a table?

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

This still sounds like a situation whereby you would be better off manually creating a default object:

$default = (object)['username' => null, 'firstname' => null, 'lastname' => null, ... ];

Rather than relying on tricks to automatically create the object. But, at least I now understand your use-case for what you wanted to do.


Average of ratings: Useful (1)
In reply to v k

Re: How to get fields (columns' names) of a table?

by Yousuf Tafhim -
In such case you will have to create an object and initialize all the values to be null.
e.g.
$myobject = new stdClass();
$myobject->username = NULL;
$myobject->quiz = NULL;

However, I don't understand why you would need to do something like this. If you can explain a bit more about why you actually need to do this, then hopefully you can get a better answer