简体   繁体   English

Phalcon PHP:更新主要自然键

[英]Phalcon PHP: Update a primary natural key

Is there a good way to be able to update a table which has a natural key in Phalcon? 有没有一种方法可以更新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. 我们假设person字段是唯一的并且是主键。 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. Phalcon最终试图做的是插入新记录,而不是更新现有记录。 What I need it to do is to UPDATE ... WHERE person = 'Ed'; 需要它做的是UPDATE ... WHERE person ='Ed';

Thoughts? 思考?

Thanks! 谢谢!

Try the following... 试试以下......

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

You are doing correct except ... People::find 你做的正确,除了...... People::find

find will prepare to fetch all data.. this means its in array Documentation find将准备获取所有数据..这意味着它在数组文档中

You need to use findFirst instead of find 您需要使用findFirst而不是find

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

Please note that you are using $person->update() instea of $personUpdate->update(); 请注意,您使用$ personUpdate-> update()的$ person-> update()instea;

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM