PHP Classes

majkel PHP dbase class: Create and manipulate databases in DBF format

Recommend this page to a friend!
  Info   View files Documentation   View files View files (80)   DownloadInstall with Composer Download .zip   Reputation   Support forum (3)   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2022-10-21 (23 days ago) RSS 2.0 feedStarStarStarStar 61%Total: 415 This week: 3All time: 6,402 This week: 270Up
Version License PHP version Categories
majkel-dbase 1.1.6MIT/X Consortium ...5.3PHP 5, Databases, Files and Folders, P...
Description Author

This package can create and manipulate databases in dbase format.

It can perform several types of actions to manipulate database dBASE III format. Currently it can:

- Define a table adding fields of given types
- Read a table from DBF file
- Filter values of the retrieved table records
- Insert new records
- Update existing records
- Perform transactional updates
- Build a table from another table

Picture of Michal Kowalik
  Performance   Level  
Name: Michal Kowalik <contact>
Classes: 3 packages by
Country: Poland Poland
Innovation award
Innovation award
Nominee: 1x

Details

dbase

Build Status SensioLabsInsight Latest Stable Version Total Downloads Latest Unstable Version PHP Version License

Library for processing dbase tables.

Table of Contents

  1. Supported formats 1. Supported memo formats
  2. Installation
  3. Documentation 1. Reading tables 2. Inserting rows 3. Automatic type conversion 4. Record object 1. Reading data from record 2. Writing data to record 4. Record object 5. Updating tables 6. Deleting records 7. Transactions 8. Defining tables 1. Creating table from another table 9. Filters 1. Using filters 2. Writing custom filter

Supported formats

- dBASE III - dBASE III PLUS

Supported memo formats

- DBT - FPT

Installation

Composer

Using composer to install this library is strongly recommended.

composer require org.majkel/dbase

Then in your script use this line of code

require_once 'vendor/autoload.php'

Old-fashion style

Download library and place it somewhere on disk.

Then in your script use this line of code

require_once 'DBASE/LIB/DIR/autoloader.php';

Documentation

Reading tables

Table object is both array accessible and traversable. You can loop over it as collection or read specific record by it's index.

require_once 'vendor/autoload.php'

use org\majkel\dbase\Table;

$totalSum = 0;

$dbf = Table::fromFile('some/table.dbf');

foreach ($dbf as $record) {
    // returns all records includeing deleted ones
    if (!$record->isDeleted()) {
        $totalSum += $record->int_val;
    }
}

echo "Total sum is $totalSum, 5th description: {$record[4]['description']}\n";

Inserting rows

You can insert records as record object or as an associative array.

> Note that insert operation is not atomic. Use transactions to achieve integrity safety.

require_once 'vendor/autoload.php'

use org\majkel\dbase\Table;
use org\majkel\dbase\Record;

$dbf = Table::fromFile('some/table.dbf');

$record = new Record();
$record->fieldBool = true;
$record->fieldInt  = 123;
$record->fieldChar = 'some text 1';
$record->fieldMemo = 'some long text';

$dbf->insert($record);

$dbf->insert([
    'fieldBool' => false,
    'fieldInt'  => 321,
    'fieldChar' => 'some text 2',
]);

Automatic type conversion

Dbase and PHP types are automatically converted during fetching and storing of rows.

Dbase type | Type name | Possible values | PHP type -----------|-----------|-----------------|---------- C | Character | _any string_ | string D | Date | DDMMYY | DateTime L | Logical | [YTNF?] | boolean M | Memo | _any string_ | string N | Numeric | [-0-9.] | int / float

Record object

Record is basically ArrayObject. Object that can be treated as array.

Reading data from record

require_once 'vendor/autoload.php'

use org\majkel\dbase\Table;

$dbf = Table::fromFile('some/table.dbf');

// fetch first record
$record = $dbf[0];

echo "int  field: {$record->number}\n"; // returns integer
echo "bool field: {$record->boolean}\n"; // returns boolean
echo "date field: {$record->date->format('Y-m-d')}\n"; // return DateTime object
echo "text field: {$record->text}\n"; // returns string
echo "memo field: {$record->memo}\n"; // returns string (not entity id)
echo "memo field id: {$record->getMemoEntryId('memo')}\n"; // returns entity id for memo field `memo`
echo "is record deleted: {$record->isDeleted('memo')}\n"; // returns whether record is deleted

// ... or ...

echo "int  field: {$record['number']}\n"; // returns integer
echo "bool field: {$record['boolean']}\n"; // returns boolean
echo "date field: {$record['date']->format('Y-m-d')}\n"; // return DateTime object
echo "text field: {$record['text']}\n"; // returns string
echo "memo field: {$record['memo']}\n"; // returns string (not entity id)
echo "memo field id: {$record->getMemoEntryId('memo')}\n"; // returns entity id for memo field `memo`
echo "is record deleted: {$record->isDeleted('memo')}\n"; // returns whether record is deleted

// you can loop over fields in the record
foreach ($record as $fieldName => $fieldValue) {
    echo "$fieldName = $fieldValue\n";
}

Writing data to record

require_once 'vendor/autoload.php'

use org\majkel\dbase\Table;

$dbf = Table::fromFile('some/table.dbf');

// fetch first record
$record = $dbf[0];

$record->number  = 123;
$record->boolean = true;
$record->date    = new DateTime();
$record->text    = 'some text';
$record->memo    = 'some longer text';

// ... or ...

$record['number']  = 123;
$record['boolean'] = true;
$record['date']    = new DateTime();
$record['text']    = 'some text';
$record['memo']    = 'some longer text';

Updating tables

> Note that update operation is not atomic. Use transactions to achieve integrity safety.

require_once 'vendor/autoload.php'

use org\majkel\dbase\Table;

$dbf = Table::fromFile('some/table.dbf');

foreach ($dbf as $record) {
    $record->int_val += 10;
    $dbf->update($record); // header is updated everytime
}

Deleting records

> Do not use Record::setDeleted to delete records

require_once 'vendor/autoload.php'

use org\majkel\dbase\Table;

$dbf = Table::fromFile('some/table.dbf');

// delete 7th record
$dbf->delete(6);

// undelete 6th record
$dbf->markDelete(5, false);

Transactions

Transactions can prevent two processes from updating the same file.

When some process cannot acquire lock on the table exception is being thrown.

Transactions can also save you from unnecessary header updates. Header is updated at the end of transaction.

require_once 'vendor/autoload.php'

use org\majkel\dbase\Table;

$dbf = Table::fromFile('some/table.dbf');

// header is updated. Transaction flag is set
$dbf->beginTransaction();

foreach ($dbf as $record) {
    $record->int_val += 10;
    $dbf->update($record);  // header is not written
}

// duplicate last row
$dbf->insert($record); // header is not written

// header is written, transaction flag is cleared, recond count is updated
$dbf->endTransaction();

Defining tables

To construct new table use builder object.

require_once 'vendor/autoload.php'

use org\majkel\dbase\Builder;
use org\majkel\dbase\Format;
use org\majkel\dbase\Field;

$table = Builder::create()
    ->setFormatType(Format::DBASE3)
    ->addField(Field::create(Field::TYPE_CHARACTER)->setName('str')->setLength(15))
    ->addField(Field::create(Field::TYPE_LOGICAL)->setName('bool'))
    ->addField(Field::create(Field::TYPE_NUMERIC)->setName('num'))
    ->build('destination.dbf');

for ($i = 1; $i <= 3; ++$i) {
    $table->insert([
        'str' => "Str $i",
        'bool' => false,
        'num' => $i,
    ]);
}

Creating table from another table

You can create new table form existing table definition.

require_once 'vendor/autoload.php'

use org\majkel\dbase\Builder;
use org\majkel\dbase\Format;
use org\majkel\dbase\Field;

$table = Builder::fromFile('source.dbf')
    ->setFormatType(Format::DBASE3)
    ->addField(Field::create(Field::TYPE_NUMERIC)->setName('newField1'))
    ->build('destination.dbf');

for ($i = 1; $i <= 3; ++$i) {
    $table->insert([
        'oldField1' => "Str $i",
        'oldField2' => false,
        'newField1' => $i,
    ]);
}

Filters

Although values are automatically converted based on column type sometimes it is necessary to perform additional processing. To achieve that you can add filters on columns.

Using filters

require_once 'vendor/autoload.php'

use org\majkel\dbase\Builder;
use org\majkel\dbase\filter\TrimFilter;
use your\CurrencyFilter;

$dbf = Table::fromFile('some/table.dbf');
$dbf->getHeader()->getField('price')
    ->addFilter(new TrimFilter())
    ->addFilter(new CurrencyFilter(',', '.'));

foreach ($dbf as $record) {
    // ...
}

Filters are applied during loading in the order they are defined. During serialization filters are applied in reversed order.

Writing custom filter

require_once 'vendor/autoload.php'

use org\majkel\dbase\FilterInterface;
use org\majkel\dbase\Field;

class CurrencyFilter extends FilterInterface
{
    / @var string */
    private $inputDot;
    / @var string */
    private $outputDot;

    /
     * @param string $inputDot
     * @param string $outputDot
     */
    public function __construct($inputDot, $outputDot)
    {
        $this->inputDot = $inputDot;
        $this->outputDot = $outputDot;
    }

    /
     * From table value to PHP value
     *
     * @param mixed $value
     * @return mixed
     */
    public function toValue($value)
    {
        return str_replace($this->inputDot, $this->outputDot, $value);
    }

    /
     * From PHP value to table value
     *
     * @param mixed $value
     * @return mixed
     */
    public function fromValue($value)
    {
        return str_replace($this->outputDot, $this->inputDot, $value);
    }

    /
     * Filter can be applied on string like columns
     *
     * @param integer $type
     * @return boolean
     */
    public function supportsType($type)
    {
        return in_aray($type, [Field::TYPE_CHARACTER, Field::TYPE_MEMO]);
    }
}
  Files folder image Files  
File Role Description
Files folder imagebenchmarks (3 files)
Files folder imagebuild (3 files)
Files folder imagesrc (17 files, 4 directories)
Files folder imagestyle (1 file)
Files folder imagetests (3 directories)
Accessible without login Plain text file .travis.yml Data Auxiliary data
Accessible without login Plain text file autoloader.php Aux. Auxiliary script
Accessible without login Plain text file bootstrap.php Aux. Auxiliary script
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file DEVELOP.md Data Auxiliary data
Accessible without login Plain text file dumpContent.php Aux. Auxiliary script
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file phpunit.xml Data Auxiliary data
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files  /  benchmarks  
File Role Description
  Plain text file SequentialReadBenchmark.php Class Class source
  Plain text file TransactionalInsertBenchmark.php Class Class source
  Plain text file TransactionalUpdateBenchmark.php Class Class source

  Files folder image Files  /  build  
File Role Description
  Accessible without login Plain text file run_benchmarks.sh Data Auxiliary data
  Accessible without login Plain text file run_phpcs.sh Data Auxiliary data
  Accessible without login Plain text file travis.phpunit.xml Data Auxiliary data

  Files folder image Files  /  src  
File Role Description
Files folder imagefield (5 files)
Files folder imagefilter (2 files)
Files folder imageformat (2 files)
Files folder imagememo (4 files)
  Plain text file Builder.php Class Class source
  Plain text file Exception.php Class Class source
  Plain text file ExtCaseInsensitiveGenerator.php Class Class source
  Plain text file Field.php Class Class source
  Plain text file File.php Class Class source
  Plain text file FileFixed.php Class Class source
  Plain text file Filter.php Class Class source
  Plain text file FilterInterface.php Class Class source
  Plain text file Flags.php Class Class source
  Plain text file Format.php Class Class source
  Plain text file FormatFactory.php Class Class source
  Plain text file Header.php Class Class source
  Plain text file HeaderInterface.php Class Class source
  Plain text file MemoFactory.php Class Class source
  Plain text file Record.php Class Class source
  Plain text file Table.php Class Class source
  Plain text file Utils.php Class Class source

  Files folder image Files  /  src  /  field  
File Role Description
  Plain text file CharacterField.php Class Class source
  Plain text file DateField.php Class Class source
  Plain text file LogicalField.php Class Class source
  Plain text file MemoField.php Class Class source
  Plain text file NumericField.php Class Class source

  Files folder image Files  /  src  /  filter  
File Role Description
  Plain text file CharsetFilter.php Class Class source
  Plain text file TrimFilter.php Class Class source

  Files folder image Files  /  src  /  format  
File Role Description
  Plain text file DBase3.php Class Class source
  Plain text file FoxPro.php Class Class source

  Files folder image Files  /  src  /  memo  
File Role Description
  Plain text file AbstractMemo.php Class Class source
  Plain text file DbtMemo.php Class Class source
  Plain text file FptMemo.php Class Class source
  Plain text file MemoInterface.php Class Class source

  Files folder image Files  /  style  
File Role Description
  Accessible without login Plain text file ruleset.xml Data Auxiliary data

  Files folder image Files  /  tests  
File Role Description
Files folder imageintegration (4 files)
Files folder imageunit (13 files, 4 directories)
Files folder imageutils (5 files)

  Files folder image Files  /  tests  /  integration  
File Role Description
  Plain text file BuilderIntegrationTest.php Class Class source
  Plain text file DBase3Test.php Class Class source
  Plain text file Issue13Test.php Class Class source
  Plain text file Issue6Test.php Class Class source

  Files folder image Files  /  tests  /  unit  
File Role Description
Files folder imagefield (5 files)
Files folder imagefilter (2 files)
Files folder imageformat (2 files)
Files folder imagememo (3 files)
  Plain text file BuilderTest.php Class Class source
  Plain text file ExtCaseInsensitiveGeneratorTest.php Class Class source
  Plain text file FieldTest.php Class Class source
  Plain text file FileTest.php Class Class source
  Plain text file FilterTest.php Class Class source
  Plain text file FlagsTest.php Class Class source
  Plain text file FormatFactoryTest.php Class Class source
  Plain text file FormatTest.php Class Class source
  Plain text file HeaderTest.php Class Class source
  Plain text file MemoFactoryTest.php Class Class source
  Plain text file RecordTest.php Class Class source
  Plain text file TableTest.php Class Class source
  Plain text file UtilsTest.php Class Class source

  Files folder image Files  /  tests  /  unit  /  field  
File Role Description
  Plain text file CharacterFieldTest.php Class Class source
  Plain text file DateFieldTest.php Class Class source
  Plain text file LogicalFieldTest.php Class Class source
  Plain text file MemoFieldTest.php Class Class source
  Plain text file NumericFieldTest.php Class Class source

  Files folder image Files  /  tests  /  unit  /  filter  
File Role Description
  Plain text file CharsetFilterTest.php Class Class source
  Plain text file TrimFilterTest.php Class Class source

  Files folder image Files  /  tests  /  unit  /  format  
File Role Description
  Plain text file DBase3Test.php Class Class source
  Plain text file FoxProTest.php Class Class source

  Files folder image Files  /  tests  /  unit  /  memo  
File Role Description
  Plain text file AbstractMemoTest.php Class Class source
  Plain text file DbtMemoTest.php Class Class source
  Plain text file FptMemoTest.php Class Class source

  Files folder image Files  /  tests  /  utils  
File Role Description
  Plain text file AbstractFieldTest.php Class Class source
  Plain text file AbstractFilterTest.php Class Class source
  Plain text file AbstractFormatTest.php Class Class source
  Plain text file SplFileObjectMock.php Class Class source
  Plain text file TestBase.php Class Class source

 Version Control Unique User Downloads Download Rankings  
 100%
Total:415
This week:3
All time:6,402
This week:270Up
 User Ratings  
 
 All time
Utility:91%StarStarStarStarStar
Consistency:83%StarStarStarStarStar
Documentation:75%StarStarStarStar
Examples:-
Tests:-
Videos:-
Overall:61%StarStarStarStar
Rank:1200
  
For more information send a message to info at phpclasses dot org.