简体   繁体   中英

Yii HAS_MANY relation in search

I would like to create a search form. My price ranges are:

public static function getPrice_list(){
    $model = array(
        0=>"0 - 250.000",
        1=>"250.000 - 600.000",
        2=>"600.000 - 1.000.000",
        3=>"1.000.000 -",
        4=>"Every",

    );
    return $model;
}

The advert model relation is:

'price'=>array(self::HAS_MANY, 'UserAndApartmanPrice', 'user_and_apartman_id')

Every advert has 4 price: 1., defalult valuta for sell. 2., choosen valuta for sell. 3.,default valuta for rent. 4., chosen valuta for rent. 3 and 4 have same value in database. main = 10

So i need something like:

$criteria = new CDbCriteria;
$criteria->with = array( 'apartman','price');
$criteria->together = true;
if($_POST['sell_price'] != 4){
   if($_POST['sell_price'] == 0){
       // this is the question
   }
      ...
}

$model = UserAndApartman::model()->findAll($criteria); 

So i would like to search in a HAS_MANY realation, where main != 10 AND valuta = 1 AND value between 2 price .

Hopefully I got right your table model and table schema. Criteria should look somewhat like that:

$criteria = new CDbCriteria;
    $criteria->together = true;
    $list = UserAndApartman::getPriceList(0);
    $criteria->with = array(
        'description' => array(
            'condition' => 'main != :main AND valuta = :valuta AND value BETWEEN :value1 AND :value2',
            'params'    => array(
                ':main' => 10,
                ':valuta' => 1,
                ':value1' => $list[0],
                ':value2' => $list[1],
            )
        )
    );

Function UserAndApartman::getPriceList() :

public static function getPriceList( $id ) {
    $list = array(
        0 => array("0", "250.000"),
        1 => array("250.000", "600.000"),
        2 => array("600.000", "1.000.000"),
        3 => array("1.000.000", '9999999999999'),
        4 => array('0', '9999999999999'),
    );

    return $list[$id];
}

Dont forget to change 0 in $list = UserAndApartman::getPriceList(0); into id of value range that you want to retrieve.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM