Tao

          Tao obeys its own inherent Nature

          How To: Using symfony to add a join between tables

          1. simple join two tables

          purpose:
          generate sql like:   

          select * from photo p
              left join artist a on p.artist_id = a.artist_id
                where a.genre = 'something' and p.genre = 'something'
          

          code:           

          if(!CriteriaUtil::hasJoin($criteria, ArtistPeer::TABLE_NAME)){
              $criteria->addJoin(PhotoPeer::ARTIST_ID, ArtistPeer::ARTIST_ID, Criteria::LEFT_JOIN);
          }
          $criteria->add(ArtistPeer::GENRE, $genre);    
          $criteria->add(PhotoPeer::GENRE, $genre);

          2. join two tables, add AND OR between conditions
          purpose:
          generate sql like:    

          select * from photo p
              left join artist a on p.artist_id = a.artist_id
                where (a.genre = 'some' or p.genre='something')
                  and a.name = 'something'

          code:   

          if(!CriteriaUtil::hasJoin($criteria, ArtistPeer::TABLE_NAME)){
             $criteria->addJoin(PhotoPeer::ARTIST_ID, ArtistPeer::ARTIST_ID, Criteria::LEFT_JOIN);
          }
          $criteria->add(ArtistPeer::GENRE, $genre);
          $c = $criteria->getCriterion(ArtistPeer::GENRE);
          if($c != null){
             $c->addOr($criteria->getNewCriterion(PhotoPeer::GENRE, $genre));
          }
          $criteria->add(ArtistPeer::NAME, $name);

           

          Note:
          It's a good habit to check if we have joined the table already. to check this, you can use the following util class, it get all the joined tables, and check if the table exists in them.

          class CriteriaUtil{
              public static function hasJoin($c, $table_name){
                  $joins = $c->getJoins();
                  if($joins != null){
                      foreach($joins as $join){
                          if($join->getRightTableName() == $table_name){
                              return true;
                          }
                          if($join->getLeftTableName() == $table_name){
                              return true;
                          }
                      }
                  }
                  return false;
              }
          }
          

          posted on 2007-12-05 17:26 wade 閱讀(534) 評論(0)  編輯  收藏 所屬分類: PHPSymfony


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          <2007年12月>
          2526272829301
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          統計

          常用鏈接

          留言簿(7)

          隨筆分類

          隨筆檔案

          相冊

          Photo

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 新和县| 阳春市| 丰顺县| 阳山县| 怀柔区| 肇州县| 鹤峰县| 田林县| 巫溪县| 张家界市| 景谷| 大英县| 罗山县| 枝江市| 西华县| 北宁市| 天水市| 汉中市| 义乌市| 南京市| 分宜县| 乡宁县| 贺州市| 南宫市| 潮州市| 天峻县| 昌邑市| 美姑县| 鄂温| 盐山县| 崇义县| 济阳县| 茶陵县| 阿克苏市| 灵丘县| 邯郸县| 二手房| 天峻县| 宜都市| 厦门市| 赤城县|