Codeigniter MY_Model由杰米和关系查询

时间:2015-04-06 13:16:56

标签: php codeigniter codeigniter-base-model

我有以下关系。

Profile
---------------------------------------
id profile_image_id otherdetails
---------------------------------------
1   22             ---
---------------------------------------  

   media
----------------------------------------------
id profile_id path                type
----------------------------------------------
22  1         /exes/lod.png       profile_image
----------------------------------------------  

个人资料有媒体。 媒体是图像路径的集合。并有一个名为' type'的列//

我想加载媒体类型=' profile_image';

的所有个人资料

我想制作如下内容:

select * from profile 
inner join with media
where media.type ='smthing';

..加载模型

$this->profile->with('media')->get_by('type','profile_image') ;; 
// runs on profile model than media model. 

这不起作用

我见过documentation,但我无法获得线索。

你之前做过的任何与之相似的事情?感谢

2 个答案:

答案 0 :(得分:1)

如果我没错,那么这对你有帮助。

$this->db->select('p.*');
$this->db->from('profile p');
$this->db->join('media m','p.id = m.profile_id','left');
$this->db->where('m.type','profile_image');
$result = $this->db->get()->result_array();
print_r($result);

答案 1 :(得分:0)

当然,这是一个解决方案,其中包含一些帮助者与MY_MODEL类的组合。

即使在未来,这也是一个定制解决方案,感谢 michail1982

public function with_media($type='profile_image') {
  $this->_database->join('medias',$this->_table.
     '.'.$this->primary_key.' = medias.profile_id')->where('medias.type', $type);
}