adding integers to mysql field with cakephp [duplicate] - php

I have this field whose value i have to increment the field value by a specific value.
I am using this
$data['quantity'] = 'Order.quantity+1';
which doesnt works for me quantity is a integer coloumn here.
Also will it work when nothing is in database?.
Regards
Himanshu Sharma

I used updateAll in my code to increment views in an article. Therefore, every time an article is visited, I call the following function from within my view action in my articles controller:
function incrementViewCount($id) {
$this->updateAll(
array('Article.viewed' => 'Article.viewed+1'),
array('Article.id' => $id)
);
}
Then in your controller…
$this->MyModel->incrementViewCount(123);
Basically similar to the tutorial suggested in the previous answer.

you can use updateAll() for this
a little googling reveals this pretty quick:
http://cakephp.1045679.n5.nabble.com/Auto-Increment-A-Field-td3491697.html

You can try this code also. Works fine for simple ++/-- operations without the need of additional querying.
https://gist.github.com/denchev/8209318

Using saveField:
<?php
$this->Article->id = $id;
$this->Article->saveField('viewed', (int)$this->Article->field('viewed') + 1);
?>

Related

How to copy ACF fields from one page to another using php?

As the question states, how do I copy an ACF field from one post to another? This is what I have so far. Any help would be appreciated.
add_action('save_post', 'dupe-expiration-date');
function dupe-expiration-date()
{
// Gets expiration date from 'Homepage' page
$date_field_data = get_field('sale_expiration_date', 2361);
// Sets expration date on 'Deals' page
update_field('sale_expiration_date', $date_field_data, 34412);
}
Essentially, my code works. I don't know PHP so when I created the function dupe-expiration-date, I didn't know I couldn't use the character '-' to name the function.
Once I corrected that issue, the code worked as intended.

Google Search Console API multiple dimensions

I am having a small problem adding multiple dimensions to the API request for Search Console.
If I add the following to group by country it works fine.
$filters->setDimension("country");
$filters->setOperator("equals");
$filters->setExpression($country);
$filter->setFilters($filters);
$filter->setFilters(array($filters));
$request->setDimensionFilterGroups(array($filter));
But if I add another dimension below for device, it skips the country dimension and only runs the query with the device dimension. It's perfectly possible to run both according to this on the search console API site:
'filters': [{
'dimension': 'country',
'expression': 'ind'
}, {
'dimension': 'device',
'expression': 'MOBILE'
}]
So, any idea how I can use them both on a query?
It's probably simply some PHP code, but I can't find any documentation on multiple dimensions or any examples anywhere with PHP in mind.
It was simple.
$filters->setDimension("country");
$filters->setOperator("equals");
$filters->setExpression($country);
$filters2->setDimension("device");
$filters2->setOperator("equals");
$filters2->setExpression($device);
$filter->setFilters($filters);
$filter->setFilters(array($filters,$filters2));
$request->setDimensionFilterGroups(array($filter));
Works like a charm.
Many thanks for sharing this, Nick - it really helped!
One tiny thing: you won't need the following:
$filter->setFilters($filters);
as you're setting filters the right way with the next line of code.
Also, I was stuck on the following line:
$request->setDimensionFilterGroups(array($filter));
as I was passing the $filter object as it is instead of using an array.
I really found this counter-intuitive: this is the only object setDimensionFilterGroups method needs; I'm still wondering why it needs an array instead.
Cheers,
Sal
Nick's answer did helped me start but currently it isn't complete. To help other devs looking for an answer, hope this would help you:
$filter_group = new Google_Service_Webmasters_ApiDimensionFilterGroup;
$groups = array(['device', 'mobile', 'equals'], ['country', 'GBR', 'equals']);
$filters = array();
if($groups) {
foreach ($groups as $key => $group) {
$filters[$key] = new Google_Service_Webmasters_ApiDimensionFilter;
$filters[$key]->setDimension($group[0]);
$filters[$key]->setExpression($group[1]);
$filters[$key]->setOperator($group[2]);
}
$filter_group->setFilters($filters);
}
$request->setDimensionFilterGroups(array($filter_group));
Also take note, currently it won't work if in your filter group you have two or more same dimension but different value for filter or expression.
e.g.
$groups = array(['device', 'mobile', 'equals'], ['device', 'tablet', 'equals']);
It is because OR operation is currently not supported for filters.
Check here and this answer here.

Yii mass assignment from $_GET not working as expected

I'm trying to perform a mass assignment of 2 variables I'm sending via GET to another model::controller (from project::actionCreate to client::actionCreate)
In the _form view for project::actionCreate I've got the following:
<?php echo " ".Chtml::link('+New client',array('client/create',array('Client' => array('redir'=>Yii::app()->controller->route,'redirId'=>$model->id))));?>
With the goal of creating an array "Client" with attributes "redir" and "redirId".
In client::actionCreate I want to do something like
if(isset($_GET['Client']))
{
$model->attributes=$_GET['Client'];
}
Now I noticed that my $_GET var puts client inside subarray 0, so I've tried this with
$_GET[0]['Client']
as well, but no luck. However if I manually assign the variables like this:
$model->redir = $_GET[0]['Client']['redir'];
$model->redirId = $_GET[0]['Client']['redirId'];
Then it works.
Any idea what is up? The goal is to allow someone to create a new client while creating/updating a project record, by sending them to client::actionCreate, but redirecting them back to their original project::actionCreate if they were linked there from my "+New Client" link.
I think the client array is put inside subarray 0 because you've added an array around the parameters. Try removing the array like the following:
<?php
Chtml::link('+New client',array('client/create', 'Client' => array('redir'=>Yii::app()->controller->route,'redirId'=>$model->id)));
?>
I don't know what your model looks like but if the fields aren't assigned they are probably not safe. You can make them safe by adding them to the rules part of your model. Or you could try the following, by specifying the false parameter it will be possible to assign values to unsafe attributes. (http://www.yiiframework.com/doc/api/1.1/CModel#setAttributes-detail)
$model->setAttributes($_GET['Client'], false);
I am not sure creating a link like you want is possible. I have asked something similar some time ago Yii link with [ as a parameter I just could never get the link to how I wanted it. In the end I just created the link the old fashion way, not using CHTML.

Record the time/date of a post meta field change WordPress

So I have a post meta field that has two values, true or false. It is defaulted at false, and it'll automatically change to true when an event occurs.
Anyway, is it possible to create another meta field that records the exact time that this change occurs? If so, how would I go about doing this?
Well without modifying wordpress core or overriding the update_field() function in your theme functions.php you cant get in a smooth way the time when a record has been added to the database.
But what can i advice to you:
1) If your application highly depends on seconds:milliseconds when post_meta has been modified, you should use advanced technique: database transactions.
2) If your application are fine with a small delay between writing up 1st meta and the second just add it with add_post_meta() right below the update_field() performed
p.s If you think that overriding the function with functions.php is the way you prefer just ping me a comment, i'll try to help you with by editing the post.
Cheers!
EDIT
Right after your manipulation with allready_aired field performed
$alreadyAired = get_field('already_aired', $episodeID);
if (get_field('already_aired', $episodeID) == "no") {
update_field('field_28', "yes", $episodeID);
}
you can use add_post_meta($post_id, $meta_key, $meta_value, $unique) function in a way like this:
$alreadyAired = get_field('already_aired', $episodeID);
if (get_field('already_aired', $episodeID) == "no") {
update_field('field_28', "yes", $episodeID);
}
add_post_meta($episodeID, 'NEW_TIMESTAMP_FIELD', time(), true);
Then, you can call this NEW_TIMESTAMP_FIELD and use in a way you like (echo formatted date-string, compare 2 dates, etc.
Please note the 4th parameter of add_post_meta function. This one tell you that this field is unique and considering that app_post_meta does not affect "Update" actions, this bundle wont let this field get modified in the second time.

Magento create new product template

I really like the magento structure but finding things is very hard ;)
My problem is that I have a custom attribute. By calling ‘create new product’ this field should be prefilled with an automatic value like the entity-id. This should only happen within the create new function.
I’m absolutely not capable of finding the corresponding code, where the initial values are set, can anyone give me a hint? (a script must run, not a default value :))
Thanks a lot and grettings,
Matthias
You can find the information needed to find the corresponding code in this post:
Finding Correct Templates and Blocks in Magento
Simply change the default attribute of the field to what you need it to be.
The answer to my problem is overwritting the Mage_Adminhtml_Block_Catalog_Product_Edit_Tab_Attributes block.
Within this block, you can use a simple if condiition. the following line has to be replaced by the if:
$values[$attribute->getAttributeCode()] = $attribute->getDefaultValue();
new solution:
if($attribute->getAttributeCode() == 'my_attribute_code') {
$values[$attribute->getAttributeCode()] = SET_THE_OWN_VALUE;
} else {
$values[$attribute->getAttributeCode()] = $attribute->getDefaultValue();
}
That's all :)
Hope this helps some one else, too !!!

Categories