I am new to phalcon framework.I would like to update data from phalcon model update method.
$sql = "UPDATE
table
SETcol1
='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" .
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.
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();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.