Welcome Guest, Not a member yet? Register   Sign In
Limited metadata from $query->field_data(); are there native options?
#1

In my model I retrieve both metadata and the results from my table
Code:
$query = $this->db->get('accounts');

$accounts_array['field_data'] = $query->field_data();
$accounts_array['result'] = $query->result_array();

The $accounts_array gets passed by the controller to my view.

I would like to use the meta data to define the maxlength of form fields and to recognize primary and foreign keys, which should be hidden. Unfortunately, the metadata is limited:
Code:
<?php
   foreach ($accounts['field_data'] as $field) {
    echo '<p>';
    foreach ($field as $property => $metavalue) {
    echo $property . ' ' . $metavalue . '</br>';
    }
   }
?>

This gives me:
  • name: id
  • type 3
  • max_length 1
  • primary_key 2
  • default
  • name account
  • type 253
  • max_length 21
  • primary_key 0
  • default
  • name ledger
  • type 253
  • max_length 6
  • primary_key 0
  • default

I was confused by the max_length values until I realized that they were the maximum length of the current values within each field. What I'm looking for is the varchar size of each text field.

I'm running the following:
-- mySQL Server version: 5.5.38
-- PHP Version: 5.5.18

Is there a native or alternative call to the mySQL table that would give me more metadata?
Reply
#2

In the MySQL and MySQLi drivers, field_data() calls
Code:
show columns from table_name

Normally, this will show the columns as they were defined, so the max_length values should be accurate. However, various options, such as the use of dynamic tables, can generate the behavior you are describing.

Since most of the valid commands to retrieve this data (for example, selecting directly from the information_schema table) get the data from the same place, it's unlikely that using another method to get the data will change the information received.

It should also be noted that the max_length values on integer types are used for display purposes by certain functions in MySQL, but aren't actually the maximum length of the given field (which is fixed, based on the type).

More than likely, given a MySQL database, the most reliable method for determining the structure of a table is probably to call
Code:
show create table table_name
. You would then have to parse the result to get the data you're looking for. In terms of performance, you're probably better off maintaining the information in your model.
Reply
#3

Thanks for reply. I agree with your final point that I'm better off maintaining an array of metadata in my model.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB