douyi5157 2014-12-14 09:26
浏览 35

Phalcon的保存方法不像我预期的那样工作

I am trying to use Phalcons save() function to either create or update a record depending on whether the record is already in the database from here

I am doing the following:

$vars = $this->request->getPost();
$code = new Code();
$code->save( $vars, array("code_type", "code", "name") );

When I am sending an update to my controller, the "id" field has the primary key populated, whereas it is blank (but the array key still exists) if I am creating the record.

My understanding is that the ORM should either create or update the record depending on whether the primary key exists or not. The problem I am having is that it is always creating the record- not updating.

I've also tried something like the below, however the reverse is true, when I use find:

$code = Code::findFirst($vars["id"]);
$code->save( $vars, array("code_type", "code", "name") );

Any idea what I could be doing wrong? I want to get to a point where I have a single controller for my insert/update actions.

  • 写回答

1条回答 默认 最新

  • dongzhuo3376 2014-12-30 21:45
    关注

    It's not surprising that the first example is only creating new records, because you're neither loading an existing record nor including the "id" in the white list of fields to save. Instantiating your object as follows should solve the problem (assuming the "id" is an integer):

    $code = ($id = (int) $vars['id']) ? Code::findFirst($id) : new Code();
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度