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


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


          網(wǎng)站導航:
           

          導航

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

          統(tǒng)計

          常用鏈接

          留言簿(7)

          隨筆分類

          隨筆檔案

          相冊

          Photo

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 甘孜县| 桂林市| 江山市| 华阴市| 栾城县| 江油市| 六安市| 光泽县| 贵阳市| 江川县| 宜兰县| 景洪市| 凤山县| 广宁县| 吉安县| 滦南县| 萝北县| 莎车县| 息烽县| 福州市| 太原市| 高陵县| 松滋市| 车致| 班戈县| 明水县| 宁强县| 拜泉县| 普宁市| 塔城市| 沂南县| 五寨县| 仙游县| 威远县| 三都| 邹平县| 秭归县| 广安市| 泸西县| 两当县| 玛纳斯县|