如何以cakephp的形式制作像现有输入字段一样的新输入字段

how to make new input field like existing input field in a form of cakephp

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

上以 'sidename' 的名称创建了一个数据库字段

我对model和controller不熟悉,只需要了解以下内容

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

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

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

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

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

在你的控制器中使用

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

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

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

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

从视图可以

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

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

您应该在 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');

您的 OLD 年龄接近:

$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 中得到的内容。

我觉得你的问题和这个问题一样cakephp new field not saving

You need to inform cakephp that you have new fields in your db.