0

我有一个名为“事件”的对象,它是通过 $data['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 在每一行的键 'delegates' 下添加一个嵌套对象,通过比较该表中的 'event_id' 和 'delegate_id 列,使用桥接表 'events_delegates_bridge' 通过附加到事件的委托. 本质上,对象看起来像这样:

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 扩展 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 回答