may be, this is one possible solution...
my projects-table kooks like this:
CREATE TABLE IF NOT EXISTS `projects` (
`id` int(10) unsigned NOT NULL auto_increment,
`year` int(10) unsigned NOT NULL default '0',
`code` int(10) unsigned NOT NULL default '0',
`fullcode` varchar(8) default NULL,
`created` datetime default NULL,
`modified` datetime default NULL,
PRIMARY KEY (`id`),
KEY `fullcode` (`fullcode`)
)
I bake m, v and c and then put this beforeSave-function into the
project-model:
function beforeSave() {
foreach ($this->_schema as $name=>$field) {
if ($name=='year') {
// set year, if empty
if (empty($this->data[$this->name]['year'])) {
$this->data[$this->name]['year'] = (int)date('Y');
}
}
if ($name=='code') {
// set new "code of the year", if empty
if (empty($this->data[$this->name]['code'])) {
$tmp = $this->find('first',
array(
'conditions'=>array('year'=>$this->data[$this-
>name]['year']),
'order'=>'code DESC'
)
);
if (isset($tmp['Project']['code'])) {
$new_code = $tmp['Project']['code'] +1; }
else {
$new_code = 1; }
$this->data[$this->name]['code'] = $new_code;
}
}
if ($name=='fullcode') {
// generate fullcode
$this->data[$this->name]['fullcode'] = sprintf("%s-%03u",
substr((string)$this->data[$this->name]['year'], -2), $this->data
[$this->name]['code']);
}
}
return true;
}
(tested on 1.2.1.8004)
If the year-field is empty, it uses the current year. If the code-
field is empty, it gets the next free "number of the year". Then it
generates your fullcode. Now the search is simple.
I would add some validations, but I think, it's a good base.
Hope that helps
RoVo
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com
For more options, visit this group at http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---
No comments:
Post a Comment