CakePHP 2.x : How to get last insert single field in controller? - cakephp

I have a tansaction table and have a field called balance. Here I am trying to update this field with new data but insert in new transaction. So I need balance field in controller. So, I have tried below code.
$client_id = $this->request->data['Transaction']['user_id'];
$new = $this->Transaction->find('first', array(
'conditions'=>array('Transaction.user_id'=> $client_id),
'fields'=>array('Transaction.balance')
));
$this->request->data['Transaction']['balance'] =$this->request->data['Transaction']['debit_money']-$this->request->data['Transaction']['credit_money']+new;
But here I am not getting old balance data.

$new is an associative array. You are adding an array to a number.
Try the following:
$client_id = $this->request->data['Transaction']['user_id'];
$new = $this->Transaction->find('first', array(
'conditions'=>array('Transaction.user_id'=> $client_id),
'fields'=>array('Transaction.balance')
));
$this->request->data['Transaction']['balance'] =
$this->request->data['Transaction']['debit_money']
- $this->request->data['Transaction']['credit_money']
+ $new['Transaction']['balance'];
Alternatively, this should also work:
$client_id = $this->request->data['Transaction']['user_id'];
$this->request->data['Transaction']['balance'] =
$this->request->data['Transaction']['debit_money']
- $this->request->data['Transaction']['credit_money']
+ $this->Transaction->field('balance',array('Transaction.user_id'=> $client_id),
));

Related

Cakephp 3 : How to get max amout row from a table

I have table call users , like
id name amount created
1 a 100 6-16-2016
2 b 200 5-16-2016
I need max amount full row, I have tried below code but getting syntax error.
$user = $this->Users->find('all',[
'fields' => array('MAX(Users.amount) AS amount'),
]);
simplest way
$user = $this->Users->find('all',[
'fields' => array('amount' => 'MAX(Users.id)'),
]);
using select instead of an options array
$user = $this->Users->find()
->select(['amount' => 'MAX(Users.id)']);
making use of cake SQL functions
$query = $this->Users->find();
$user = $query
->select(['amount' => $query->func()->max('Users.id')]);
the above three all give the same results
if you want to have a single record you have to call ->first() on the query object:
$user = $user->first();
$amount = $user->amount;
Simplest method using CakePHP 3:
$this->Model->find('all')->select('amount')->hydrate(false)->max('amount')
Will result in an array containing the maximum amount in that table column.
Is is better to disable hydration before getting the results, to get the results as an array, instead of entity. Below is a complete example of how to get the results in an array, with distinct steps:
//create query
$query = $this->Users->find('all',[
'fields' => array('amount' => 'MAX(Users.id)'),
]);
$query->enableHydration(false); // Results as arrays instead of entities
$results = $query->all(); // Get ResultSet that contains array data.
$maxAmount = $results->toList(); // Once we have a result set we can get all the rows

updating data without model with classRegistry

I have a table called user_tasks. I do not have a model for that table and in my totally unrelated controller I have a need to update a single field in the user_tasks table.
This is what I have tried so far
$user = ClassRegistry::init('user_tasks');
$conditions = "user_id = ". $user_id;
$userRows = $user->find('first', array('conditions'=>$conditions));
$user->set(array(
'task' => $level
));
$user->save();
However it does not like $user->save(); and my table never gets updated. What can I do to make it work.
thanks
UPDATE:
In my try / catch block,
I get this
{"errorInfo":["HY000",1364,"Field 'user_id' doesn't have a default value"],"queryString":"INSERT INTO `mytable`.`user_tasks` (`subscription`, `modified`, `created`) VALUES ('Latest', '2015-04-09 15:08:51', '2015-04-09 15:08:51')"}
ok I figured it out. I have to send user id as well
so
$user = ClassRegistry::init('user_tasks');
$conditions = "user_id = ". $user_id;
$userRows = $user->find('first', array('conditions'=>$conditions));
$user->set(array(
'task' => $level,
'user_id' => $user_id
));
$user->save();
The above will create a new record. If you are updating a record then just pass a primary key. And if you are passing the primary key, then you do not need user_id etc. You just need the fields in the set that need to be updated.

Update except row of null in CakePHP

I have problem,when I tried to update except row of null in CakePHP.
Model -> Spot
column -> name,address
[MySQL]
id / name / address
1 / Shibuya Bakery / Shibuya street 123
For example,there is database like the one above.
Then CakePHP got a name-value(Shibuya Cake Shop) from Android,and address-value is null.
Therefore I wanna update just name-column.
id = 1(post from Android)
public function update()
{
if( isset($this->request->data["id"]) )
{
$id = intval($this->request->data["id"]);
$fields = array( 'Spot.id' => $id );
$data = array( 'Spot.name' => "'".$this->request->data["name"]."'",
"Spot.address" => "'".$this->request->data["address"]."'");
$this->Spot->updateAll( $data,$fields );
}
}
If you just want to update just one field, just use saveField.
$this->Spot->id = $this->request->data['id'];
$this->Spot->saveField('name', $this->request->data['name']);
Don't use updateAll, as that is meant for updating multiple records, though technically you could add conditions to prevent it from doing so. You're also passing incorrect arguments into updateAll. (See http://book.cakephp.org/2.0/en/models/saving-your-data.html#model-updateall-array-fields-mixed-conditions for the correct way.) If you want to update multiple fields at once on a single record, use save:
public function update()
{
if( isset($this->request->data["id"]) )
{
$fields = array('name');
if(!empty($this->request->data['address'])) array_push($fields, 'address');
$this->Spot->save($this->request->data, true, $fields);
}
}
Read more on saving: http://book.cakephp.org/2.0/en/models/saving-your-data.html#saving-your-data

Saving multiple rows in a single query

Is there anyway to have cake do a multi-row insert in a single query without writing raw SQL to do this? The saveMany and saveAssociated options will only save multiple rows in a single transaction, but that transaction contains multiple insert statements so these methods are clearly not a solution to write heavy applications.
Thanks for reading.
Yes
Though it's not a common practice to do so in app-land code, and doing so removes the possibility to use almost any application logic (validation rules, behaviors, events etc.). You can see an example of doing this in the way fixtures are loaded:
$db = ConnectionManager::getDataSource('default');
$table = "stuffs";
$fields = array('id', 'name');
$values = array(
array(1, 'one'),
array(2, 'two'),
...
);
$result = $db->insertMulti($table, $fields, $values);
You may also find this repository useful (either directly or as a basis for your code) which loads fixture files into your app database - using multi-inserts.
Yes, Big_Data is good idea for inserting bulk. But as AD7six note, it still use basic value quoting and does not return insert ids. And base on your ideas, i wrote small script to inserting bulk in a single query, using default CakePHP quoting and returning ids of inserted records.
$count = count($records);
$dbSource = $this->getDataSource();
$table = $dbSource->fullTableName($this->table);
$fields = $dbSource->prepareFields($this, array('fields' => array_keys($records[0])));
$values = array();
foreach ($records as $index => $record) {
if (!is_array($record) || !$record) {
return null;
}
foreach ($record as $column => $value) {
$values[$index][$column] = $dbSource->value($value, $this->getColumnType($column));
}
$values[$index] = '(' . implode(',', $values[$index]) . ')';
}
$query = 'INSERT INTO %s (%s) VALUES %s;';
$query = sprintf($query, $table, implode(',', $fields), implode(',', $values));
if (!$dbSource->execute($query)) {
return false;
}
$lastInsertId = $dbSource->getConnection()->lastInsertId();
$insertIds = array();
for ($i = 0; $i < $count; $i++) {
$insertIds[] = $lastInsertId + $i;
}
return $insertIds;
Someone pointed me towards the Big Data Behavior https://github.com/jmillerdesign/CakePHP_Big_Data
If you are using CakePHP 3.0 you can check the answer to this question: How to use insert in query builder insert multiple records?
If you are using CakePHP 2 you will have to use raw SQL like this:
$sql = "INSERT INTO `people` (`name`,`title`) VALUES ";
foreach($people as $person){
list($name,$title) = $person;
$sql.= "('$name','$title'),";
}
$this->query(substr($sql,0,-1));
Source: Inserting Multiple Rows with CakePHP 3
yes you can use like below
The getDataSource() method is static in CakePHP 2.x, so you should be able to use:
$db = ConnectionManager::getDataSource('default');
$db->rawQuery($some_sql);
here i am posting method to do. you have to create some SQL statement manually to insert multiple row in one time.
Please let me know if i can help you more.

Update a row +1 in CakePHP

I am trying to update a row in the database but haven't found a way to do this in the CakePHP way (unless I query the row to retrieve and update).
UPDATE mytable (field) VALUES (field+1) WHERE id = 1
In CodeIgniter, it would have been as simple as:
$this->db->set('field', 'field+1', FALSE);
$this->db->where('id', 1);
$this->db->update('mytable');
How do I do this without querying the row first, retrieve the value, then updating the row with the information I got?
I don't think CakePHP has a similar method for doing this in a normal save() on a single row.
But the updateAll() method, which updates multiple rows, does support SQL snippets like so:
$this->Widget->updateAll(
array('Widget.numberfield' => 'Widget.numberfield + 1'),
array('Widget.id' => 1)
);
The first param is an array of fields/values to be updated, and the second param are the conditions for which rows to update.
Apart from that I think the only thing is to use:
$this->Widget->query('YOUR SQL QUERY HERE');
Which lets you query with raw SQL. [EDIT: but this is not recommended as it bypasses the ORM.]
Try this
<?php
class WidgetsController extends AppController {
public function someFunction( $id = null ){
if( $id ){
// read all fields from the model
// alternately you can $this->Widget->read( array( 'field' ), $id );
$this->Widget->read( null, $id );
// grab the 'field' field so we don't have to type out the data array
$field = $this->Widget->data[ 'Widget' ][ 'field' ];
// where field is the name of the field to be incremented
$this->Widget->set( 'field', $field + 1 );
$this->Widget->save( );
}
// someday cake devs will learn to spell referrer
$this->redirect( $this->referer( ));
}
}
?>
Basically you are passing the id, if it exists you read the Widget model (see the notes above, null as 1st param read the entire table) and then you are using Model::set to st the field to a value one greater than itself - remember to cast to int if you store the field as a char/varchar - and then save the model.

Resources