主要照片
我创建了发送到控制器ID的更新表单,并且一切正常。但是,主要照片我如何检查返回首先添加的数据。例如,event_id = 2没有主图像,event_id = 4具有main_photo(例如:test),但它获取event_id = 2空值。我花了很多时间找不到解决方案。
Modell
public function get_list_for_edit($event_id){
return
$this->db->select('*')
->select('pn.title as push_title, e.title as event_title', false)
->from('events e')
->join('events_locations el','el.event_id =' . $event_id,'left')
->join('push_notifications pn',
'pn.type_id ='. $event_id,'left')
->get('events')
->row_object();
}
数组1 event_id = 2
stdClass Object
(
[event_id] => 2
[title] => test1 -pushnotificationtitle
[description] => <p>Test1 -description</p>
[main_photo] =>
[start] => 2017-03-12 08:00:00
[end] => 2017-03-15 06:00:00
[phone] => 946871783
[email] => abdurasulov.ravshan@gmail.com
[keywords] => test1 -keywords
[created_at] => 2017-03-12 20:29:09
[status] => active
[deleted] => 0
[event_location_id] => 2
[country_id] => 1
[city_id] => 1
[area_id] => 1
[street] => Tinchlik street
[building_no] => Tinchlik street
[latitude] => 25.218670435768225
[longitude] => 55.29264020500705
[push_notificaion_id] => 2
[type] => event
[type_id] => 2
[datetime] => 2017-03-12 20:29:09
[message] => test1 -pushnotificationmessage
[push_title] => test1 -pushnotificationtitle
[event_title] => Test1
)
数组2 event_id = 5
stdClass Object
(
[event_id] => 5
[title] => test4 - notificationtitle
[description] => <p>Test1 -description</p>
[main_photo] =>
[start] => 2017-03-12 08:00:00
[end] => 2017-03-15 06:00:00
[phone] => 946871783
[email] => abdurasulov.ravshan@gmail.com
[keywords] => test1 -keywords
[created_at] => 2017-03-12 20:45:47
[status] => active
[deleted] => 0
[event_location_id] => 5
[country_id] => 1
[city_id] => 1
[area_id] => 1
[street] => Tinchlik street
[building_no] => Tinchlik street
[latitude] => 25.197858466144194
[longitude] => 55.244231696706265
[push_notificaion_id] => 5
[type] =>
[type_id] => 5
[datetime] => 2017-03-17 08:00:00
[message] => test4 - notificationtitle
[push_title] => test4 - notificationtitle
[event_title] => Test1
)
如果我正确理解并且需要获取一个图像,但是主要图像可能会或可能不可用,这就是我要做的:
控制器:
//get your event
$data['event'] = $this->db->get_where('event_table')->row();
$data['event']->main_image = $this->event_model->get_main_image($data['event']);
模型:
//get all images
$images = $this->db->get_where('e_products_main_images', array('main_image_product_id_fk' => $product->product_id))->result();
$image = null;
//check if main exists
foreach ($images as $key => $animage) {
if ($aniamge->is_main == '1') {
$image = $aniamge;
}
}
//if not get the first one
if ($image == null and isset($images[0])) {
$image = $images[0];
}
//or return null
if ($image == null) {
return null;
}
return $image->main_image_path;