简体   繁体   中英

How to display value from other table using primary key

I have implemented my project in Yii. I displaying search results in view part.

I wrote a search query for fetching the results from one table which is Recipe. In this table name, course_id, cuisinename,type, colorie_count respectively, course_id,cuisinename, type are columns respectively.

In my controller i write code like this:

 $result="SELECT * FROM recipe WHERE name LIKE '%$name%' AND `cuisinename` LIKE '$cuisine1%' AND course_id  LIKE '%$course1%' AND `type` LIKE '%$type1%' AND `calorie_count` LIKE '%$calorie1%' ORDER BY recipe_id DESC LIMIT 15";

values are getting. if i give condition based to display the search result text. not displaying all name. but those are displaying based on names.

I added below my view part condition and code:

$query=  Course::model()->find("course_id=$as1");
$course2=$query->course_name;

$query1= Cuisine::model()->find("id=$as4");
$cuisine2=$query1->cuisinename;

$query3= RecipeType::model()->find("id=$as3");
$type2=$query3->recipeType_name;

<?php echo '<p align="center"> Showing results for&nbsp:&nbsp'.'Name'.$getval.',&nbsp'.'Course-'.$course2.',&nbsp'.'Cuisine-'.$cuisine2.',&nbsp'.'Type-'.$type2;',&nbsp';'</p>'; 
echo ',&nbsp'.'Calories-'.$calorie;
?> 

You need to create relations between tables look there . For Recipe model it should be

public function relations()
{
    return array(
        'cuisine'=>array(self::BELONGS_TO, 'Cuisine', 'cuisine_id'),
        'type'=>array(self::BELONGS_TO, 'RecipeType', 'type_id'),
    );
}

Then you can get values as $model->cuisine->name. If you don't understand creating relations, generate models (it tables must be correct foreign keys) with gii.

check this article: http://www.yiiframework.com/doc/guide/1.1/en/database.arr about relations in AR

class Recipe extends CActiveRecord
{
    ......

    public function relations()
    {
        return array(
            'course'=>array(self::BELONGS_TO, 'Course', 'course_id'),
            'cuisine'=>array(self::BELONGS_TO, 'Cuisine', 'cuisine_id'),
            'type'=>array(self::BELONGS_TO, 'RecipeType', 'type_id'),
        );
    }
}

and related Models

class RecipeType extends CActiveRecord
{
    ......

    public function relations()
    {
        return array(
            'recipes'=>array(self::HAS_MANY, 'Recipe ', 'type_id'),            
        );
    }
}

and your search query will be smth like this in controller file:

$criteria=new CDbCriteria;

$criteria->with=array(
    'course',
    'cuisine',
    'type',
);

$criteria->addCondition('course.course_id = :filter_course'); // for ID compares
$criteria->addSearchCondition('cuisine.name', $cuisinename) //for LIKE compares
...
$criteria->params = array(':filter_course' => intval($course1)); 

$searchResults = Receipe::model()->findAll($criteria);   

and in your view you can get related tables values:

foreach ($searchResults as $result){
  echo $result->cuisine->name;
}

check also http://www.yiiframework.com/doc/api/1.1/CDbCriteria for details

you can also use this $criteria to create DataProdier for CListView or CGridView helpers in your view file

$dataProvider=new CActiveDataProvider( Receipe::model()->cache(5000),
      array ( 
        'criteria' => $criteria, 
        'pagination' => array ( 
          'pageSize' => 10, 
        )
      ) 
    );

    $this->render('search',array(
      'dataProvider'=>$dataProvider         
    ));

http://www.yiiframework.com/doc/api/1.1/CListView/

http://www.yiiframework.com/doc/api/1.1/CGridView

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