Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Generate AutoId for new Records #60

Open
wants to merge 1 commit into
base: 1.0
Choose a base branch
from

Conversation

chrissander
Copy link

Added the option to leave the id value empty when generating a new item via $db->get(). As suggested in feature request #3 there are two config options for auto_id_mode, these are autoincrement and hash.

The option 'hash' will generate a random id with 8 characters. The option 'autoincrement' will result the highest current id +1.

@coveralls
Copy link

coveralls commented May 16, 2019

Pull Request Test Coverage Report for Build 208

  • 1 of 17 (5.88%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-2.6%) to 92.308%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/Database.php 1 17 5.88%
Totals Coverage Status
Change from base Build 148: -2.6%
Covered Lines: 528
Relevant Lines: 572

💛 - Coveralls

@chrissander
Copy link
Author

chrissander commented May 16, 2019

Only problem with autoincrement is that there is a possibility, that a new item can get the same id as a previously deleted item. This will occur if the last inserted item with the current max id gets deleted, than its old id will get the maxid and will be used for the next inserted item.

In some scenarios like UserIds this could cause trouble, then the hash method should be used. As an alternative one could use a deleted flag instead of deleting the whole item.

@tsmgeek
Copy link

tsmgeek commented Apr 13, 2020

@chrissander why not instead just keep autoinc in a single file with locks etc?

You could also create an AutoInc class which could be instantiated, this would then allow for multiple autoinc sequences if needed.
ie.

$inc_evt = new AutoInc('events');
$inc_users = new AutoInc('users'); // specific counters
or even
AutoInc::init('assets'); //default

// new document based on Event counter
$doc = $db->get($inc_evt);
$doc_id = $doc->getId();

// new document based on default counter, if not defined then AutoIncException is thrown etc
$doc = $db->get(false);
$doc_id = $doc->getId();

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants