luads / php-xbase

A simple parser for *.dbf files using PHP
MIT License
184 stars 84 forks source link

Protected Columns #130

Open JohnnyQ352 opened 3 weeks ago

JohnnyQ352 commented 3 weeks ago

I cannot read the column header or type anymore, it is protected when I use $dbf->getColumns();

I had to use Laravel ReflectionClass and do a whole loop-ta-loop to get the information. LOL

Anyway, if anyone needs a current work around here is a simple way of getting it:

$dbf = new TableReader($dir_file->getPathname()); while ($record = $dbf->nextRecord()) { $reflectionClass = new ReflectionClass($record); $properties = $reflectionClass->getProperties(); foreach($properties as $property) { $property->setAccessible(true); // Make the property accessible if it's protected or private $value = $property->getValue($record); // This gets the properties to construct the fields // This checks that value is an object and has header as a property if (is_object($value) && property_exists($value, 'header')) { // $value is an object with a 'header' property for($i=0; $i < count($value->header->columns); $i++ ){ // We do not want the info from the property named header if($value->header->columns[$i]->name != "header"){ $fieldName = strtoupper($value->header->columns[$i]->name); $value->header->columns[$i]->type } } } } }

Hope this helps others for the moment.

JohnnyQ352 commented 2 weeks ago

I found that this is not a problem.

I found that you created different way for obtaining the column data.

The way to do it is deep in your files and was not easy to find.

This is the way:

// Code to read table $dbf = new TableReader($dir_file->getPathname()); $fields = $dbf->getColumns(); foreach ($fields as $field) { echo "Field name: " . $field->getName() . "\n"; echo "Field type: " . $field->getType() . "\n"; echo "Field length: " . $field->getLength() . "\n";

/ // This is to show the data in the files fields while ($record = $dbf->nextRecord()) { $fieldName = $field->getName(); echo $record->$fieldName; } }