i have two tables users
and userslog
where i have users
contains id
, username
,email
,gender
and userslog
contains id
,firstname
,lastname
,designation
and u_id
which is foreign key of users table
.
my register controller
class RegisterController extends Zend_Controller_Action
{
public function init()
{
/* Initialize action controller here */
}
public function indexAction()
{
$form = new Application_Form_register();
$this->view->form = $form;
if($this->getRequest()->isPost())
{
$data = $this->getRequest()->getPost();
}
}
}
my register model is
class Application_Model_DBtable_register extends Zend_Db_Table
{
protected $_name = 'users';
// i have to define two tables here!! how?
}
my register zend form
class Application_Form_register extends Zend_Form
{
public function init()
{
$this->setName('register');
$this->setMethod('post');
$firstname = $this->createElement('text', 'firstname');
$firstname->setLabel('FirstName: ')
->setRequired(true)
->setFilters(array(
'stringTrim','StringToLower'));
$lastname = $this->createElement('text', 'lastname');
$lastname->setLabel('LastName: ')
->setRequired(true)
->setFilters(array(
'stringTrim','StringToLower'));
$email = $this->createElement('text', 'email');
$email->setLabel('Email: ')
->setRequired(true);
$username = $this->createElement('text', 'username');
$username->setLabel('UserName: ')
->setRequired(true)
->setFilters(array(
'stringTrim','StringToLower'));
$password = $this->createElement('password', 'password');
$password->setLabel('Password: ')
->setRequired(true);
$password2 = $this->createElement('password', 'password2');
$password2->setLabel('Confirm Password: ')
->setRequired(true);
$submit = $this->createElement('submit', 'register');
$submit->setLabel('Register')
->setIgnore(true);
$this->addElements(array(
$firstname,
$lastname,
$email,
$username,
$password,
$password2,
$submit));
}
}
how can i do it? can we use Zend_Auth for authentication? if yes then how.please give me example. I saw $_dependantTable
and $_referenceMaps
but i can't undestand it . I am confused how to do it because i am not able to define table name more than once in a class. i have to insert the values at same time in two different tables.
ok this is incorrect:
class Application_Model_DBtable_register extends Zend_Db_Table
{
protected $_name = 'users';
// i have to define two tables here!! how?
}
The intent of classes that have DbTable in their names is that they they are the adapter/gateway to a single database table.
So your issue would breakdown into at least 2 classes/files:
class Application_Model_DBtable_Users extends Zend_Db_Table
{
protected $_name = 'users';
}
class Application_Model_DBtable_Userslog extends Zend_Db_Table
{
protected $_name = 'userslog';
}
I would at this level put a method in either class that does the actions required to register a user. Later you may decide to use mappers and domain objects.
class Application_Model_DBtable_Users extends Zend_Db_Table
{
protected $_name = 'users';
/*
* psuedocode not meant for production
*/
public function register($array $data) {
//sort out the data
$user = array();//put the elements from the $data array that you want to go into the users table
$userLog = array(); //same thing for the userslog table
//get userslog adapter
$userlogTable = new Application_Model_DbTable_Userslog();
//do the first insert
$result = $this->insert($user);
//insert returns the primary key of the row created,
$u_id = array('u_id' => $result);
$userLog = array_merge($u_id, $userLog); //merge u_id with existing data
//perform second insert
$result2 = $userlogTable->insert($userLog);
//handle any errors
}
}
This should provide a basic example to demonstrate the steps you might take to implement you design. The point behind the DbTable models is to abstract the connection to a given table, each table has it's own DbTable model. The DbTable model provides an adapter to each table and access to the api provided by Zend_Db_Table_Abstract.
The Steps shown above could just as easily be taken in a third model or in the controller itself.
Note: There may be away to do this with one sql statement but I'm not the person who knows that answer.