我在CakePHP中为用户和任务做了一个小应用程序。 如果删除用户我不想删除它们,只需更改数据库中的值,其中列为' status'来自' 1'到' 0'。我不知道该怎么做。
用户控制器代码(删除):
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$user = $this->Users->get($id);
if ($user['status'] == '1') {
//Here should be code about change value
$this->Flash->success(__('The user has been deleted.'));
} else {
$this->Flash->error(__('The user could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
有什么想法吗?
答案 0 :(得分:0)
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$user = $this->Users->get($id);
if ($user->status == 1) {
$user->status = 0;
$this->Users->save($user);
$this->Flash->success(__('The user has been deleted.'));
} else {
$this->Flash->error(__('The user could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
答案 1 :(得分:0)
如果您按照正确的命名约定创建了模型,那么您可以尝试下面的代码。
var tableData = [String]()
var tableAvailable = [String]()
var tableImages = [String]()
var tableDesc = [String]()
var tablePin = [String]()
override func viewDidAppear(_ animated: Bool) {
let nib = UINib(nibName: "vwTblCell", bundle: nil)
tableView.register(nib, forCellReuseIdentifier: "cell")
self.tableData.removeAll(keepingCapacity: false)
self.tableAvailable.removeAll(keepingCapacity: false)
self.tableImages.removeAll(keepingCapacity: false)
self.tableDesc.removeAll(keepingCapacity: false)
let url = NSURL(string: "https://www.asmserver.co.uk/sally/parsexml.php")!
let task = URLSession.shared.dataTask(with: url as URL) { (data, response, error) -> Void in
if let urlContent = data {
do {
if let jsonResult = try JSONSerialization.jsonObject(with: urlContent, options: []) as? [[String:AnyObject]] {
for item in jsonResult {
guard let name = item["display-name"] as? String else { continue }
if (self.tableData.contains(item["display-name"] as! String)) {
}else{
self.tableData.append(name)
guard let available = item["status"] as? String else { continue }
self.tableAvailable.append(available)
guard let image = item["image-url"] as? String else { continue }
self.tableImages.append(image)
guard let desc = item["short-desc"] as? String else { continue }
self.tableDesc.append(desc)
guard let pin = item["agent-id"] as? String else { continue }
self.tablePin.append(pin)
}
}
}
} catch {
print("JSON serialization failed")
}
} else {
print("ERROR FOUND HERE")
}
DispatchQueue.main.async(execute: { () -> Void in
self.tableView.reloadData()
})
self.tableView.isUserInteractionEnabled = true
}
task.resume()
}
override func viewDidLoad() {
if revealViewController() != nil {
menuButton.addTarget(self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)), for: UIControlEvents.touchUpInside)
self.view.addGestureRecognizer(revealViewController().panGestureRecognizer())
}
}
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.tableData.count
}
// 3
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell: TblCell = self.tableView.dequeueReusableCell(withIdentifier: "cell") as! TblCell
cell.lblCarName.text = tableData[indexPath.row]
let url = NSURL(string: "\(tableImages[indexPath.row])")
if let data = NSData(contentsOf: url as! URL) {
cell.imgCarNane.sd_setImage(with: (string: url) as URL!)
}
cell.pinLabel.text = tablePin[indexPath.row]
if(tableAvailable[indexPath.row] == "Busy"){
cell.onlineIcon.image = UIImage(named: "livefeedofflineicon.png")
}
if (indexPath.row % 2 == 0){
cell.contentView.backgroundColor = UIColor(red: 237/255.0, green: 234/255.0, blue: 234/255.0, alpha: 1.0)
}
return cell
}
// 4
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
print("Row \(indexPath.row) selected")
}
// 5
func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
return 140
}