cakephp 2.x:在saveField()中使用变量

时间:2014-11-24 16:26:11

标签: php cakephp

所以说我想为我的系统只保存一个特定交易的2个字段*基本上我从库存库存中取出物品"

public function admin_take($id = null) {
    if (!$this->Inventory->exists($id)) {
        throw new NotFoundException(__('Invalid inventory item'));
    }
    if ($this->request->is(array('post', 'put'))) {
        if ($this->request->data['Inventory']['amount_taken'] < $this->Inventory['Inventory']['amount']){
                amt = $this->request->data['Inventory']['amount_taken'];
                $this->Inventory['Inventory']['amount'] = $this->Inventory['Inventory']['amount'] - amt;
                $this->Inventory['Inventory']['amount_taken'] = $this->Inventory['Inventory']['amount_taken'] + amt;
                $this->Inventory->saveField('amount_taken','x');
                $this->Inventory->saveField('amount','y');
                $this->Session->setFlash(__('The inventory item has been saved.'));
                return $this->redirect(array('action' => 'index'));
            }
            else if ($this->request->data['Inventory']['amount_taken'] > $this->Inventory['Inventory']['amount']) {
                $this->Session->setFlash(__('request could not be processed, not enough inventory stock to fulfil request .'));
            }
            else {
                $this->Session->setFlash(__('The request could not be processed, please try again.'));
            }

        }
    } 
    else {
        $options = array('conditions' => array('Inventory.' . $this->Inventory->primaryKey => $id));
        $this->request->data = $this->Inventory->find('first', $options);
    }
}

是否可以使用包含上述算术运算中的变更数据的变量替换saveField中的x和Y?

我看到的大多数示例都倾向于使用saveField $ stringValue中的实际字符串,如果我真的需要字符串?我将如何处理字符串替换?

2 个答案:

答案 0 :(得分:1)

Dave的答案确实是正确的,只需改变它并看到。 Cakephp已经完成了许多数据库操作,而且你的代码做了很多次。

$this->Inventory->saveField('amount_taken','x');
$this->Inventory->saveField('amount','y');

是2个操作。更好的是:

$this->Inventory->save(array('amount_taken' =>'x', 'amount' => 'y'));

$this->Inventory->set('amount_taken','x');
$this->Inventory->set('amount','y');
$this->Inventory->save();

答案 1 :(得分:0)

  

是否可以使用变量替换saveField中的x和Y.   包含上述算术运算中的改变数据?

是的。试试吧,你会发现它有效。

  

我将如何处理字符串替换?

步骤1)替换&#39; x&#39;或者&#39; y&#39;用你的算术运算。这可以通过使用你的&#34;复制&#34;和&#34;粘贴&#34;快捷键。 (在Windows上按Ctrl + C / Ctrl + P)

步骤2)庆祝!

相关问题