I am looking to execute this statement via Zend Framework. As I understand it, I can use Zend_Db_Select. Is it possible to use Zend_Db_Table?
Three tables: classes, students, and class_stu开发者_Python百科dents
select classes.name, students.student_id, students.fname, students.lname from students, classes, class_students where class_students.student_id=students.student_id AND class_students.class_id=classes.class_id;
yes, is possible - Zend_Db_Table provides you with an interface to execute a variety of operations on a table. For example, considering the multiple table selection that you want to perform, and assuming you have your db adapter properly configured, we can end up with something like the following:
$table = new Model_DbTable_Classes(); // which extends Zend_Db_Table_Abstract
$select = $table->select()->setIntegrityCheck(false);
$select->join('class_students', 'class_students.class_id = classes.class_id')
->join('students', 'student.student_id = class_students.student_id')
->where('classes.class_id = ?', 1)
->where('student.student_id = ?', 10);
$result = $table->fetchAll($select);
print_r($result->toArray());
For this particular case I wouldn't use the Zend_Db_Table though, I tend to use Zend_Db_Table when I need just to take actions upon one single table. As for multiple table selections I'd rather go with db select, or structuring my query old-school-fashioned (SQL string), and fetch it using my db object.
Hope it helps :)
M.
精彩评论