在添加数据库之前编辑 php 中的表单输入

Editing Form inputs in php before adding databse

我有一种将产品添加到数据库的方法,我想自动将产品名称编辑为产品 url。 怎么可能?

这是我的表格:

<?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')); ?>

in product table in db 有一个名为 url 的列。我想获取产品名称并对其进行编辑,然后将其保存为产品的 url.

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;
}

$this->request->data['Product']['url'] = $this->slugify($this->request->data['Product'][ 'name']);

解决了我的问题。但是还有一个问题。如果有多个产品具有相同的名称...

嘿,你可以在这个url下下载cakephp Utility插件 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 table。 现在,如果你想在更新数据时更改 slug,那么你应该传递 'onUpdate' => true
这是为了创建 Unique slug
Cakephp 还提供了一些功能,您可以在此 url http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html

下查看

Inflector::slug($word, $replacement = '_')