5

「login」と「profile」の2つのテーブルがあります。login tableが含まれていますuser_id, username, password, typeprofile tableが含まれていますprofile_id, user_id, name, address, phone_no,email,status,pancardno,gender,birthday,joiingdate,endingdate。ここでは、 .Formにユーザー名、パスワード、タイプ、名前、住所、電話番号が含まれているため、使用user_idしています。reference keyでは、Zend Frameworkの「ログイン」テーブルにユーザー名、パスワード、タイプを挿入し、プロファイルテーブルにその他のフィールドを挿入するにはどうすればよいですか。
これが私のコントローラーコードです。

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 extends Zend_Db_Table_Abstract
{
public function insertData($ un、$ pwd)

{
$ data = array(
'username' => $ un、
'password' => $ pwd
);
$ data2 = array('name' => $ name、'birthdate' => $ bdate、'phoneno' => $ phoneno、'address' => $ add、'qualification' => $ qf、'joiningdate' => $ jdate、'endingdate' => $ edate、'gender' => $ gn、'maritalstatus' => $ ms、'designation' => $ desg、'email' => $ email、'pancardno' => $ pan 、'ステータス'=> $ status);

  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)
  }  

catch(exception $ e){
echo"
"。$e; exit;
}
}

では、ログインテーブルにデータを挿入しながらプロファイルテーブルにデータを挿入するにはどうすればよいですか?

4

2 に答える 2

2

わかりました、ジガーはこれを私のレベルまで下げさせてくれました(あなたはおそらく私の近くにいると思います)。

アプリで使用するテーブルごとに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()に存在するコードパターンは、多くのフォームを操作する場合に非常によく知られています。

おそらく、コントローラー内の2つのDbTable呼び出しを、たとえば次のような3番目のモデルに結合しますApplication_Model_User

ここで覚えておくべきことは、ビュー/コントローラーでユーザーデータをフィルター処理して検証してから、モデルでデータを正規化することです。同じ配列のデータをどこにでも渡すことができ、こことそこに少しだけ抽出できます。

ロブの答えは私のものよりもはるかに正しいですが、それは私たちの一部のアマチュア/初心者プログラマーがまだ完全に理解していないという概念を含んでいます。:)

幸運を。

于 2012-04-22T07:16:19.690 に答える
1

User両方のテーブルから必要なすべてのプロパティを保持するエンティティを作成します。User エンティティの読み込みと保存に使用するサービスオブジェクトも作成し ます。

サーバー オブジェクト (たとえばUserService、 ) は、テーブルごとに 2 つのプロパティを持つ必要があります。これは、作成した 2 つのマッパー オブジェクトで行うことも、2 つのオブジェクトを使用することもできZend_Db_Tableます。

UserService呼び出されたメソッドをSaveUser($user)記述してから、各テーブルの正しいプロパティを抽出し、関連するテーブル オブジェクトを呼び出して挿入または更新を行います。

同様に、UserService::LoadUser($id)User エンティティをインスタンス化し、2 つのテーブル ゲートウェイ オブジェクトからそのプロパティを入力できる を作成します。

これは、私が意味するアイデアを示すサンプルコードです。明らかに、それは生産準備ができていません!

<?php

class User {
    public $user_id;
    public $username;
    public $password;
    public $profile_id;
    public $name;
    public $address;
    public $phone_no;

}

class LoginTable extends Zend_Db_Table_Abstract
{
    protected $_name = 'login';
}

class ProfileTable extends Zend_Db_Table_Abstract
{
    protected $_name = 'profile';
}

public UserService
{
    public function saveUser(User $user)
    {
        $loginTable = new LoginTable();
        $profileTable = new ProfileTable();

        $loginData = array(
            'username' => $user->username,
            'password' => $user->password,
        );
        $profileData = array(
            'user_id' => $user->user_id,
            'name' => $user->name,
            'address' => $user->address,
            'phone_no' => $user->phone_no,
        );

        if (!user->user_id > 0) {
            // updating
            $loginTable->update($loginData, 'user_id = '. (int)$user->user_id);
            $profileTable->update($profileData, 'profile_id = '. (int)$user->profile_id);
        } else {
            // inserting
            $user->user-id = $loginTable->insert($loginData);
            $profileData['user_id'] = $user->user_id;
            $profileTable->insert($profileData);
        }
    }
}
于 2012-04-19T18:14:49.487 に答える