Zend Framework 表前缀和连接失败

发布于 2024-10-14 02:50:03 字数 1234 浏览 8 评论 0原文

我想为不同客户的所有表设置动态前缀。假设我有客户 A,我的表将是“a_tablename”等。

这一切都工作正常,所有设置都很顺利,但是,当我在 Zend 框架中运行联接查询时,它会从默认表(“tablename”)中进行选择,而不是前缀。

这是我的代码片段:

$select = $hours->select()
  ->setIntegrityCheck(false)
  ->from(array('h' => 'hours'), array('id', 'type', 'remark', 'minutes', 'date'))
  ->join(array('p' => 'projects'), 'h.project_id = p.id', array('description' => 'description', 'order_nr' => 'order_nr'))
  ->join(array('c' => 'clients'), 'p.client_id = c.id', array('client' => 'name'))
  ->where($this->db->quoteInto('h.user_id = ?', $userId, 'INTEGER'))
  ->where($this->db->quoteInto('h.date >= ?', $startDate))
  ->where($this->db->quoteInto('h.date <= ?', $endDate))
  ->order(array('h.date', 'h.type'));
$rows = $hours->fetchAll($select);
$this->view->hours = $rows;

此联接采用标准表(客户),其中非联接查询选择客户“a_clients”表。我缺少什么? 这是我的类扩展:

抽象类 Zend_Db_Table 扩展了 Zend_Db_Table_Abstract { 函数_setupTableName() { 父级::_setupTableName(); $前缀='StackOverflow'; // 也许来自配置.. $this->name = $prefix 。 ''。 $这个->_name; } (StackOverflow当然

只是一个固定值,但它用于测试,我从这里复制粘贴它:P)

I want to set a dynamic prefix to all my tables for different customers. Say I got customer A, my tables would be "a_tablename" etc.

This all works fine, all setup goes well, but, when I run a join-query in the Zend framework, it selects from the default table ("tablename"), not the prefixed-one.

Here's a snippet of my code:

$select = $hours->select()
  ->setIntegrityCheck(false)
  ->from(array('h' => 'hours'), array('id', 'type', 'remark', 'minutes', 'date'))
  ->join(array('p' => 'projects'), 'h.project_id = p.id', array('description' => 'description', 'order_nr' => 'order_nr'))
  ->join(array('c' => 'clients'), 'p.client_id = c.id', array('client' => 'name'))
  ->where($this->db->quoteInto('h.user_id = ?', $userId, 'INTEGER'))
  ->where($this->db->quoteInto('h.date >= ?', $startDate))
  ->where($this->db->quoteInto('h.date <= ?', $endDate))
  ->order(array('h.date', 'h.type'));
$rows = $hours->fetchAll($select);
$this->view->hours = $rows;

This join takes the standard-table (clients), where as non-joined queries select the customers 'a_clients' table. What am I missing?
Here's my class-extension:

abstract class Zend_Db_Table extends Zend_Db_Table_Abstract
{
function _setupTableName()
{
parent::_setupTableName();
$prefix = 'StackOverflow'; // maybe from config..
$this->name = $prefix . '' . $this->_name;
}
}

(StackOverflow is offcourse just a fixed value, but it's for testing and I copy-pasted it from here :P )

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

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

发布评论

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

评论(1

别挽留 2024-10-21 02:50:03

我不确定这是否能解决您的问题,但是确实没有理由使用 Zend_Db_Table_Abstract 的实例来执行带有连接的查询 - 它只会把事情搞砸(您必须执行 setIntegrityCheck 的事情..这很奇怪)。

您最好从模型中获取表名,并使用通用数据库句柄:

$db = Zend_Registry::get('db');
// or, you might prefer:
$db =  Zend_Db_Table::getDefaultAdapter();
$db->select()->from(array('alias' => $hours->info('name')));

然后从那里开始。

I'm not sure if this will solve your problem, but there's really no reason to use an instance of Zend_Db_Table_Abstract to perform a query with joins - it just tends to screw stuff up (you have to do the setIntegrityCheck thing.. it's just weird).

You might be better off just getting your table names from your models, and using your generic DB handle:

$db = Zend_Registry::get('db');
// or, you might prefer:
$db =  Zend_Db_Table::getDefaultAdapter();
$db->select()->from(array('alias' => $hours->info('name')));

and go from there.

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