我如何将Criteria :: LIMIT应用于上限和下限,还是有另一种方式?

时间:2011-09-08 12:37:45

标签: php symfony1 propel

我在indexSucces.php中有这段代码

    //some html for the main page
    <td width='48%' align='right'> 
       <?php include_partial('login', array('form' => $form)) ?>
    </td>

在actions.class.php索引我有:

    public function executeIndex()
    {
    $this->form = new RcLoginForm();
    $this->setTemplate('index');
    $this->search_form = new RcSearchForm();
        $this->age_form = new RcAgeTableForm();
    }

    public function executeListmatches(sfWebRequest $request)
    {

    }   

然后是部分_login.php代码:

   <form action="<?php echo url_for('password/listmatches' ) ?>"  method="post" >
   <tr>
    <td colspan="2">
        <span class='spn_med_lightblue_rbc'>I am a:</span>  
        <select id="gender1" name="gender1">
            <option value="male1" >Male</option>
            <option value="female1" selected="selected">Female</option>
        </select>  
    </td>
  </tr>
  <tr>
    <td colspan="2">
        <span class='spn_med_lightblue_rbc'>Seeking a:</span>  
        <select id="gender2" name="gender2">
            <option value="male2" >Male</option>
            <option value="female2" selected="selected">Female</option>
        </select>  
    </td>
  </tr>
  <tr>
    <td>
        <span class='spn_med_lightblue_rbc'>Age:</span>  
        <select id="age1" name="age1">
        <?php $ages = RcAgeTablePeer::getById(18); 
            foreach ($ages as $age)
            { 
                //echo $age->getId();
                echo "<option>";
                echo $age->getId();
                echo "</option>";
            }  ?>
        </select>
    </td>
    <td>
        <span class='spn_med_lightblue_rbc'>To:</span>  
        <select id="age2" name="age2">
            <?php foreach ($ages as $age)
            { 
                //echo $age->getId();
                echo "<option>";
                echo $age->getId();
                echo "</option>";
            }  ?>
        </select>  
    </td>
  </tr>
  <tr> 
    <td colspan="2">
        <span class='spn_med_lightblue_rbc'>Location:</span>  
        <select id="province" name="province">
            <option value="all" selected="selected">All</option>
            <option value="ec">Eastern Cape</option>
            <option value="nc">Northern Cape</option>
            <option value="wc">Western Cape</option>
            <option value="fs">Free State</option>
            <option value="gp">Gauteng</option>  
            <option value="kzn">Kzn</option>
            <option value="lim">Limpopo</option>
            <option value="mpu">Mpumulanga</option>  
            <option value="nw">North West</option>  
        </select>  
    </td>  
    <td><input class='submit_img' type="image" src="/images/rainbow/gobuttonbluesmall.png" value="Submit" alt="Submit"></td>
</tr>
<tr></tr>
<tr></tr>
</form>

以上将给出一个主页,其中包含一个部分(部分_login),其中包含网站的登录信息,当您点击go按钮时,快速搜索表单操作将转到 行动=”

这是我的困境..我不在任何班级,我只是通过POST传递选定的值并使用这些值来获取我的行

listmatchesSuccess.php中的

   $matching_rows = RcProfileTablePeer::getAllBySelection($gender_id2,$age1,$age2,$province_id,$from,$limit);

上面的RcProfileTablePeer.php:

 static public function getAllBySelection($gender2,$age1,$age2,$province_id,$from,$limit)
{
    $criteria = new Criteria();
    $criteria->add(RcProfileTablePeer::AGE,$age1,Criteria::GREATER_EQUAL);
    $criteria->add(RcProfileTablePeer::AGE,$age2,Criteria::LESS_EQUAL);
    $criteria->add(RcProfileTablePeer::GENDER_ID,$gender2, Criteria::EQUAL);
    if ($province_id <> 10)
        $criteria->addAnd(RcProfileTablePeer::PROVINCE_ID,$province_id, Criteria::EQUAL);
    $criteria->setLimit($limit);
    return self::doSelect($criteria);
}

希望这一切现在更有意义:) 谢谢

2 个答案:

答案 0 :(得分:1)

我希望我理解你在问什么,

我想为查询添加限制

$criteria->setLimit(10);

如果你想在symfony中创建一个寻呼机,我建议推进寻呼机类 例如     $ this-&gt; page = $ request-&gt; getParameter('page',1);     $ c = new Criteria();

$c->add(UserPeer::Status_ID,1);

$this->pager = new sfPropelPager('User', $limit);
$this->pager->setCriteria($c));
$this->pager->setPeerMethod('doSelect');
$this->pager->setPage($this->page);
$this->pager->init(); 

您可以参考此链接http://www.symfony-project.org/cookbook/1_0/en/pager以获取更多信息。

我希望能回答你的问题;

答案 1 :(得分:0)

您的查询中存在错误:

$criteria->add(RcProfileTablePeer::AGE,$age1,Criteria::GREATER_EQUAL);
$criteria->add(RcProfileTablePeer::AGE,$age2,Criteria::LESS_EQUAL);

结果查询rc_profile.age >= :age1

如果您正在寻找年龄介于$ age1和$ age2之间的物品,这应该可以解决问题:

$criteria->add(RcProfileTablePeer::AGE,$age1,Criteria::GREATER_EQUAL);
$criteria->addAnd(RcProfileTablePeer::AGE,$age2,Criteria::LESS_EQUAL);

结果查询rc_profile.age >= :age1 AND rc_profile.age <= :age2

请注意,您应该使用$criteria->addAnd$criteria->addOr来组合相同列的条件。