user133127
user133127

Reputation: 547

Is it possible to detect the field type of a MySQL field in CakePHP?

I'm planning on converting all the datetime data from a MySQL database to the format date('d-M-Y'). I'm thinking of doing it in the afterFind() callback but I'm wondering, how do detect the field type in CakePHP so I can create conditional statements?

Thanks in advance!

Upvotes: 0

Views: 1645

Answers (3)

Ben Hitchcock
Ben Hitchcock

Reputation: 1378

I think what you're after is the function getColumnType().

It's used like this:

$fieldType = $model->getColumnType($fieldName);

More info here: CakePHP Model::getColumnType API documentation

Upvotes: 0

deceze
deceze

Reputation: 522042

Off the top of my head: Model::$_schema holds the schema for the database table, including the type of the field. This is auto-populated by Cake using SQL DESCRIBE queries. You can go through your query results in an afterFind() callback and access $this->_schema to find out the type of the field. Try debug($this->_schema); to see how it's structured.

Upvotes: 2

John Boker
John Boker

Reputation: 83709

from http://php.net/manual/en/function.checkdate.php

<?php
function is_date( $str )
{
  $stamp = strtotime( $str );

  if (!is_numeric($stamp))
  {
     return FALSE;
  }
  $month = date( 'm', $stamp );
  $day   = date( 'd', $stamp );
  $year  = date( 'Y', $stamp );

  if (checkdate($month, $day, $year))
  {
     return TRUE;
  }

  return FALSE;
}
?>

Upvotes: 0

Related Questions