简体   繁体   English

用篝火加入查询(codeigniter)

[英]join query with bonfire (codeigniter)

I'm trying to do a join query in bonfire (which is great for the beginner i am btw) 我正在尝试在篝火中进行联接查询(这对我是初学者的初学者来说很棒)

I first tried to set a public function in the method, but after a while, i though it would be better to put it in the controller (maybe i'm wrong...?) 我首先尝试在该方法中设置一个公共函数,但是过了一会儿,尽管将它放在控制器中会更好(也许我错了...?)

However, i guess my join request is valid, but i can't get it in my view... 但是,我想我的加入请求是有效的,但是我无法在我看来...

Here is my controller: 这是我的控制器:

class Patient extends Admin_Controller {

    public function __construct() {
        parent::__construct();

        $this->load->model('post_model');

        Template::set_block('search/search', 'search/search');
        //Template::set('toolbar_title', 'Manage Your Blog');
        Template::set_theme('custom', 'default');
        Template::set_block('nav', 'nav');
    }

    public function detail($id = null) {
        $this->load->helper('typography');
        $this->load->model('operation_model');

        $this->db->select('*');
        //$this->db->from ('operation AS ope');
        $this->db->from('operation');

        $this->db->join('patient', "patient.zkp_pat = operation.zkf_pat ");

        $query = $this->db->get();
        $post = $this->post_model->find($id);

        //Template::set('post', $query);
        Template::set('post', $post);

        Template::set_view('detail');
        Template::render();
    }
}

And my view file: 和我的视图文件:

<code>
    <div class="post">
        <?php
        echo ($post->nom . ' ');
        echo ($post->prenom . ' ');
        echo ($post->zkp_pat . ' ');

        echo ($post->dob);

        foreach ($query as $row) :
            echo ($row->zkf_pat . ' ' );
            echo "titre de l'opération: " . ($row->titre);
        endforeach;
        ?>
    </div>
</code>

Thanks for your advices and help ! 感谢您的建议和帮助!

I asked for help on the BF forum too, and somebody gave me the solution=>hopefully this will help somebody else. 我也在BF论坛上寻求帮助,有人给了我解决方案=>希望这会帮助其他人。 The issue was with the $query , which is a DB_result object. 问题出在$query ,后者是一个DB_result对象。 http://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data http://www.codeigniter.com/user_guide/database/query_builder.html#selecting-data

to solve the problem i had to doo: $query->result() 解决我不得不做的问题: $query->result()

Final code: 最终代码:

Controller: 控制器:

public function detail ($id = null){
    $this->load->helper('typography');


    $this->db->from ('operation as ope');
    $this->db->join ('patient',
                    "patient.zkp_pat = ope.zkf_pat " );
    $this->db->where ('patient.zkp_pat', $id);
    $query = $this->db->get();

    $post = $this->post_model->find($id);

    Template::set('query', $query);
    Template::set('post', $post);


    Template::set_view('detail');
    Template::render()        
}

And view file: 并查看文件:

<div class="post">
    <h2><?php e($post->nom); ?></h2>
<?php 
echo ($post->nom . ' '); echo ($post->prenom . ' '); 
echo($post->zkp_pat . ' ');
echo ($post->dob);

foreach ($query->result() as $row) :
    echo '<div>'
    echo " \n"; 
    echo ($row->zkf_pat . '  ');
    echo "titre de l'opération: " . ($row->titre);
?>
    </div>
<?php 
    endforeach;
?>

</div>

$query->get()->result() returns an object with all fetched data, whereas $query->get()->row() returns only a single result row, if the result has one row, it returns only the first one. $query->get()->result()返回包含所有已获取数据的对象,而$query->get()->row()仅返回单个结果行,如果结果只有一行,则仅返回第一个。 The result is given as an object... 结果作为对象给出...

If you like my edit, please upvote, that would be very helpful ! 如果您喜欢我的编辑,请投票,这将非常有帮助!

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

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