Incrementing Cakephp database field by a value - cakephp

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

TYPO3 findDemanded return an empty array

i'm struggling with a tricky problem.
I'm creating a new extension for typo3, this extension need to retrive all the news matching the demand-array.
Actually my code looks like this:
$demand = $this->objectManager->get(NewsDemand::class);
$demand->setActionAndClass(__METHOD__, __CLASS__);
$demand->setStoragePage(18);
$res = $this->findDemanded($demand);
the main problem is that
$res->count()
this code retrieve correctly the number of news matching the array but if i try with
$res->getFirst()
this return nothing.
In theory the code $this->findDemanded($demand); will return an array with all the news matching the demand-array but the results is empty.
If i use $this->findDemandedRaw($demand); it returns the correct plain query (tested in phpmyadmin and returns the correct values).
I'm pretty new on TYPO3 and i don't know how it works really well, some one has any hints about that?
The news extension key is: news
Thanks in advance!
Fixed:
The returned value was an object not an array, so now i'm able to iterate every item inside the object and return it as a json.
My bad, sorry :)

Save data from array in variable

SOLVED!
I`m trying save data from array in variable. I have in controller:
$data = array('upload_data' => $this->upload->data());
and I know that in this array are data about uploading file. One with this date is "file_name", and I want to save this value in controller at variable. I try with:
$image_name= $data['file_name'];
But this not working. I use CodeIginter 2.1.3 framework.
Good solution is: $data['upload_data']['file_name'];
Thanks for help!
$data = $this->upload->data();
then $data['file_name'] will work. The way you're doing it you're burying an array inside another array. Pretty sure you could call it with the following but it's still pointless
$data['upload_data']['file_name']
What version of CodeIgniter are you using?
With 2.1.3 it should work - Check out the last few paragraphs of the documentation at: http://codeigniter.com/user_guide/libraries/file_uploading.html
The $data['file_name'] will give you the file name of the uploaded file if it has been successful. To get the full name/path use the other fields in the array.

CakePHP: Use Inflector Class over whole array

I need to use Inflector::slug() over all results fetched from my database, which are, of course, retrieved in an array. Is it possible somehow, or I'll need to loop each result and slugify it?
Thanks!
PHP's array_map() function might do what you need (although it assumes a simple indexed array).
array_map( 'Inflector::slug', $your_result )
If you're looking at something more complex, CakePHP's Set utility class may be helpful in a multi-step implementation.
I haven't tried this in a CakePHP context (i.e. mapping through a CakePHP class method), but I can't think of any reason it wouldn't work off the top of my head. Maybe it'll at least get you started.
Depending on the array you can use array_walk or array_walk_recursive.
Something like this should work.
This is for 5.3+;
array_walk_recursive($posts, function(&$value) {
$value = Inflector::slug($value);
});
If you wanted to limit it to a certain field you could also do something like this:
array_walk_recursive($posts, function(&$value, $key) {
if ($key == 'title') {
$value = Inflector::slug($value);
}
});
I haven't used Cake in a while but like Rob Wilkerson said, you might find that the Set class could make lighter work of this.

cakephp appmodel save question

I'm just getting familiar with cakephp (thanks to the developer before me), and ran into something funny. I have finally found out what went wrong, but still do not know why. In very pseudo code:
a controller function calls bar() twice in the same scope:
$value = 'A';
$this->foo->bar($value);
// do other stuff
$value = 'B';
$this->foo->bar($value);
bar() basically just calls cakephp's save() model to write $value to table foobar$:
$AppModel->save(array(
'AppModel'=> array('value'=>$value)
));
I expected that save() would create two rows in foobar$, however this was not the case. It first created a row with value A, then updated that row to value B. When the second call ran, it recognized the DB id generated by the previous call, decided it was the same entry and made it an update instead of insert. It sort of makes sense, but they still are separate calls, right? What obvious thing am I missing here? Thanks a lot.
After saving something to the database, Cake sets the $Model->id to the last insert id.
When saving, if there's either an id field in the data array it is supposed to save, or if there's an id set on $Model->id, Cake will update this entry instead. Both of these update the entry 42:
$Model->save(array('id' => 42, 'value' => 'foo'));
$Model->id = 42;
$Model->save(array('value' => 'foo'));
To make sure you're creating a new entry, call Model::create(), as described here.
I typically always put a model create call before a save. If the array your saving has the primary key already it will update the row, otherwise it does an insert:
Insert:
$Model->create();
$Model->save(array('value'=>'foo'));
Update
$Model->create();
$Model->save(array('id'=>1,'value'=>bar'));

Pagination by conditions in cakephp

i am having a doubt in using pagination like thing in my application..
where mytable looks like
Entry Firstname Last Name Residential Address Degree Gender Submitter
1 Aruna Chinnamuthu MDu BE Female aruna#gmail.com
2 Nisha Durgaeni MS BE Female nisha#gmail.com
3 Nathiya N Mumbai BE Female nathiya#gmail.com
In my view i am trying to bring the First entry at first and on clicking the next link in the View it must show the Entries for the second submitter and goes on ..
I have seen the pagination concepts in book.cakephp.org. But in all those they have mentioned only to paginate by using limit and order and not by using conditions.. how to do so..
It is possible ??
My submitters is in the array as
function view($formid){
$submitters=$this->Result->find('all',array('conditions'=>array('Result.form_id'=>$formid),'group'=>array('Result.submitter_id')));
foreach($submitters as $submitter)
{
echo $submitter['Result']['submitter_id'];
}
}
That is i am trying to paginate by SUbmitters of my form .. Is it possible in cakephp??Please suggest me..
I think you might be looking to use the Containable behavior. That will let you specify conditions that will apply to the model's data retrievals.
I can't quite discern the rest of your code, as the formatting didn't make it from the OP very well, but I think your conditions are limited to form_id=$formid, right? I'd suggest (before that line) dynamically attaching a Containable behavior that limits the forms to just $formid.
I'm not 100% that this is what you're after, but I might be understanding the question incorrectly.
I am unsure of the CakePHP version that you are using, but in CakePHP 1.2, the Paginate method takes a second parameter. This is the conditions array:
$this->paginate('Result', array( 'id' => 1, 'someOtherCondition' => 'condition' ) );

Resources