Zend DB Select with multiple table joins

ε祈祈猫儿з 提交于 2019-12-11 01:57:31

问题


Trying to replicate the following query using Zend_Db_Select. Any pointers?

SELECT 
  compounds.id as compounds_id,
  reactions.id as reactions_id, 
  reaction_compound.number as reaction_compound_number  
FROM compounds, reactions, reaction_compound 
WHERE  
  compounds.id IN (68,74,112) 
  AND compounds.id = reaction_compound.compound  
  AND reactions.id = reaction_compound.reaction;

Specifically some issues I'm running into are doing multiple table joins in Zend. I'm not sure how to do the join across multiple tables using their query builder.

Any help is appreciated!

J


回答1:


Something the likes of:

$compoundIds = array(68,74,112);
$select = $db->select()
   ->from('compounds', array('compounds_id' => 'id')
   ->where('compounds.id in ( ? )', $compoundIds)
   ->join('reaction_compound', 'compounds.id = reaction_compound.compound', array('reaction_compound_number' => 'number'))
   ->join('reactions', 'reactions.id = reaction_compound.reaction', array('reaction_id' => 'id');

That should get you somewhere. I didn't test it, so there might be some errors in there.




回答2:


The format:

$db->select()->from('table_name')->join('...')->join('...')

Works great!
Thank both of you posters for this info! Helped me solve quite a doozy! (Abstract Table Models oh my!!) Lol

Ps: To others about to utilize the same format detailed above remember that column names will be overwritten by latter tables with same column names. Happy Coding!



来源:https://stackoverflow.com/questions/1884313/zend-db-select-with-multiple-table-joins

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!