简体   繁体   中英

Zend Framework Tableprefixes and joins failing

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 )

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM