cakephp 3 增量记录
cakephp 3 increment record
我尝试从我的控制器增加一条 table 记录,但它失败了,我想创建一个添加到购物车的方法,该方法将插入新产品或更新现有产品的数量。
另外我想知道是否有任何书籍或教程(cakephp 站点的教程除外)详细解释了 cakephp。
我是 cakephp 的初学者,看起来很困惑,欢迎和感谢任何帮助。
控制器
我的代码:
> function add($id = null,$name = null){
>
> $addCart = $this->Cart->newEntity();
>
> if($this->request->is('Get')){
>
> $data1 = $this->Cart->exists(['productId' => $id]);
>
> //if product doesn't exist in table then add it
> if(!$data1){
> $addCart = $this->Cart->patchEntity($addCart,[
> 'productId' => $id,
> 'name' => $name,
> 'productQty' => 1]
> );
> if($this->Cart->save($addCart)){
> $this->Flash->success(__('The product has been saved.'));
> return $this->redirect(['controller' => 'users','action' => 'index']);
> }else{
> $this->Flash->error(__('The product could not be saved. Please, try again.'));
> }
>
> }else{ //if product already exists in table then update quantity
> $updateQty = $this->Cart->patchEntity($addCart, [
> ['productQty' => 'productQty+1'],
> 'conditions' => ['productId' => $id] //update quantity where the product id matches
> ]);
> if($this->Cart->save($updateQty)){
> $this->Flash->success(__('The product already exists.Quantity updated'));
> return $this->redirect(['controller' => 'users','action' => 'index']);
> }else{
> $this->Flash->error(__('The product could not be updated. Please, try again.'));
> }
> }//end inner if
>
> }//end if
> }//end function
查看
<button class="btn btn-default">
<?= $this->Html->link('Add Product',['controller' => 'Cart', 'action' => 'add',2,'gpu'])
//$this->Html->link('Add Product',['controller' => 'Cart', 'action' => 'index'])
?>
</button>
读取当前值,在 PHP 级别递增并更新 table 容易出现竞争条件,而是使用表达式添加原始 SQL 片段以递增数据库级别的列值
$query = $this->Cart->query();
$result = $query
->update()
->set(
$query->newExpr('productQty = productQty + 1')
)
->where([
'productId' => $id
])
->execute();
另见
我尝试从我的控制器增加一条 table 记录,但它失败了,我想创建一个添加到购物车的方法,该方法将插入新产品或更新现有产品的数量。 另外我想知道是否有任何书籍或教程(cakephp 站点的教程除外)详细解释了 cakephp。 我是 cakephp 的初学者,看起来很困惑,欢迎和感谢任何帮助。
控制器
我的代码:
> function add($id = null,$name = null){
>
> $addCart = $this->Cart->newEntity();
>
> if($this->request->is('Get')){
>
> $data1 = $this->Cart->exists(['productId' => $id]);
>
> //if product doesn't exist in table then add it
> if(!$data1){
> $addCart = $this->Cart->patchEntity($addCart,[
> 'productId' => $id,
> 'name' => $name,
> 'productQty' => 1]
> );
> if($this->Cart->save($addCart)){
> $this->Flash->success(__('The product has been saved.'));
> return $this->redirect(['controller' => 'users','action' => 'index']);
> }else{
> $this->Flash->error(__('The product could not be saved. Please, try again.'));
> }
>
> }else{ //if product already exists in table then update quantity
> $updateQty = $this->Cart->patchEntity($addCart, [
> ['productQty' => 'productQty+1'],
> 'conditions' => ['productId' => $id] //update quantity where the product id matches
> ]);
> if($this->Cart->save($updateQty)){
> $this->Flash->success(__('The product already exists.Quantity updated'));
> return $this->redirect(['controller' => 'users','action' => 'index']);
> }else{
> $this->Flash->error(__('The product could not be updated. Please, try again.'));
> }
> }//end inner if
>
> }//end if
> }//end function
查看
<button class="btn btn-default">
<?= $this->Html->link('Add Product',['controller' => 'Cart', 'action' => 'add',2,'gpu'])
//$this->Html->link('Add Product',['controller' => 'Cart', 'action' => 'index'])
?>
</button>
读取当前值,在 PHP 级别递增并更新 table 容易出现竞争条件,而是使用表达式添加原始 SQL 片段以递增数据库级别的列值
$query = $this->Cart->query();
$result = $query
->update()
->set(
$query->newExpr('productQty = productQty + 1')
)
->where([
'productId' => $id
])
->execute();
另见