[英]trying to create a 'join' statement using Zend DB Table - zend framework 2
我有一個模型需要在2個表上執行連接查詢...讓我們稱之為friend_list和user_profile。
我有一段時間試圖將zend樣式代碼放在一起以產生我需要完成的正確查詢...下面是所需的查詢:
SELECT friend_list.friend_id, user_profile.id, user_profile.username
FROM `friend_list`
INNER JOIN `user_profile`
ON friend_list.friend_id = user_profile.id
where user_id = 1
這是我的model_friends
<?php
//model created to add user to database, sendmail etc...
require_once 'Zend/Db/Table/Abstract.php';
class Model_Friends extends Zend_Db_Table_Abstract
{
protected $_name = "friend_list";
public function fetchFriendList($userID)
{
$accountsTable = array('up' => 'user_profile');
$select = $this->select()
->from($this->_name)
->join($accountsTable, 'up.id = friend_List.friend_id', array())
->where("up.id = ?", $userID);
$result = $this->fetchAll($select);
if ($result !== null){
echo $select;
return $result;
} else {
echo "no records found";
}
}
}
上面的模型產生了以下SQL語句,這不是我想要的......
SELECT `friend_list`.*
FROM `friend_list`
INNER JOIN `user_profile`
AS `up`
ON up.id = friend_List.friend_id
WHERE (up.id = '1')
按要求添加表結構:
DROP TABLE IF EXISTS `buzz`.`friend_list`;
CREATE TABLE `buzz`.`friend_list` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`friend_id` int(11) NOT NULL,
`approved_timestamp` date NOT NULL,
`status` varchar(15) DEFAULT 'pending',
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `buzz`.`user_profile`;
CREATE TABLE `buzz`.`user_profile` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`mob` varchar(50) NOT NULL DEFAULT 'no info',
`dob` varchar(50) NOT NULL DEFAULT '',
`yob` varchar(50) NOT NULL DEFAULT '',
`language` varchar(75) NOT NULL DEFAULT 'English',
`gender` varchar(25) NOT NULL DEFAULT 'no info',
`about` varchar(1000) NOT NULL DEFAULT 'no info',
`country` varchar(45) NOT NULL DEFAULT 'no info',
`username` varchar(45) NOT NULL,
PRIMARY KEY (`id`,`username`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8;
嘗試將Zend_Db_Select對象更改為以下內容:
$select = $this->select()
->join($accountsTable, 'friend_list.friend_id = user_profile.id', array())
->where('user_profile.id = ?', $userID)
->reset('columns')
->columns(array('friend_list.friend_id', 'user_profile.id', 'user_profile.username'));
這不是問題的答案,但由於我無法發表評論,我將在此發布。 我發現以下網站對加入示例很有幫助。
我的model_friends腳本的最終結果如下:
<?php
//model created to add user to database, sendmail etc...
require_once 'Zend/Db/Table/Abstract.php';
class Model_Friends extends Zend_Db_Table_Abstract
{
protected $_name = "friend_list";
public function fetchFriendList($userID)
{
$select = $this->select()
->from($this)
->setIntegrityCheck(false)
->join(array('u'=>'user_profile'), 'friend_list.friend_id =u.id', array())
->columns(array('u.id', 'u.username'))
->where("friend_list.user_id = ?", $userID);
$result = $this->fetchAll($select);
if ($result !== null){
echo $select;
return $result;
} else {
echo "no records found";
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.