MainelyDesign.com Blog

CakePHP 3 - Getting List of Column Definitions from a Table (like schema())

Posted on 12/26/2018 at 02:48 pm by Kevin Wentworth
Viewed 11,535 times | 0 comments

You may come from a time when using $this->ModelName->schema() would give you the details of a table's column defintions.  Not in CakePHP 3.  I found this manual way of achieving the same thing:

  1. // insert into begin of the controller file
  2.         use Cake\Datasource\ConnectionManager;
  3.  
  4.         $db = ConnectionManager::get('default');
  5.  
  6.         // Create a schema collection.
  7.         $collection = $db->schemaCollection();
  8.  
  9.         // Get a single table (instance of Schema\TableSchema)
  10.         $tableSchema = $collection->describe('articles');
  11.        
  12.         //Get columns list from table
  13.         $columns = $tableSchema->columns();
  14.        
  15.         //Empty array for fields
  16.         $fields = [];
  17.        
  18.         //iterate columns
  19.         foreach ( $columns as $column ){
  20.             $fields[ $column ] = $tableSchema->column( $column );
  21.         }
Bookmark and Share

Tags for CakePHP 3 - Getting List of Column Definitions from a Table (like schema())

Cakephp | Cake3

Comments for this Posting

No comments. Be the first to post a reply.

Sorry, comments are closed for this posting.

Please Email Kevin if you have any questions. Thanks!

Meet Site Avenger - Hosted Content Management System

Powered By: Site Avenger | Site Production: Saco Design