Hi!
I want to have a running numer for all entries for a specific blog, so
would this code be the cake way or what would be better?
Here are my Tables:
MySQL Table blogs:
id | next_entry_id | ...
Autoincr. | int-default 1 |
--------------------------------
1 | 2 | ...
2 | 1 | ...
3 | 4 | ...
Blog hasMany Entries
Because cake understands only single keys I have
the id (in an normal Model blod_id AND display would be the key
together...)
displayed_id = an autoincrement on one blog
(this shoudl be static, so when I have 4 entries for blog x and I
delete entry 3, entry 4 should stay entry 4 and not become 3, thats
why I need another column for the display_id)
MySQL Table entries:
id | blog_id | display_id | ...
Autoincr. | int | int | ...
---------------------------------------
1 | 1 | 1 | ...
2 | 3 | 1 | ...
3 | 3 | 2 | ...
4 | 3 | 3 | ...
__________________________________________________
So now to automatically set the right display_id I would add this
function to the Entries Model:
[CODE lang=PHP]
function beforeSave() {
// is it an insert?
if(!isset($this->data['Entry']['id']) {
// new entry, so get the display_id
mysql_query("lock record in blogs where blog_id = '".$this->data
['Entry']['blog_id']."'");
$result = mysql_query("select next_entry_id from blogs where
blog_id = '".$this->data['Entry']['blog_id']."'");
if(!$next_entry_id = mysql_fetch_array($result, MYSQL_NUM)) {
return false;
}
mysql_query("update next_entry_id in blogs set next_entry_id =
'".$next_entry_id+1."' where blog_id = '".$this->data['Entry']
['blog_id']."'");
mysql_query("unlock table blogs");
$this->data['Entry']['display_id'] = $next_entry_id;
}
return true;
}
[/CODE]
thx
Aurelius
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---