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


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

在 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;
}
嘿,

您可以在此网址下下载 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。现在,如果您想在更新数据时更改 slug,那么您应该传递"onUpdate"=> true
这是为了创建独特的蛞蝓
Cakephp还提供了一些功能,您可以在此URL下检查 http://book.cakephp.org/2.0/en/core-utility-libraries/inflector.html

变形符::slug($word, $replacement = '_')

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

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