Zend_Db,如何使用相关表?

发布于 2024-09-27 17:36:46 字数 1236 浏览 11 评论 0原文

我想学习如何使用ZF中的相关表直到最后。 (1) 谁能帮忙解决这个问题? 有 2 个users 表和 *users_openid* 以及指向许多的链接。我想在 Zend_Db 的表之间实现关系,以便这些用户在 Drugs findDependentRowset 上使用 openid,添加 openid,openid 通过 findParentRow 来引导用户...

该手册是俄语帮助...但不像我无法处理它....( http://framework .zend.com/manual/en/zend.db.table.relationships.html

数据库的结构:

- 
- Table structure `users` 
- 

CREATE TABLE IF NOT EXISTS `users` ( 
`Id` int (11) UNSIGNED NOT NULL AUTO_INCREMENT, 
`Nickname` varchar (200) NOT NULL, 
`Email` 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另一个小podvoprosik(2),用它你可以创建到MySQL(ubuntu)的连接,工具有phpmyadmin和mysql管理员,但是那里我还没有找到怎么做,但是又不想写sql(

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, 
`Email` 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 (

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小女人ら 2024-10-04 17:36:46

回答您的问题,如何创建与数据库的连接?

我知道使用 Zend 连接数据库的两种方法。

  • 使用 zf 配置 db-adapter,然后检索数据库资源

// Initialize and retrieve DB resource
$bootstrap = $application->getBootstrap();
$bootstrap->bootstrap('db');
$dbAdapter = $bootstrap->getResource('db');
?>

// 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'
        ));
?>

在您的 SQL 中,您没有从一个表到另一个表的任何引用...我的参考虚数?无论如何...

<?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 = '[email protected]';
$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');

}
?>

或者,查看有关创建选择的 Zend 文档 。您可以在模型中创建选择并加载数据。

矢田

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');
?>

<?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 = '[email protected]';
$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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文