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 閱讀(539) 評論(0)  編輯  收藏 所屬分類: PHPSymfony


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


          網站導航:
           

          導航

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

          統計

          常用鏈接

          留言簿(7)

          隨筆分類

          隨筆檔案

          相冊

          Photo

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 鄱阳县| 崇州市| 中西区| 民和| 泉州市| 永和县| 隆安县| 大同县| 武穴市| 满城县| 仙游县| 云南省| 曲周县| 信丰县| 滕州市| 梧州市| 文昌市| 隆昌县| 象州县| 思茅市| 蒙山县| 栖霞市| 涟水县| 红桥区| 小金县| 仁怀市| 广昌县| 新源县| 咸宁市| 枣强县| 高陵县| 安吉县| 赤水市| 镶黄旗| 自贡市| 安新县| 安丘市| 海南省| 措美县| 萨迦县| 宝鸡市|