0
Ich benutze CakePHP 2 in meinem Controller hinzufügen Funktion, ich möchte die Daten bearbeiten, wenn die ID existiert, wenn nicht existiert create.Cakephp Funktion hinzufügen, wenn ID existiert bearbeiten sonst erstellen
Dies ist mein Code in Add-Funktion:
public function add() {
if(!$this->request->data){
throw new NotFoundException();
}
$googleCategory = $this->request->data;
foreach ($googleCategory as $key => $value) {
if(empty($value['category'])){
unset($value);
}
$conditions = array (
'AccountShopMeta.shop_id' => $value['shop_id'],
'AccountShopMeta.name' => $value['category'],
'AccountShopMeta.value' => $value['url_key']
);
if(!$this->AccountShopMeta->hasAny($conditions)){
$this->AccountShopMeta->create();
$data['shop_id'] = $value['shop_id'];
$data['name'] = $value['category'];
$data['value'] = $value['url_key'];
$data['tag'] = '';
if($this->AccountShopMeta->save($data)){
$account_shop_meta = $this->AccountShopMeta->read();
$this->set($account_shop_meta);
$this->set('_serialize', array_keys($account_shop_meta));
}
}
}
}