CodeIgniter多维对象- ActiveRecord



我有一个名为'events'的对象,这是通过$data['events'] = function创建的(该函数从事件表和其他使用活动记录的事件表中提取信息)。

事件对象看起来像:

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的每一行的关键'delegate '下添加一个嵌套对象,它通过比较该表中的'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扩展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();
    }
}
<

仪表板控制器/strong>$ data['事件')= $ this -> event_model -> get_events ();

<

代表模型/strong>我创建这个是为了获取委托数据。您认为它可以用于向事件对象添加正确的委托吗?

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();
    }
}

刚刚测试过,显示空白页

您最好使用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索引的数组中。然后,运行一个单独的查询来提取委托,并将它们附加到适当的事件。

use $name=$variable->result_array();var_dump($name); I think This work

相关内容

  • 没有找到相关文章

最新更新