model.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php echo '<?php' ?>
  2. use Orm\Model;
  3. class Model_<?php echo $model_name; ?> extends Model
  4. {
  5. protected static $_properties = array(
  6. 'id',
  7. <?php foreach ($fields as $field): ?>
  8. '<?php echo $field['name']; ?>',
  9. <?php endforeach; ?>
  10. <?php if ($include_timestamps): ?>
  11. 'created_at',
  12. 'updated_at',
  13. <?php endif; ?>
  14. );
  15. <?php if ($include_timestamps): ?>
  16. protected static $_observers = array(
  17. 'Orm\Observer_CreatedAt' => array(
  18. 'events' => array('before_insert'),
  19. 'mysql_timestamp' => false,
  20. ),
  21. 'Orm\Observer_UpdatedAt' => array(
  22. 'events' => array('before_save'),
  23. 'mysql_timestamp' => false,
  24. ),
  25. );
  26. <?php endif; ?>
  27. public static function validate($factory)
  28. {
  29. $val = Validation::forge($factory);
  30. <?php foreach ($fields as $field): ?>
  31. <?php
  32. $rules = array('required');
  33. if (in_array($field['type'], array('varchar', 'string', 'char')))
  34. {
  35. if ($field['name'] === 'email')
  36. {
  37. $rules[] = 'valid_email';
  38. }
  39. $rules[] = ! is_null($field['constraint']) ? "max_length[{$field['constraint']}]" : 'max_length[255]';
  40. }
  41. elseif (in_array($field['type'], array('int', 'intenger')))
  42. {
  43. $rules[] = 'valid_string[numeric]';
  44. }
  45. $rules = implode('|', $rules);
  46. ?>
  47. $val->add_field('<?php echo $field['name']; ?>', '<?php echo ucwords(str_replace('_', ' ', $field['name'])); ?>', '<?php echo $rules; ?>');
  48. <?php endforeach; ?>
  49. return $val;
  50. }
  51. }