1

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!

Mr Mikkél
  • 2,577
  • 4
  • 34
  • 52

3 Answers3

2

Try the following...

<?php 
$personUpdate = People::findFirst('person = "Ed"');
$personUpdate->person = 'Bob';
$person->update();
Machavity
  • 30,841
  • 27
  • 92
  • 100
Dheeresha
  • 797
  • 1
  • 4
  • 6
  • Aw, man. I was so hopeful! Even with ->update(), nothing actually happens. (No CREATE nor UPDATE.) As soon as I don't try to update the nat. key, everything's golden. – Mr Mikkél Dec 09 '14 at 19:03
1

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();
Raj
  • 1,083
  • 8
  • 22
  • Yes, you're right. I wasn't thinking and was just typing from my head. I actually do use the findFirst to get a single object. – Mr Mikkél Dec 08 '14 at 20:54
  • It doesn't fix the problem, I'm sorry :( I'm grateful for the answer, though, because it reminded me to change my code above. – Mr Mikkél Dec 09 '14 at 07:02
0

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

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