<?php
namespace app\rongyu\model;
use app\common\model\Base;
class JsRongyuInfo extends Base
{
public function search($search)
{
$hjschool = $search['hjschool'];
$fzschool = $search['fzschool'];
$category = $search['category'];
$rongyuce = $search['rongyuce'];
$order_field = $search['order_field'];
$order = $search['order'];
$category = $search['category'];
$search = $search['search'];
$data = $this->order([$order_field =>$order])
->when(strlen($hjschool)>0,function($query) use($hjschool){
$query->where('hjschool','in',$hjschool);
})
->when(strlen($fzschool)>0,function($query) use($fzschool){
$query->where('rongyuce','in',function($q) use($fzschool){
$q->name('JsRongyu')->where('fzschool','in',$fzschool)->field('id');
});
})
->when(strlen($category)>0,function($query) use($category){
$query->where('rongyuce','in',function($q) use($category){
$q->name('JsRongyu')->where('category','in',$category)->field('id');
});
})
->when(strlen($rongyuce)>0,function($query) use($rongyuce){
$query->where('rongyuce',$rongyuce);
})
->when(strlen($search)>0,function($query) use($search){
$query->where('title','like',$search);
})
->with(
[
'hjSchool'=>function($query){
$query->field('id,jiancheng');
},
'rySubject'=>function($query){
$query->field('id,title');
},
'jxCategory'=>function($query){
$query->field('id,title');
},
'ryTuce'=>function($query){
$query->field('id,title');
},
'hjJsry'=>function($query){
$query->field('rongyuid,teacherid')
->with(['teacher'=>function($query){
$query->field('id,xingming');
}]);
},
]
)
->append(['hjJsName'])
->select();
return $data;
}
public function ryTuce()
{
return $this->belongsTo('\app\rongyu\model\JsRongyu','rongyuce','id');
}
public function hjSchool()
{
return $this->belongsTo('\app\system\model\School','hjschool','id');
}
public function fzSchool()
{
return $this->belongsTo('\app\system\model\School','fzschool','id');
}
public function rySubject()
{
return $this->belongsTo('\app\teach\model\Subject','subject','id');
}
public function jxCategory()
{
return $this->belongsTo('\app\system\model\Category','jiangxiang','id');
}
public function hjJsry()
{
return $this->hasMany('\app\rongyu\model\JsRongyuCanyu','rongyuid','id')->where('category',1);
}
public function allJsry()
{
return $this->hasMany('\app\rongyu\model\JsRongyuCanyu','rongyuid','id');
}
public function cyJsry()
{
return $this->hasMany('\app\rongyu\model\JsRongyuCanyu','rongyuid','id')->where('category',2);
}
public function getJibieAttr()
{
$jibie = '';
if($this->fzschool){
$jibie = $this->fzSchool->jibie;
}
return $jibie;
}
public function setHjshijianAttr($value)
{
return strtotime($value);
}
public function getHjshijianAttr($value)
{
if ($value>0)
{
$value = date('Y-m-d',$value);
}else{
$value = "";
}
return $value;
}
public function getHjJsNameAttr($value)
{
$teacherList = $this->getAttr('hjJsry');
$teachNames = "";
$i = 0;
foreach ($teacherList as $key => $value) {
# code...
if($i == 0)
{
$teachNames = $value['teacher']['xingming'];
}else{
$teachNames = $teachNames . '、' .$value['teacher']['xingming'];
}
$i++;
}
return $teachNames;
}
}