简体   繁体   English

2表的内部联接无法使用yii

[英]Inner join of 2 tables is not working using yii

I want to perform a query with join in Yii. 我想用Yii中的join执行查询。 Query is like that "select j.title,j.company_name from job_application a inner join jobs j on a.job_id = j.id" 查询就像“从job_application中选择一个j.title,j.company_name一个a.job_id = j.id的内部联接jobs”

My query in controller-sitecontroller 我在controller-sitecontroller中的查询

         public function actionmyjob()
         {
         $row = Yii::app()->db->createCommand()
        ->select('j.title,j.company_name')
        ->from('job_application a')
        ->join('jobs j','a.job_id = j.id')
        ->queryRow();
    echo $count=count($row);
       $this->render('myjob',array('row' =>$row));
      }

My view page myjob.php 我的查看页面myjob.php

 <?php
 foreach($row as $rows)  
 {
 echo $row->title."</br>";
 }

 ?>

Is there anything you can help? 有什么可以帮忙的吗? Any idea is greatly appreciated. 任何想法都将不胜感激。

I think there is no problem with your query. 我认为您的查询没有问题。 But still you should check using CVarDumper::dump($row,10,true); 但是仍然应该使用CVarDumper::dump($row,10,true);

And in your view file you should use this 在您的视图文件中,您应该使用此

<?php
 foreach($row as $rows)  
 {
 echo $rows."</br>"; // If you want to use only title then try $row['title']
 }
 ?>


And if you are interested in all the result then you should use queryAll() . 如果您对所有结果都感兴趣 ,则应使用queryAll()

For this change your action to 为此,将您的操作更改为

public function actionmyjob()
         {
         $row = Yii::app()->db->createCommand()
        ->select('j.title,j.company_name')
        ->from('job_application a')
        ->join('jobs j','a.job_id = j.id')
        ->queryAll();
    echo $count=count($row);
       $this->render('myjob',array('row' =>$row));
      }

And your view to 和你的看法

<?php
 foreach($row as $rows)  
 {
 echo $rows['title']."</br>"; // change $row to $rows
 }

 ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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