如何使用cakephp在数据库中插入数据

时间:2017-11-06 07:26:51

标签: mysql cakephp

我有一个表名存储,我的控制器名称是StoresController.php,模型名称是Stores.php,并且在Stores文件夹中有一个add.ctp文件,但我无法从form中插入数据。这是我的添加。 ctp文件

<div class="row form-main">
  <div class="panel panel-default">
    <div class="panel-body">
      <?php echo $this->Form->create('Store', array('class'=>"contact-
          form")); 
      ?>
      <div class="form-group">
         <div class="col-sm-6">
      <?php 
      echo $this->Form->input('name',array('required'=>false,'class'=>"form-
      control",  'placeholder'=>"Enter Name","label"=>false)); 
      ?>
     </div>
     <div class="col-sm-6">
     <?php 
     echo $this->Form-
     >input('address1',array('required'=>false,'class'=>"form-control",  
    'placeholder'=>"Enter Address1","label"=>false)); 
     ?>
            </div>
          </div>
          <div class="form-group">        
            <div class="col-sm-offset-2 col-sm-10">
              <input type="submit" name="submit" value="Submit" class="btn 
              btn-success">
              <button type="submit" class="btn btn-info">Cancel</button>
            </div>
          </div>
         <?php echo $this->Form->end(); ?>

这是我的StoresController.php

<?php
App::uses('AppController', 'Controller');
class StoresController extends AppController {
var $uses = array();
var $component = array("Common");
public $paginate = array();  


public function add(){
    $this->layout = 'user_layout';
    $user_id = $this->UserAuth->getUserId();
    $owner_id = $this->Common->getOwnerId($user_id);
    if ($this->request->is('post')) { 
        $this->Store->create(); 
        if ($this->Store->save($this->request->data)) { 
            $this->Flash->success(__('User has been Added')); 
            return $this->redirect(array('action' => 'add')); 
        } $this->Flash->error(__('Unable to add your post.')); 
    } 

}    

}

?>

我收到错误在控制器中的非对象上调用成员函数create()

2 个答案:

答案 0 :(得分:0)

你应该使用:

$这 - &GT;形状配合&GT;创建( '存储');

class Userprofile(models.Model):
    user = models.OneToOneField(User)
    profile_id = models.IntegerField(db_column='profile_id')  # Field namemade lowercase.
    f_name = models.CharField(max_length=45, blank=True, null=True)
    middle_name = models.CharField(max_length=45, blank=True, null=True)
    l_name = models.CharField(max_length=45, blank=True, null=True)
    dob = models.DateField(db_column='DOB', blank=True, null=True)  # Field namemade lowercase.
    contact_number = models.IntegerField(blank=True, null=True)
    email = models.CharField(max_length=45, blank=True, null=True)
    joining_date = models.DateField(db_column='Joining_date', blank=True, null=True)  # Field name made lowercase.
    temp_address = models.CharField(max_length=45, blank=True, null=True)
    permanant_add = models.CharField(db_column='Permanant_add', max_length=45, blank=True, null=True)  # Field name made lowercase.
    user_image = models.CharField(max_length=45, blank=True, null=True)
    gender_idgender = models.ForeignKey(Gender, models.DO_NOTHING, db_column='gender_idgender')

    class Meta:
         managed = False
         db_table = 'userprofile'
         unique_together = (('profile_id', 'gender_idgender'),)

答案 1 :(得分:-1)

是的,我解决了这个问题。有一个小问题。我忘记了控制器中的以下行:

var $uses = array('Store');