pear 里的joinAdd()咋个用?
怎么返回menu表的 menuText 字段的值?我要执行这么一条语句:
SELECT *
FROM news
LEFT JOIN menu ON menu.menuID = news.menuID
WHERE menu.menuID =1
ORDER BY newsID DESC
LIMIT 0 , 20$news = new D_News();
$menu = new D_Menu();
$news->query("set names utf8");
//$news->debugLevel(5);
//$news->joinAdd($news);
$news->whereAdd("menuID=2");
$news->orderBy("newsID desc");
$news->limit("0,7");
$news->find();
$i = 0;
$NewsListArray = array();
while ($news->fetch()) {
$NewsListArray[$i]['newsTitle'] = $news->newsTitle;
$NewsListArray[$i]['menuID'] = $news->menuID;
$NewsListArray[$i]['newsID'] = $news->newsID;
//$NewsListArray[$i]['menuText'] = $menu->menuText;
$i++;
}

解决方案 »

  1.   

    void $DB_DataObject->joinAdd ([object $dataobject [, string $joinType [, string $joinAs [, string $joinCol]]]])
    通过添加另一个DataObject实体类的实例创建连接查询。不带参数则清空当前连接查询的相关子句。
    第一个参数是要用来进行连接查询的实体。
    第二个参数是连接查询的类型,分别有左连接,右连接,内连接。
    $joinAs 当你在连接时需要从另一个表中取得多个字段,又觉得写表名太长麻烦的话,可以用这个参数起个别名
    $joinCol 当前实体(表)与子实体进行连接匹配的字段。如果当前实体与子实体有多个字段相关联,那么必须要指定这个参数的值。
    实例:
    $i = new DataObject_Image();
    $pi = new DataObjects_Product_image();
    $pi->product_id = 24; // set the product id to 24
    $i->joinAdd($pi); // add the product_image connectoin
    $i->find();
    while ($i->fetch()) {
    $aR[] = $i -> toArray();

    相当于下列SQL语句:
    SELECT * FROM image,product_image 
    LEFT JOIN image ON product_image 
    WHERE product_image.id = 24 
    AND product_image.pid = product.id