Phalcon PHP: Update a primary natural key

456 views Asked by At

Is there a good way to be able to update a table which has a natural key in Phalcon?

Consider this table:

people
------
person
created_at
updated_at

We're going to assume that the person field is unique and is the primary key. I try to do the following:

$person = new People();
$person->person = 'Ed';
$person->save();

$personUpdate = People::findFirst('person = "Ed"');
$personUpdate->person = 'Bob';
$person->save();

What Phalcon ends up trying to do is to INSERT a new record, rather than to update the existing record. What I need it to do is to UPDATE ... WHERE person = 'Ed';

Thoughts?

Thanks!

3

There are 3 answers

2
Raj On

You are doing correct except ... People::find

find will prepare to fetch all data.. this means its in array Documentation

You need to use findFirst instead of find

$personUpdate = People::findFirst('person = "Ed"');
$personUpdate->person = 'Bob';
$person->save();
1
Dheeresha On

Try the following...

<?php 
$personUpdate = People::findFirst('person = "Ed"');
$personUpdate->person = 'Bob';
$person->update();
0
Trent Ramseyer On

Please note that you are using $person->update() instea of $personUpdate->update();

    <?php 
$personUpdate = People::findFirst('person = "Ed"');
$personUpdate->person = 'Bob';
$person->update();