2016-03-15 31 views
1

我有一種將產品添加到數據庫的方法,我想自動將產品名稱編輯爲產品的網址。 它是怎麼回事?在添加數據庫之前在php中編輯表單輸入

這是我的形式:

<?php echo $this->Form->create('Product', array('type'=>'file')); ?> 
     <fieldset> 
      <legend><?php echo __('Add Product'); ?></legend> 
     <?php 
      echo $this->Form->input('category_id'); 
      echo $this->Form->input('name'); 
      echo $this->Form->input('Picture.0.name', array('type'=>'file')); 
      echo $this->Form->input('description'); 
     ?> 
     </fieldset> 
    <?php echo $this->Form->end(__('Submit')); ?> 
在產品表

在數據庫有一個名爲url coloumn。我想獲取產品的名稱並進行編輯,然後將其保存爲產品的網址。

public function add() { 
    if ($this->request->is('post')) { 
     $this->Product->create(); 
     $this->request->data['Product']['user_id'] = $this->member['id']; 
     if ($this->Product->saveAll($this->request->data)) { 
      $this->Flash->success(__('The product has been saved.')); 
      return $this->redirect(array('action' => 'index')); 
     } else { 
      $this->Flash->error(__('The product could not be saved. Please, try again.')); 
     } 
    } 
    $categories = $this->Product->Category->find('list'); 
    $users = $this->Product->User->find('list'); 
    $this->set(compact('categories', 'users')); 
} 

這是我的編輯功能:

function edit_url($input_str){ 
    $input_str = strtolower($input_str); 
               // replaces other characters to en characters 
    $input_str=str_replace('â', 'a', $input_str); 
    $input_str=str_replace('ç', 'c', $input_str); 
    $input_str=str_replace('ğ', 'g', $input_str); 
    $input_str=str_replace('ı', 'i', $input_str); 
    $input_str=str_replace('ö', 'o', $input_str); 
    $input_str=str_replace('ş', 's', $input_str); 
    $input_str=str_replace('ü', 'u', $input_str); 
               // replaces spaces with - 
    $input_str=str_replace(' ', '-', $input_str); 
               // replaces symbols 
    $input_str=str_replace('.', '', $input_str); 
    $input_str=str_replace('/', '', $input_str); 
    $input_str=str_replace('*', '', $input_str); 
    $input_str=str_replace('+', '', $input_str); 
    $input_str=str_replace('?', '', $input_str); 

    return $input_str; 
} 
+1

您可以在Model beforeSave()函數中做到這一點。請檢查蛋糕手冊。 – Indrajit

+0

這不是關於beforeSave()函數。在形式上沒有user_id的地方。我像那樣添加了它。我可以在這些代碼中完成它的工作,這裏是你看到的一個例子: $ this-> request-> data ['Product'] ['user_id'] = $ this-> member ['id']; – sudu

+0

請讓我知道如何將產品名稱保存爲db中的url。您可以在編輯過程中使用相同的代碼。您沒有提及如何將網頁名稱保存爲網址。據我瞭解,你問的是slu?? –

回答

0

嘿,你可以在此URL下下載CakePHP的實用插件http://milesj.me/code/cakephp/utility ,那麼你應該在你的模型中添加此代碼


     public $actsAs = array(
       'Utility.Sluggable' => array(
        'field'  => 'title', // Field that will be slugged 
        'slug'  => 'slug', // Field that will be used for the slug 
        'lowercase' => true, // Do we lowercase the slug ? 
        'separator' => '_', 
        'onUpdate' => false// 
       )); 

您還應該創建一個列在你的數據庫表中爲slug。 現在,如果你想改變更新數據,那麼你應該通過蛞蝓「的onUpdate」 =>真
這是創造獨特塞
CakePHP的還proives一些功能,你可以在這個網址http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html

變形器下檢查: :slug($ word,$ replacement ='_')

0

$這個 - >請求 - >數據[ '產品'] [ '鏈接'] = $這個 - > slugify($這個 - >請求 - >數據[ '產品'] [ '名稱']);

解決了我的問題。但還有一個問題。如果有一個以上的產品那些具有相同的名稱...

相關問題