-
Notifications
You must be signed in to change notification settings - Fork 44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add utility class/method for transactional counter++ #18
Comments
Whilst not concluded, having now added the Contention exceptions, this should be pretty straight forward! |
Hi Tom, Can you elaborate on this one? this might be useful for paginations. Thank you. |
The idea is that sometimes we need atomic incrementing counters. Maybe counting the number of posts to a forum thread. Let's say we have a count of 10. If we have 2 concurrent posts, we want to ensure that each one results in an increment, rather than both threads incrementing from 10 to 11. This can be done with transactions. This issue is here as a reminder for me to provide a utility class with all the transaction and automated retry goodness included. |
Hi Tom, Meanwhile you can provide some code this is what I am using to count all "post" tagged with the same "word". The logic was to add an integer field that will hold the count and it will increment it or decrement it depending on the operation. /**
* Alter the entity counter
*
* @param $entity_id
* @param $op | plus or minus
*/
public function alterCount($entity_id, $op) {
$entity = '';
$store = $this->getStore();
$entity = $store->fetchById($entity_id);
$counter = $entity->counter;
if ($op == 'plus') {
$counter++;
}
if ($op == 'minus') {
$counter--;
}
$entity->counter = $counter;
$store->upsert($entity);
} Now when adding a new "post" I increment the count: $this->post->writeData($data);
// Update tag count
$this->tag->alterCount($data['tag'], 'plus'); When deleting a "post" I reduce the count: $this->post->deleteEntity($args['id']);
// Update tag count
$this->tag->alterCount($data['tag'], 'minus'); Now the question is when I load the entity tag, I will know it is in use by 100 posts, if I want to paginate thru those posts 10 post at the time, how will I do that? I know from your documentation, I can do something like this: $store->query('SELECT * FROM Posts');
while($posts = $store->fetchPage(10)) {
echo "Page contains ", count($posts), " records", PHP_EOL;
}
|
I figure it out. Once again, thank you. $offset = (--$page * POST_LIMIT);
$posts = $store->fetchPage(10, $offset); |
Hi Emilorol -- I need some help. `$obj_schema_2 = new GDS\Schema('inbox'); $offset ==== it is not working when I pass a offset value here------==== This is printing (If I just pass the value as 10 without offset) all the records and not just 10. If I pass the offset, then it just doesn't work at all. but I want to display the first 10 records and then when it is passed as 11,20, it should display only from 11-20 records. |
Try it like this: $results = $obj_store_ns_2->query("SELECT * FROM inbox");
if (!is_null($results))
{
$page_results = $results->fetchPage(10, $offset);
foreach ($page_results as $page_result)
{
echo $page_result->inboxid;
}
} Let me know if it works. |
Thank you so much.... you saved me :) Your services are just awesome. On Tue, Jun 28, 2016 at 7:22 AM, Emil Orol [email protected] wrote:
|
@emilorol please help! |
Here is the code -- I am using, basically I get the input parameter as offset and itemsperpage=100 records per paging. if I have 998 as $offset it works fine, but if get as 1001, then it fails. how do I get around or modify my query? |
Hmmm, maybe we should get Tom involved because I don't know if that is a library issue or a datastore issue. Have you try using the JSON gateway? |
for some reason, if I do the following using JSON, it doesn't give me any //$obj_client = $obj_client = GDS\Gateway\GoogleAPIClient::createGoogleClient(GDS_APP_NAME, so I replaced the 2 line of code with JSON (JSON is correct though) but -V On Mon, Jul 25, 2016 at 7:33 AM, Emil Orol [email protected] wrote:
|
Is there any php code that uses Query/FetchPage using Cursors that I can On Mon, Jul 25, 2016 at 8:50 AM, Venkat [email protected] wrote:
|
Let's continue this chat in #126 |
Make it easier for people to use transactions.
Few options for syntax, which I will work on - perhaps...
$obj_thing->incrementInTransaction('count', 1);
or maybe
Utils::adjust($obj_thing, 'count', 1);
...insert more choices here!
The text was updated successfully, but these errors were encountered: