I want to learn to work with related tables in the ZF to the end. (1) Can anyone help with this? there are 2 users table and *users_openid* with a link to many. I would like to implement a relationship between the tables at Zend_Db so that such users from taking it openid on Drugs findDependentRowset, add openid, openid to take the user through findParentRow ...
The manual is a Russian HELP ... but not like I can not deal with it ....( http://framework.zend.com/manual/en/zend.db.table.relationships.html
The structure of the database:
-
- Table structure `users`
-
CREATE TABLE IF NOT EXISTS `users` (
`Id` int (11) UNSIGNED NOT NULL AUTO_INCREMENT,
`Nickname` varchar (200) NOT NULL,
`Em开发者_开发问答ail` varchar (200) NOT NULL DEFAULT'',
`Active` tinyint (1) NOT NULL DEFAULT '0 ',
`Enabled` tinyint (1) NOT NULL DEFAULT '0 ',
PRIMARY KEY (`id`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8 AUTO_INCREMENT = 1;
- ------------------------------------------------ --------
-
- Table structure `users_openid`
-
CREATE TABLE IF NOT EXISTS `users_openid` (
`User_id` int (11) UNSIGNED NOT NULL,
`Openid` varchar (200) NOT NULL,
PRIMARY KEY (`user_id`),
KEY `openid` (`openid`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8;
PS another small podvoprosik (2), with which you are creating a connection to MySQL (ubuntu), the tools have phpmyadmin and mysql administrator, but there I have not found how to do it, but do not want to write sql (
To answer your question, How to create connection to database?
I know 2 ways how to get connection to database using Zend.
- Configure db-adapter using
zf
and then retrieving the db resource
<?php
// Initialize and retrieve DB resource
$bootstrap = $application->getBootstrap();
$bootstrap->bootstrap('db');
$dbAdapter = $bootstrap->getResource('db');
?>
- Creating Zend_Db Adapter from code
<?php
// Get DB resource
// You can also use Zend_Db_Adapter_Pdo_Mysql
$db = Zend_Db::factory('PDO_MYSQL', array(
'host' => 'localhost',
'username' => 'user',
'password' => 'password',
'dbname' => 'database'
));
?>
In your SQL you do not have any references from one table to another... I the reference imaginary? Anyways...
<?php
class Users extends Zend_Db_Table_Abstract
{
protected $_name = 'users';
//
//protected $_dependentTables = array('UsersOpenids');
}
class UsersOpenids extends Zend_Db_Table_Abstract
{
protected $_name = 'users_openid';
protected $_referenceMap = array(
'User' => array(
'columns' => 'User_id',
'refTableClass' => 'Users',
'refColumns' => 'Id'
)
);
}
//
$openIdsTable = new UsersOpenids();
// Get only 1st Row
$openId = 'emails@gmail.com';
$openIdRow = $openIdsTable->fetchRow(array('Openid = ?' => $openId));
if ($openIdRow === null){
throw new Exception("OpenId is not found");
}else{
// Find parent row, will use current value of Zend_Db_Table_Row_Abstract
// Will get user who is associated with the open id
// $openIdRow->findParentRow($parentTable, $ruleKey, $select)
$user = $openIdRow->findParentRow('Users', 'User');
}
?>
Alternatively, look at Zend documentation on creating selects. You can create a select in your model and load the data.
YDACHI
精彩评论