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) 評(píng)論(0)  編輯  收藏 所屬分類: PHPSymfony


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           

          導(dǎo)航

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

          統(tǒng)計(jì)

          常用鏈接

          留言簿(7)

          隨筆分類

          隨筆檔案

          相冊(cè)

          Photo

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 大厂| 尚志市| 临江市| 景洪市| 澄迈县| 广东省| 民丰县| 通许县| 胶州市| 洞头县| 新干县| 桃园市| 南召县| 湖口县| 长子县| 古浪县| 马关县| 河池市| 西城区| 大关县| 大同县| 遂宁市| 尼勒克县| 柳州市| 彭阳县| 长春市| 望城县| 增城市| 定边县| 辛集市| 饶阳县| 灌云县| 漳平市| 铜陵市| 济源市| 分宜县| 成安县| 库尔勒市| 曲靖市| 定安县| 阜新市|