Trouble With Createcommand And Now()

hello forum!

i want to execute this query




$query = 'INSERT INTO Pedidos (OrdenCompra, Cliente, Fecha, Anulado)

		VALUES (' . (int)$id . ', ' . Yii::app()->user->id . ', NOW(), 0)';

$query = Yii::app()->db->createCommand($query)->execute();

but fail

would be the right way to do?

Hello xchwarze!

Try something like this:




...

$fecha = new CDbExpression('NOW()');


$query = 'INSERT INTO Pedidos (OrdenCompra, Cliente, Fecha, Anulado)

          VALUES (' . (int)$id . ', ' . Yii::app()->user->id . ', ' . $fecha . ', 0)';


$query = Yii::app()->db->createCommand($query)->execute();

...




CDbException

:(

I don’t get it!! I have tested the code right now and works perfectly! Btw… show us your code, the full error and the database table so we can help you better.

thanks RSfTDL!

I changed NOW() with GETDATE() and works!

(I forget to mention that database engine used)

Hi

You can write a insert query


$model = new Pedidos();

$model->OrdenCompra=$id;

$model->Cliente='';

$model->Fecha=Yii::app()->user->id;

$model->Anulado=date('Y-m-d');

$model->save(false)