MainelyDesign.com Blog

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

Posted 3 weeks, 4 days ago by Kevin Wentworth
Viewed 132 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.

Leave a Comment

Post a Reply

*Please leave blank. Do not fill in.

Will not be posted or used in any way.

We'll link back to your site if you want.

No links or formatting allowed.

Meet Site Avenger - Hosted Content Management System

Powered By: Site Avenger | Site Production: Saco Design