CakePHP 3.6.11:更新 table 中的值
CakePHP 3.6.11: Update value in table
我有这个表:
customers[id, name, surname, phone, text, balance, created]
service_types[id, title, price, length, is_subscription, created]
customer_service_types[id, customer_id, service_type_id, price, created]
当我为客户添加新的 customerServiceType
时,我想使用 customerServiceType
的给定 price
值相应地更新客户的 balance
。
所以这是我尝试过的:
CustomerServiceTypes/add.ctp
:
<fieldset>
<legend><?= __('Add Customer Service Type') ?></legend>
<?php
echo $this->Form->control('customer_id', ['options' => $customers]);
echo $this->Form->control('service_type_id', ['options' => $serviceTypes]);
echo $this->Form->control('price');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
Model/Entity/CustomerServiceType.php
:
class CustomerServiceType extends Entity
{
//On create a new CustomerServiceType then update the selected customer's balance
public function addCustomerServiceType($data = array()){
$this->create;
$this->save($data);
//update customer balance
$customer = $this->Customers->get($data['customerid']);
$this->Customer->updateAll(
array('Customer.balance' => 'Customer.balance + ' . $data['price']),
array('Customer.id' => $data['customerid'])
);
return true;
}
}
所以在 CustomerServiceTypesController.php
中,我将 add()
更改为:
public function add($customerid = null)
{
$customerServiceType = $this->CustomerServiceTypes->newEntity();
if ($this->request->is('post')) {
$customerServiceType = $this->CustomerServiceTypes->patchEntity($customerServiceType, $this->request->getData());
if ($this->CustomerServiceType->addCustomerServiceType($this->request->data)) {
$this->Flash->success(__('The customer service type has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The customer service type could not be saved. Please, try again.'));
}
$customers = $this->CustomerServiceTypes->Customers->find('list', ['limit' => 200]);
$serviceTypes = $this->CustomerServiceTypes->ServiceTypes->find('list', ['limit' => 200]);
$this->set(compact('customerServiceType', 'customers', 'serviceTypes'));
}
但是我得到这个错误:
Call to a member function addCustomerServiceType() on boolean
还有这条通知:
Undefined property: CustomerServiceTypesController::$CustomerServiceType
既然你希望在保存数据后发生一些事情,我认为你最好使用Model.afterSave
事件:https://book.cakephp.org/3.0/en/orm/table-objects.html#aftersave
在你的控制器中
$this->CustomerServiceTypes->save($customerServiceType)
在你的Table对象中CustomerServiceTypesTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Event\Event;
use Cake\ORM\TableRegistry;
use ArrayObject;
use Cake\Datasource\EntityInterface;
class CustomerServiceTypesTable extends Table
{
public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options)
{
// only execute if the entity is new
// if you want this to happen on edits too, remove this clause
if ($entity->isNew()) {
// get instance of the Customers table
$customers = TableRegistry::getTableLocator()->get('Customers');
$customer = $customers->get($entity->customerid);
// do more stuff here
}
}
}
我有这个表:
customers[id, name, surname, phone, text, balance, created]
service_types[id, title, price, length, is_subscription, created]
customer_service_types[id, customer_id, service_type_id, price, created]
当我为客户添加新的 customerServiceType
时,我想使用 customerServiceType
的给定 price
值相应地更新客户的 balance
。
所以这是我尝试过的:
CustomerServiceTypes/add.ctp
:
<fieldset>
<legend><?= __('Add Customer Service Type') ?></legend>
<?php
echo $this->Form->control('customer_id', ['options' => $customers]);
echo $this->Form->control('service_type_id', ['options' => $serviceTypes]);
echo $this->Form->control('price');
?>
</fieldset>
<?= $this->Form->button(__('Submit')) ?>
<?= $this->Form->end() ?>
Model/Entity/CustomerServiceType.php
:
class CustomerServiceType extends Entity
{
//On create a new CustomerServiceType then update the selected customer's balance
public function addCustomerServiceType($data = array()){
$this->create;
$this->save($data);
//update customer balance
$customer = $this->Customers->get($data['customerid']);
$this->Customer->updateAll(
array('Customer.balance' => 'Customer.balance + ' . $data['price']),
array('Customer.id' => $data['customerid'])
);
return true;
}
}
所以在 CustomerServiceTypesController.php
中,我将 add()
更改为:
public function add($customerid = null)
{
$customerServiceType = $this->CustomerServiceTypes->newEntity();
if ($this->request->is('post')) {
$customerServiceType = $this->CustomerServiceTypes->patchEntity($customerServiceType, $this->request->getData());
if ($this->CustomerServiceType->addCustomerServiceType($this->request->data)) {
$this->Flash->success(__('The customer service type has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The customer service type could not be saved. Please, try again.'));
}
$customers = $this->CustomerServiceTypes->Customers->find('list', ['limit' => 200]);
$serviceTypes = $this->CustomerServiceTypes->ServiceTypes->find('list', ['limit' => 200]);
$this->set(compact('customerServiceType', 'customers', 'serviceTypes'));
}
但是我得到这个错误:
Call to a member function addCustomerServiceType() on boolean
还有这条通知:
Undefined property: CustomerServiceTypesController::$CustomerServiceType
既然你希望在保存数据后发生一些事情,我认为你最好使用Model.afterSave
事件:https://book.cakephp.org/3.0/en/orm/table-objects.html#aftersave
在你的控制器中
$this->CustomerServiceTypes->save($customerServiceType)
在你的Table对象中CustomerServiceTypesTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Event\Event;
use Cake\ORM\TableRegistry;
use ArrayObject;
use Cake\Datasource\EntityInterface;
class CustomerServiceTypesTable extends Table
{
public function afterSave(Event $event, EntityInterface $entity, ArrayObject $options)
{
// only execute if the entity is new
// if you want this to happen on edits too, remove this clause
if ($entity->isNew()) {
// get instance of the Customers table
$customers = TableRegistry::getTableLocator()->get('Customers');
$customer = $customers->get($entity->customerid);
// do more stuff here
}
}
}