I just upgraded to 1.3.3 before posting my last comment. 1.3.2 previously.
This is my table definition:
Field |
Type |
Null |
Key |
Default |
Extra |
barcode |
varchar(13) |
NO |
PRI |
|
|
meal |
varchar(20) |
NO |
PRI |
|
|
meal_date |
date |
NO |
PRI |
0000-00-00 |
|
username |
varchar(20) |
YES |
|
NULL |
|
first_name |
varchar(255) |
YES |
|
NULL |
|
last_name |
varchar(255) |
YES |
|
NULL |
|
date_entered |
timestamp |
NO |
|
CURRENT_TIMESTAMP |
on update CURRENT_TIMESTAMP |
This is my complete model:
<?php
namespace Model;
use Phalcon\Db\Column,
Phalcon\Mvc\Model\MetaData;
class Log extends \Phalcon\Mvc\Model{
/* Define the metadata so Phalcon doesn't have to get it every page load */
public function metaData(){
return[
MetaData::MODELS_ATTRIBUTES=>[
'barcode','meal','meal_date','username','first_name','last_name','date_entered'
],
MetaData::MODELS_PRIMARY_KEY=>[
'barcode','meal','meal_date'
],
MetaData::MODELS_NON_PRIMARY_KEY=>[
'username','first_name','last_name','date_entered'
],
MetaData::MODELS_NOT_NULL=>[
'barcode','meal','meal_date','date_entered'
],
MetaData::MODELS_DATA_TYPES=>[
'barcode' =>COLUMN::TYPE_VARCHAR,
'meal' =>COLUMN::TYPE_VARCHAR,
'meal_date' =>COLUMN::TYPE_DATE,
'username' =>COLUMN::TYPE_VARCHAR,
'first_name' =>COLUMN::TYPE_VARCHAR,
'last_name' =>COLUMN::TYPE_VARCHAR,
'date_entered' =>COLUMN::TYPE_DATETIME
],
MetaData::MODELS_DATA_TYPES_NUMERIC=>[],
MetaData::MODELS_IDENTITY_COLUMN=>FALSE,
MetaData::MODELS_DATA_TYPES_BIND=>[
'barcode' =>COLUMN::BIND_PARAM_STR,
'meal' =>COLUMN::BIND_PARAM_STR,
'meal_date' =>COLUMN::BIND_PARAM_STR,
'username' =>COLUMN::BIND_PARAM_STR,
'first_name' =>COLUMN::BIND_PARAM_STR,
'last_name' =>COLUMN::BIND_PARAM_STR,
'date_entered' =>COLUMN::BIND_PARAM_STR
],
MetaData::MODELS_AUTOMATIC_DEFAULT_INSERT=>[],
MetaData::MODELS_AUTOMATIC_DEFAULT_UPDATE=>[]
];
}
/* This is just a shortcut for find() */
public static function findMeal($date,$meal){
return parent::find([
'conditions'=> 'meal_date = :date: AND meal = :meal:',
'bind'=>['date'=>$date,'meal'=>$meal]
]);
}
/* This is another shortcut for findFirst() */
public static function findEntry($barcode,$date,$meal){
return parent::findFirst([
'conditions'=> 'meal_date = :date: AND meal = :meal: AND barcode = :barcode:',
'bind'=>['date'=>$date,'meal'=>$meal,'barcode'=>$barcode]
]);
}
}
?>