0

$data['events'] = function (この関数は、アクティブ レコードを使用してイベント テーブルなどから情報を引き出します) によって作成された、'events' というオブジェクトがあります。

イベント オブジェクトは次のようになります。

Array
    (
        [0] => stdClass Object
            (
                [id] => 2
                [course_name] => Course 3
                [course_description] => Course
                [course_price] => 995
                [supplier_name] => Supplier 3
                [location_country_code] => GB
                [location_country] => United Kingdom
                [location_city] => London
                [venue_name] => Venue 2
                [venue_address] => 2 Street
                [venue_postcode] => EC2M 7PQ
                [venue_city] => London
                [venue_county] => 
                [venue_country] => United Kingdom
                [venue_locality] => 
                [event_type] => Materials Only
                [event_status] => Confirmed
                [course_id] => 2
                [event_duration] => 3
                [event_start_date] => 2013-09-12
                [event_date_added] => 2013-02-26 14:36:06
                [event_status_id] => 2
                [event_type_id] => 4
                [tutor_id] => 0
                [tutor_confirmed] => 0
                [event_featured] => 0
                [event_push] => 0
                [event_active] => 0
                [invigilator_id] => 0
                [event_discount] => 
                [event_max_delegates] => 16
                [location_id] => 1
                [venue_id] => 1
                [supplier_id] => 2
            )

        [1] => stdClass Object
            (
                [id] => 1
                [course_name] => Course Name
                [course_description] => Course Description
                [course_price] => 995
                [supplier_name] => Supplier 1
                [location_country_code] => GB
                [location_country] => United Kingdom
                [location_city] => London
                [venue_name] => Venue Name
                [venue_address] => Street
                [venue_postcode] => EC2M 7PQ
                [venue_city] => London
                [venue_county] => 
                [venue_country] => United Kingdom
                [venue_locality] => 
                [event_type] => Private Venue
                [event_status] => Provisional
                [course_id] => 1
                [event_duration] => 3
                [event_start_date] => 2013-11-13
                [event_date_added] => 2013-02-26 09:56:17
                [event_status_id] => 1
                [event_type_id] => 3
                [tutor_id] => 0
                [tutor_confirmed] => 0
                [event_featured] => 0
                [event_push] => 0
                [event_active] => 0
                [invigilator_id] => 0
                [event_discount] => 395
                [event_max_delegates] => 16
                [location_id] => 1
                [venue_id] => 1
                [supplier_id] => 1
            )

    )

ActiveRecord を使用して各行のキー「デリゲート」の下にネストされたオブジェクトを追加したいと思います。これは、ブリッジ テーブル「events_delegates_bridge」を使用して、そのテーブルの「event_id」列と「delegate_id」列を比較することにより、イベントに関連付けられたデリゲートをプルします。 . 基本的に、オブジェクトは次のようになります。

Array
    (
        [0] => stdClass Object
            (
                [id] => 2
                [course_name] => Course 3
                        [delegates] => Array
                                            (
                                                [0] => stdClass Object
                                                    (
                                                        [id] => 1
                                                        [name] => Joe Bloggs
                                                    )
                                                [1] => stdClass Object
                                                    (
                                                        [id] => 2
                                                        [name] => Joe Smith
                                                    )
                                                [3] => stdClass Object
                                                    (
                                                        [id] => 3
                                                        [name] => Jane Doe
                                                    )
                                            )
                [course_description] => Course
                [course_price] => 995
                [supplier_name] => Supplier 3
                [location_country_code] => GB
                [location_country] => United Kingdom
                [location_city] => London
                [venue_name] => Venue 2
                [venue_address] => 2 Street
                [venue_postcode] => EC2M 7PQ
                [venue_city] => London
                [venue_county] => 
                [venue_country] => United Kingdom
                [venue_locality] => 
                [event_type] => Materials Only
                [event_status] => Confirmed
                [course_id] => 2
                [event_duration] => 3
                [event_start_date] => 2013-09-12
                [event_date_added] => 2013-02-26 14:36:06
                [event_status_id] => 2
                [event_type_id] => 4
                [tutor_id] => 0
                [tutor_confirmed] => 0
                [event_featured] => 0
                [event_push] => 0
                [event_active] => 0
                [invigilator_id] => 0
                [event_discount] => 
                [event_max_delegates] => 16
                [location_id] => 1
                [venue_id] => 1
                [supplier_id] => 2
            )
                )

これを達成するための最善の方法はありますか?ありがとう。

Event Model クラス Event_Model extends CI_Model {

    public function get_events() {

        $this->db->select( '*' );
        $this->db->from( 'courses' );
        $this->db->from( 'suppliers' );
        $this->db->from( 'locations' );
        $this->db->from( 'venues' );
        $this->db->from( 'event_type' );
        $this->db->from( 'event_status' );
        $this->db->join( 'events', 'events.course_id = courses.id AND events.supplier_id = suppliers.id AND events.location_id = locations.id AND events.venue_id = venues.id AND events.event_type_id = event_type.id AND events.event_status_id = event_status.id', 'inner' );
        $this->db->order_by( 'events.event_start_date', 'asc' );
        $query = $this->db->get();

        return $query->result();

    }

}

ダッシュボード コントローラ $data['events'] = $this->event_model->get_events();

デリゲート モデル デリゲート データを取得するためにこれを作成しました。イベント オブジェクトに正しいデリゲートを追加するために使用できると思いますか?

class Delegate_Model extends CI_Model {

    public function get_delegates() {

        $this->db->select( '*' );
        $this->db->from( 'delegates' );
        $this->db->from( 'events_delegates_bridge' );
        $this->join( 'delegates', 'delegates.id = events_delegates_bridge.delegate_id', 'inner' );
        $query = $this->db->get();
        return $query->result();

    }

}

これをテストしたところ、空白のページが表示されます。

4

2 に答える 2

1

2 つの別々のクエリで実行するのが最善です。

$events = array();
$result = $this->db->query('SELECT * FROM events WHERE ...');
foreach($result->result_array() as $event) {
    $events[$event['id']] = $event;
}

$result = $this->db->query('
    SELECT * FROM events_delegates_bridge 
    JOIN delegates ON (delegate_id = delegates.id)
    WHERE ...
');
foreach($result->result_array() as $delegate) {
    if (!empty($events[$delegate['event_id']])) {
        $events[$delegate['event_id']]['delegates'][] = $delegate
    }
}

このコードは、イベントをクエリして、イベント ID でインデックス付けされた配列に配置するだけです。次に、別のクエリを実行してデリゲートを取得し、それらを適切なイベントにアタッチします。

于 2013-02-27T15:43:49.290 に答える
-1

使う$name=$variable->result_array();var_dump($name);と思う この作品

于 2013-02-27T15:35:11.073 に答える