Join tables into a model in Zend Php

I understand the use of Zend_Table and can get data using Zend functions from the table associated with this class. For example, I have a video table, and in another table I have a connection between the video and what category it is in.

It’s a little exaggerated how to activate the selection within the framework:

 SELECT * FROM video,category WHERE category.category_id = 3 AND video.id = category.video_id 

I want to do this in a video model that refers to a video table.

+2
source share
2 answers

Here is a rude class. It uses the Zend db adapter directly, so the zend_db_table object is not aware of the relationship, but it works.

 class Video extends Zend_Db_Table { public function doQueryWithSql($id) { $qy = " SELECT * FROM video,category WHERE category.category_id = $id AND video.id = category.video_id "; return $this->getAdapter()->fetchAll($qy); } public function doQueryWithObject($id) { $select = $this->getAdapter()->select(); $select->from(array('v'=>'video')) ->join(array('c'=>'category'),'v.id = c.video_id') ->where("c.category_id = $id"); return $this->getAdapter()->fetchAll($select); } } 
+1
source

From what you published, it seems that there is a relationship between the tables for videos and categories: in the category there are many videos and the video belongs to the category. You should check this article for a Zend_Db_Table relationship.

0
source

Source: https://habr.com/ru/post/1305402/


All Articles