使用Zend中的单个表单将数据插入两个表中

时间:2012-04-19 17:14:47

标签: zend-framework

我有两个表'登录'和'个人资料'。 login table包含user_id, username, password, typeprofile table包含profile_id, user_id, name, address, phone_no,email,status,pancardno,gender,birthday,joiingdate,endingdate。在这里,我使用user_id作为reference key。表单包含用户名,密码,类型,名称,地址,电话号码。那么如何在Zend Framework的配置文件表中插入用户名,密码,输入'login'表和ohter字段 这是我的控制器代码。

  

include_once(APPLICATION_PATH.'/modules/admin/models/DbTable/Login.php');
public function emppostAction()
{
$session=new Zend_Session_Namespace();
if(isset($session->id))
{
$this->view->name="<b>".$session->name."</b>";
$this->render('employee');

          $data= new Model_DbTable_Login();
          if($this->getRequest()->isPost())
          {
              $un=$this->getRequest()->getPost('un');
              $name=$this->getRequest()->getPost('name');

              $bday=$this->getRequest()->getPost('bday');
              $bmnth=$this->getRequest()->getPost('bmonth');
              $byear=$this->getRequest()->getPost('byear');                   
              $bdate=$byear."-".$bmnth."-".$bday;         

              $jday=$this->getRequest()->getPost('jday');
              $jmnth=$this->getRequest()->getPost('jmonth');
              $jyear=$this->getRequest()->getPost('jyear');
              $jdate=$jyear."-".$jmnth."-".$jday;

              $eday=$this->getRequest()->getPost('eday');
              $emnth=$this->getRequest()->getPost('emonth');
              $eyear=$this->getRequest()->getPost('eyear');                   
              $edate=$eyear."-".$emnth."-".$eday;

              $phoneno=$this->getRequest()->getPost('phoneno');
              $add=$this->getRequest()->getPost('add');   
              $qf=$this->getRequest()->getPost('qf');
              $jod=$this->getRequest()->getPost('jod');
              $email=$this->getRequest()->getPost('email');
              $pwd=$this->getRequest()->getPost('pwd');
              $gn=$this->getRequest()->getPost('gender');
              $ms=$this->getRequest()->getPost('ms');
              $desg=$this->getRequest()->getPost('desig');
              $status=$this->getRequest()->getPost('status');
              $pan=$this->getRequest()->getPost('pancard');


              $insert=$data-> >insertData($un,$pwd$name,$bdate,$phoneno,$add,$qf,$jdate,$edate,$gn,$ms,$desg,$email,$pan,>$status);

          $this->_helper->redirector('viewemp', 'Leave');
              exit;   

          }               
      }
      else
      {
          $this->_helper->redirector('login','index');
      }       
     

}

在模特中我有

  

class Model_DbTable_Login扩展Zend_Db_Table_Abstract
  {
         public function insertData($ un,$ pwd)

     

{
        $ data = array(
        '用户名'=&GT; $取消,
        '密码'=&GT; $ PWD
        );
                 $ data2 = array(         '名称'=&GT; $名称,         '生日'=&GT; $ bdate,                  'PHONENO'=&GT; $ PHONENO,         '地址'=&GT; $添加,         '资格'=&GT; $ QF,         'joiningdate'=&GT; $ jdate,         '日期ENDINGDATE'=&GT; $ EDATE,         '性别'=&GT; $ GN,         'maritalstatus'=&GT; $毫秒,                 '指定'=&GT; $ DESG,         '电子邮件'=&GT; $电子邮件,         'pancardno'=&GT; $锅,         '状态'=&GT; $状态         );

  try{  
                  //here i m inserting data in login table.
      $result=$this->insert($data);
                   // now here i want to insert data in profile table
                   $profile=$this->insert($data2)
  }  
     

捕获(例外$ e){
        echo“
”。$ e;退出;
       }
    }

那么如何在登录表中插入数据时在配置文件表中插入数据?

2 个答案:

答案 0 :(得分:2)

好吧Jigar让我愚蠢到我的水平(我想你可能接近我)。

您打算在应用中使用的每个表都需要一个DbTable模型。

dbTable模型的最低要求(所有类使用ZF 1.11默认名称和路径):

class Application_Model_DbTable_Login extends Zend_Db_Table_Abstract
{
    //$_name = name of table, not reuired if classname = tablename, but good idea
    protected $_name = 'login';
    //$_primary = primary key column of table, good idea for easy reference.
    protected $_primary = 'user_id';

    //save function added for brevity
    //I like to use the save() method over insert() because for a single row 
    //I can save and update with the same method...easily.
 public function saveUser(array $data) {
    //convert array to standard object for convience
    $dataObject = (object) $data;
    //check if user_id array key exists and isset in original data, if exist will update
    if (array_key_exists('user_id', $data) && isset($data['user_id'])) {
        $row = $this->find($dataObject->id)->current();
    } else {
        //if user_id not set or present in array we create a new row
        $row = $this->createRow();
    }

    $row->username = $dataObject->username;
    $row->password = $dataObject->password;
    $row->type     = $dataObject->type;
    //save or update row
    $row->save();
    //return the whole row object, we'll use it to save data to 'profile'
    return $row;
  }
}

//I would change the profile table to use the user_id as a 'natural' key, this requires adding
//a third property $_sequence = false.
class Application_Model_DbTable_Profile extends Zend_Db_Table_Abstract
{
    //$_name = name of table, not reuired if classname = tablename, but good idea
    protected $_name = 'profile';
    //$_primary = primary key column of table, good idea for easy reference.
    protected $_primary = 'user_id';
    //primary key auto-increment? True for yes false for natural key
    protected $_sequence = FAlSE;

    public function saveUserProfile(array $data, $id) {
    //convert array to standard object for convience
    $dataObject = (object) $data;
    //the user_id will always exist when we deal with the profile
    $row = $this->find($id)->current();
    if (!row)) {
        $row = $this->createRow();
        $row->user_id  = $id;
    } 

    $row->name      = $dataObject->name;
    $row->address   = $dataObject->email;
    $row->phone     = $dataObject->phone;//continue adding fields
    //save or update row
    $row->save();
    //return the whole row object, we'll use it to save data to 'profile'
    return $row;
  }
 }
}

现在用于简单的控制器操作来获取我们想要保存的数据。我们将使用默认的IndexController / indexAction作为示例。

class IndexController extends Zend_Controller_Action {


    public function indexAction() {
        $form = new Form(); //add your form here
        //this assumes the use of a Zend_Form object. For other form types use $this->getRquest()->getParams();
        if ($this->getRequest()->isPost() {
            if ($form->isValid($this->getRequest()->getPost()){
            $formData = $form->getValues(); //returns array of filtered/validated form values
           $model = new Application_Model_DbTable_Login();
           //pass the whole array to the saveUser() correct the data in the model.
           $user = $model->saveUser($formData);
           //$user returns the row object we just saved
           $profile = new Application_Model_DbTable_Profile();
           $profile->saveUserProfile($formData, $user->user_id);
      }
    }
    //assign form to view
    $this->view->form = $form;
  }
}

这是一个简单的例子,绝不符合最佳实践,但该技术有效。如果使用多种形式,indexAction()中存在的代码模式将变得非常熟悉。

我可能会将控制器中的两个DbTable调用组合成第三个模型,例如:Application_Model_User

这里要记住的是:在视图/控制器中过滤和验证用户数据,然后规范化模型中的数据。您可以在任何地方传递相同的数据数组,只需在此处稍微提取一下即可。

Rob的答案比我的更正确,但它确实包含了我们中的一些不成熟/初学者程序员还没有完全理解的概念。 :)

祝你好运。

答案 1 :(得分:1)

创建一个User实体,其中包含两个表所需的所有属性。还要创建一个用于加载和保存用户实体的service对象。

服务器对象(比如UserService)应该为每个表都有两个属性。您可以使用您编写的两个映射器对象执行此操作,也可以使用两个Zend_Db_Table个对象。

在名为UserService的{​​{1}}上编写一个方法,然后为每个表提取正确的属性,并调用相关的表对象进行插入或更新。

同样,创建一个SaveUser($user),它允许您实例化一个User实体并从两个表网关对象中填充其属性。

这是一些示例代码,显示了我的意思。显然它不是生产准备好了!

UserService::LoadUser($id)