2013-11-28 63 views
-1

您好我有一个为具有一个场(网址),我想在表单中键入的URL插入到数据库如何将数据插入到数据库中的CakePHP

我的函数添加在我的控制器看起来像这个:

public function add(){ 
    if($this->request->is('post')){ 

     // debug($this->Link->find('all'));//this works 

     $link = $this->Link->findByUrl($this->request->data['link']['url']); 
     if(empty($link)){ 
      //I will create the link 
      $this->Link->create($this->request->data, true);//true = ignoring the ID 
      $this->Link->save(null, true, array('url')); 

      //null = because I ready wrote "$this->request->data" in create 
      //true = I want to use VALIDATION 
      //array = I choose to SAVE this field only 
      echo "I've created the link"; 
      die($this->Link->id); 
     }else { 
      debug($link); 
      die("The link is already in the database"); 
      //je dois récupérer le lien 
     } 

    } 

如何在数据库中插入输入的链接?

回答

1

不知道你正在使用的蛋糕的版本,但是这通常会的工作方式是:

  • create()新的参考
  • set()新的数据
  • save()参考

例如:

 $this->Link->create(); 
     $this->Link->set($this->request->data['link']); 
     $save = $this->Link->save(); 
     echo $save ? "I've created the link" : "Error creating link!"; 
     die($this->Link->id); 
相关问题