Php Zend framework sample source code

blahboatsInternet and Web Development

Dec 13, 2013 (3 years and 6 months ago)

158 views

Php Zend framework sample source code



<?php

/**


* This is member controller class

with register action,

which handles the request of '/member/register'.


* It will display error message in case of form validation

erro
r
, otherwise it would redirect to
'profile' page.


*/

class MemberController extends Zend_Controller_Action

{


public function init()


{


$this
-
>_helper
-
>layout
-
>setLayout('register');


}




public function registerAction()


{


require_once APPLICATION_
PATH . '/modules/default/forms/Register.php';


$request = $this
-
>getRequest();


$form = new Form_Register();


if ($
r
equest
-
>isPost()) {


if ($form
-
>isValid($request
-
>getPost())) {


require_once
APPLICATION_PATH . '/modules/default/models/Member.php';


$member = new Member();


$member
-
>register($request
-
>getPost());


$_SESSION['member'] = $member;


return $this
-
>_r
edirect('/member/profile');


}


}


// assign the form to the view


$this
-
>view
-
>form = $form;


}

}
<?php

/**


* This is member model class, which has register() function to save registration info to db,


* and addProfile() function to save profile to db.


*/

require_once APPLICATION_PATH . '/../modules/default/models/Tool.php';


class Member

{


public $id;


public $name;


public $gender;


public $age;


public $basic = null;


pub
lic $profile = null;


public $requirement = null;


public $photo = null;



/**


* It adds member basic information to db


* @param $valueArr an array to contain all member basic information


* @return boolean the result of saving d
ata to db


*/


public function register($valueArr)


{


try {


$id = (string)time() . (string)rand(100, 999);


$valueArr['id'] = $id;


$valueArr['sysFlag'] = 1;


$result = Tool::insert(‘Memb
erBasic’, $valueArr);


If (empty($result)) throw new Exception(‘fail to register new member’);


return true;


} catch(Exception $e) {


Zend_Registry::get('logger')
-
>log($e, Zend_Log::ERR);


return false;



}


}




/**


* It saves member profile information to db


* @param $valueArr an array to contain all profile information


* @return boolean the result of saving data to db


*/


public function addProfile($valueArr)


{


$dbCon = Zend_Registry::get('dbAdapter');


$dbCon
-
>beginTransaction();


try {


$id = $this
-
>id;


$valueArr['id'] = $id


$result1 = Tool::insert('MemberProfile', $val
ueArr, $dbCon, ‘rowNum’);


$result2 = Tool::update(‘MemberProfile’, array('sysFlag'=>'2'), array(‘id’=>$id), $dbCon, ‘rowNum’);


If (($result1 == 1) && ($result2 == 1) {


$dbCon
-
>commit();



} else {


throw new Exception(‘fail to add profile’);


}


$dbCon
-
>closeConnection();


return true;


} catch(Exception $e) {


Zend_Registry::get('logger')
-
>
log($e, Zend_Log::ERR);


$dbCon
-
>rollBack();


$dbCon
-
>closeConnection();


return false;


}


}


}