0

I am new to phalcon framework.I would like to update data from phalcon model update method.

$sql = "UPDATE table SET col1='1',col2 = NULL WHERE 1";

to

>  $all = model::findFirst();
>                 $all->col1 = '1';
>                 $all->col2= NULL;
>                 $all->update();

I have no idea about for "where 1" .

2 Answers2

0

Before you can update a model you need to request it from the database.
In the following example you are querying the first record where the value in column col1 equals 123.

$all = model::findFirst(['col1 = 123']);
// you can also write this like
$all = model::findFirstByCol1(123);

In the background Phalcon will convert the above code to a query, similar to:

SELECT * FROM model WHERE col1 = 123 LIMIT 1;

Now that you have access to the model via $all, you can alter its attributes:

$all->col2 = null;

If you are done altering $all, you can update the values in the database:

$all->update(); // or $all->save();

Refer to the Phalcon documentation, if you need more help on working with models.

Timothy
  • 2,004
  • 3
  • 23
  • 29
-1

What is this WHERE 1 ? You mean WHERE id = 1 ? Then you need:

$all = model::findFirst(1);
$all->col1 = '1';
$all->col2= NULL;
$all->update();
Juri
  • 1,369
  • 10
  • 16
  • 1
    It will select if id is your PK(primary key) in database. Passing integer value to findFirst means phalcon will look for PK - if it will find it it will get the name of this PK and find for value which you passed. – Juri Jun 17 '16 at 12:58
  • table has without primary key . – illusionist Jun 20 '16 at 03:26
  • Then you need to use findFirstByCol1 or something. Or just create PR. Also phalcon as far as i know will not update anything until it has PR. – Juri Jun 20 '16 at 18:58