[SOLVED] CakePHP3 get table fields info (schema)


#1

How in the CakePHP 3 get all fields info from the table model?

For example, in the CakePHP2, method of model schema.

$fields = $this->Article->schema();

//return
$fields = [
  'id' => [
     'type' => 'integer',
     'comment' => null,
     'size' => 11,
     'length' => 10,
     'key' => 'primary'
   ],
   'name' => [
     'type' => 'string',
     'length' => 255,
     'comment' => 'Enter a name for article, please...',
     'default' => null
   ],
   /* ... other fields ...*/
];

but in CakePHP dont finded analog of this…


#2

https://api.cakephp.org/3.5/class-Cake.Database.Schema.TableSchema.html


#3

Thanks. It’s great!
https://book.cakephp.org/3.0/en/orm/schema-system.html#schema-collections

        // insert into begin of the controller file
        use Cake\Datasource\ConnectionManager;

        $db = ConnectionManager::get('default');

        // Create a schema collection.
        $collection = $db->schemaCollection();

        // Get a single table (instance of Schema\TableSchema)
        $tableSchema = $collection->describe('articles');
        
        //Get columns list from table
        $columns = $tableSchema->columns();
        
        //Empty array for fields
        $fields = [];
        
        //iterate columns
        foreach ( $columns as $column ){
            $fields[ $column ] = $tableSchema->column( $column );
        }

And now - in the $fields return:

[
	'id' => [
		'type' => 'integer',
		'length' => (int) 10,
		'unsigned' => false,
		'null' => false,
		'default' => null,
		'comment' => '',
		'autoIncrement' => true,
		'precision' => null
	],
	'name' => [
		'type' => 'string',
		'length' => (int) 255,
		'null' => false,
		'default' => null,
		'collate' => 'utf8_general_ci',
		'comment' => 'Артикль',
		'precision' => null,
		'fixed' => null
	],
	'description' => [
		'type' => 'string',
		'length' => (int) 255,
		'null' => true,
		'default' => null,
		'collate' => 'utf8_general_ci',
		'comment' => '',
		'precision' => null,
		'fixed' => null
	],
	'article_good_id' => [
		'type' => 'integer',
		'length' => (int) 11,
		'unsigned' => false,
		'null' => true,
		'default' => null,
		'comment' => 'Товарная группа артикля',
		'precision' => null,
		'autoIncrement' => null
	],
	'disabled' => [
		'type' => 'boolean',
		'length' => null,
		'null' => true,
		'default' => '0',
		'comment' => '',
		'precision' => null
	],
	'date_expired' => [
		'type' => 'date',
		'length' => null,
		'null' => true,
		'default' => null,
		'comment' => '',
		'precision' => null
	],
	'article_type_id' => [
		'type' => 'integer',
		'length' => (int) 11,
		'unsigned' => false,
		'null' => true,
		'default' => null,
		'comment' => '',
		'precision' => null,
		'autoIncrement' => null
	],
	'producer_company_id' => [
		'type' => 'integer',
		'length' => (int) 11,
		'unsigned' => false,
		'null' => true,
		'default' => null,
		'comment' => 'Производитель',
		'precision' => null,
		'autoIncrement' => null
	],
	'supplier_company_id' => [
		'type' => 'integer',
		'length' => (int) 11,
		'unsigned' => false,
		'null' => true,
		'default' => null,
		'comment' => 'Поставщик',
		'precision' => null,
		'autoIncrement' => null
	],
	'created' => [
		'type' => 'datetime',
		'length' => null,
		'null' => true,
		'default' => null,
		'comment' => '',
		'precision' => null
	],
	'updated' => [
		'type' => 'datetime',
		'length' => null,
		'null' => true,
		'default' => null,
		'comment' => '',
		'precision' => null
	],
	'created_user_id' => [
		'type' => 'integer',
		'length' => (int) 11,
		'unsigned' => false,
		'null' => true,
		'default' => null,
		'comment' => '',
		'precision' => null,
		'autoIncrement' => null
	],
	'updated_user_id' => [
		'type' => 'integer',
		'length' => (int) 11,
		'unsigned' => false,
		'null' => true,
		'default' => null,
		'comment' => '',
		'precision' => null,
		'autoIncrement' => null
	]
]

#4

You are welcome

Why woud you need this?


#5

Im developing a frontend jQuery plugin online full records control table component with REST requests and CakePHP 3 server side. with full search,inserting, updating lookup fields and more

This fields info needed for autobuil columns, types, comment.

After the first beta release, a want publicate this project on the github.com. Interest it?


#6

So you would create something like phpmyadmin… Good luck!
Yes, sure I am interested.


#7

Thanks
I’m oriented on MS Access's like desktop windowed application via web ui. (chromium/chrome browser app-mode)
You have a github account?


#8

Yes https://github.com/rrd108/


#9

Hi, Im publicated a table component projects in the github.com.
Manual and docs in progress.

This is a deep developing beta :slight_smile: with bugs and simple functionality. But main idea is present

Wiki url: https://github.com/xv1t/untable/wiki