2

I've MySQL Query like this

INSERT INTO users (firstname, amount) VALUES ('Suresh', ABS('-400'));

It inserts data like this

Suresh | 400

Same Query with CakePHP Model

$this->User->query("INSERT INTO users (firstname, amount) VALUES ('Suresh', ABS('-400'))");

It inserts data like this

Suresh | 400

Now My Question is How Can I use MySQL functions with CakePHP save() function

if ($this->request->is('post')) {
     $this->User->create();
     $this->request->data['User']['firstname'] = "Suresh";    //This is inserting in database
     $this->request->data['User']['amount'] = "ABS('-400')";  //This is not inserting in database
     $this->User->save($this->request->data);
}

Please let me know where I'm doing wrong?

7
  • Firstname is inserting ? Commented Mar 30, 2016 at 7:09
  • Yeah I've tried with ABS(-400) I can use PHP function, but I want to use MySQL functions @sagi Commented Mar 30, 2016 at 7:11
  • Yes Firstname is inserting @AlimonKarim Commented Mar 30, 2016 at 7:11
  • 2
    You should have choosen a different example. ABS() can be easily solved at PHP level, there is absolutely no need to handle this at SQL level. Commented Mar 30, 2016 at 7:26
  • 1
    I know this can be handled by PHP side also, but I'm curious to know how can i use MySQL function with save() function, It allows in query() function but not allowing in save() function. @ndm Commented Mar 30, 2016 at 7:29

2 Answers 2

3

You could try to use a DboSource expression, for example:

$this->request->data['User']['amount'] =
    $this->User->getDataSource()->expression('ABS(-400)');

Expression objects are not going to be sanitized or escaped, but inserted into the query as is.

A word of warning, be careful what you are passing, in case the -400 would actually be user input, you would need to make sure that it is being sanitized/casted/validated properly!

See also

Sign up to request clarification or add additional context in comments.

3 Comments

After applying your code i got this error "Non-static method DboSource::expression() should not be called statically, assuming $this from incompatible context.." Is there anything I'm missing? or I've to include anything?
@ShriSuresh Just like the error message says, the method isn't static, so it shouldn't be called that way. Use $this->User->getDataSource()->expression() instead. And a word of warning, be careful what you are passing, in case the -400 would actually be user input, you would need to make sure that it is being sanitized/casted properly!
I used $connection->newQuery()->newExpr('WorkoutOrder + 1') successfully to increment a column, note the warning from ndm though
0

Change your code

$this->request->data['User']['amount'] = "ABS('-400')";  // here you are sending string.

To

$this->request->data['Album']['amount'] = ABS('-400'); 

1 Comment

I would Like to use MySQL functions not PHP function, in above case it's using PHP function.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.