【问题标题】:how to make new input field like existing input field in a form of cakephp如何以cakephp的形式制作新的输入字段,就像现有的输入字段一样
【发布时间】:2015-02-14 18:11:50
【问题描述】:

嗨,我是 cakephp 的初学者,我喜欢在管理端 add.ctp 添加一个输入字段,我从未使用过,但我知道这个 mvc 结构,所以我尝试复制并粘贴现有的输入字段“名称”,我刚刚复制相同的代码,只需将名称更改为“sidename”即可正常工作。我还在相应的表上创建了一个名为“sidename”的数据库字段。

我不熟悉模型和控制器我只需要知道以下内容

  1. 当我们在表单中添加新字段时,需要更新哪些东西和文件(模型、视图、控制器)以保存数据

  2. 如果我想更新意味着在这种情况下我在我的代码中使用帮助类,那么我需要在哪里更新

  3. 一旦它存储数据库意味着如何像在 ctp 文件中那样检索它

现有代码(我需要在下面的代码中添加新的字段边名??)

enter code here <h3><?php echo __l('General'); ?></h3>
        <?php
            echo $this->Form->input('user_id', array('type' => 'hidden'));
            echo $this->Form->input('clone_deal_id', array('type' => 'hidden'));
            //echo $this->Form->input('name',array('label' => __l('Name')));
            echo $this->Form->input('name', array('type'=>'text','class' => 'js-dealname-count {"display":"js-dealname","field":"js-dealname-count","count":"'.Configure::read('deal.dealname_count').'"}','label'=>__l('Name'), 'info' => __l('This is deal name shown in home page.') . ' ' . '<span class="character-info">' . __l('You have') . ' ' . '<span id="js-dealname-count"></span>' . ' ' . __l('characters left') . '</span>'));

            echo $this->Form->input('sidename',array('label' => __l('SideName')));

            //echo $this->Form->input('sidename', array('type'=>'text','class' => 'js-dealname-count {"display":"js-dealname","field":"js-dealname-count","count":"'.Configure::read('deal.dealname_count').'"}','label'=>__l('SideName'), 'info' => __l('This is deal name shown in side bar.') . ' ' . '<span class="character-info">' . __l('You have') . ' ' . '<span id="js-dealname-count"></span>' . ' ' . __l('characters left') . '</span>'));

            echo $this->Form->input('DealCategory', array('type'=>'select', 'multiple'=>'checkbox','id'=>'DealCategory1', 'label' => __l('Category'),'info' =>'Choose Category'));
            //echo $this->Form->input('deal_category_id', array('label' => __l('Category'),'empty' =>__l('Please Select'))); 
            if($this->Auth->user('user_type_id') == ConstUserTypes::Admin || $this->Auth->user('user_type_id') == ConstUserTypes::Employee):
                echo $this->Form->input('company_id', array('label' => __l('Merchant'),'empty' =>__l('Please Select')));
                echo $this->Form->input('company_slug', array('type' => 'hidden'));
            else:
                echo $this->Form->input('company_id', array('type' => 'hidden'));
                echo $this->Form->input('company_slug', array('type' => 'hidden'));
            endif;
        ?>

【问题讨论】:

标签: php forms cakephp


【解决方案1】:

您应该在 PHP/cakePHP 中使用现代且更好的功能,例如 compact()

//Here you can include many conditions to find data AND fields to select.
$variable_to_pass = $this->YourModel->find('all');    
$this->set(compact('variable_to_pass'));

这可能看起来并不比像上面那样在短期内设置每个变量更好,但让我给你一个更大的画面。

$variable_to_pass1 = $this->YourModel->find('all');    
$variable_to_pass2 = $this->YourModel->find('all');    
$variable_to_pass3 = $this->YourModel->find('all');    
$variable_to_pass3 = $this->YourModel->find('all');    
$variable_to_pass4 = $this->YourModel->find('all');    
$variable_to_pass5 = $this->YourModel->find('all');    
$variable_to_pass6 = $this->YourModel->find('all');
$variable_to_pass7 = $this->YourModel->find('all');
$variable_to_pass8 = $this->YourModel->find('all');
$variable_to_pass9 = $this->YourModel->find('all');

您的老年方法:

$this->set ('variable_to_pass1', $variable_to_pass9);
$this->set ('variable_to_pass2', $variable_to_pass9);
$this->set ('variable_to_pass3', $variable_to_pass9);
$this->set ('variable_to_pass4', $variable_to_pass9);
$this->set ('variable_to_pass5', $variable_to_pass9);
$this->set ('variable_to_pass6', $variable_to_pass9);
$this->set ('variable_to_pass7', $variable_to_pass9);
$this->set ('variable_to_pass8', $variable_to_pass9);
$this->set ('variable_to_pass9', $variable_to_pass10);

现代方法(减少头痛 :) 和更少的代码行。)

$this->set(compact('variable_to_pass1','variable_to_pass2','variable_to_pass3','variable_to_pass4','variable_to_pass5','variable_to_pass6','variable_to_pass7','variable_to_pass8','variable_to_pass9'));

而在View中,你可以这样使用,像在控制器中设置的变量一样使用,

echo $variable_to_pass1;

如果它在数组中,你在 foreach 中的 $variable_to_pass1 中得到什么。

【讨论】:

  • @Rymn,注意你的语言,这不是nonsense。我提供了两种选择,并提出了更好的选择。这一切都在食谱中给出。如果您不知道,请阅读它,如果 OP 遇到任何问题,请仅询问。这是非常基本的问题,Cakephp 食谱中已经回答了。
  • 我猜你是对的,这是一个学习和发现的地方。我还想补充一点,如果每个人都阅读文档,则该网站将不存在...
【解决方案2】:

我认为您的问题与此问题相同cakephp new field not saving

您需要通知 cakephp 您的数据库中有新字段。

【讨论】:

    【解决方案3】:

    您好,欢迎来到 cakephp 的精彩世界。

    因此,如果您在公司中添加一个字段并在数据库中添加一个字段,您基本上就完成了。 Cakephp 为您做所有其他事情。我猜你的模型和你正在使用这个信息的页面,但它会使用与表单数据的其余部分相同的约定来显示。

    在你的控制器中使用

    $general = $this->General-> find ('all');
    

    将获取数据库中的所有行然后使用。

    $this->set ('general', $general);
    

    这将在视图中设置一个名为 general 的变量。

    从外观上可以

    Foreach ($general as $key => $value){
    $sirname = $value['General']['sirname'];
    }
    

    希望这会有所帮助。对不起,我在手机上的格式浪费了一些时间。如果您发布更多代码,我们绝对可以提供更多帮助,甚至可以编写您需要的代码。特别是您的控制器和视图。

    【讨论】:

    • 我只是想让你知道我刚刚在 ctp 文件中添加的一件事,这段代码 echo $this->Form->input('sidename',array('label' => __l(' SideName')));然后我确实用新字段“sidename”更新了我的表,你说这足以将数据保存在 db 中。但是在我提交表单后,它并没有在我的数据库表上更新
    猜你喜欢
    • 2011-12-13
    • 2016-03-09
    • 1970-01-01
    • 1970-01-01
    • 2014-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多