staff.php模型中(該模型對(duì)應(yīng)名為Staff的信息表):
<?php
namespace app\index\model;
use think\Model;
class Staff extends Model
{
}
?>
index.php控制器中:
<?php
namespace app\index\controller;
use app\index\model\Staff;
class Index
{
public function index()
{
$staff = Staff::get(1033);
return $staff->name.'的入職時(shí)間是:'.date('Y-m-d',$staff->hiredate);
}
}
?>
可以在模型中設(shè)計(jì)一個(gè)讀取器,就不用每次都這么用了逆害。
staff.php模型中:
<?php
namespace app\index\model;
use think\Model;
class Staff extends Model
{
protected function getHireDateAttr($hiredate)
{
return date('Y-m-d',$hiredate);
}
}
?>
index.php控制器中:
<?php
namespace app\index\controller;
use app\index\model\Staff;
class Index
{
public function index()
{
$staff = Staff::get(1033);
return $staff->name.'的入職時(shí)間是:'.$staff->hiredate;
}
}
?>
也可以這樣:
index.php控制器中:
<?php
namespace app\index\controller;
use app\index\model\Staff;
class Index
{
public function index()
{
$staff = Staff::get(1033);
return $staff->hiredate;
}
}
?>
staff.php模型中:
<?php
namespace app\index\model;
use think\Model;
class Staff extends Model
{
protected function getHireDateAttr($hiredate,%data) //get...Attr是固定格式纲熏,只有這樣的才是讀取器舰褪,會(huì)自動(dòng)讀取...的內(nèi)容
{
return $data['name'].'的入職時(shí)間是:'.date('Y-m-d',$hiredate);
}
}
?>
在model.php中添加:
protected function setHireDateAttr($hiredate)
{
return strtotime($hiredate);
}
在index.php中添加:
$staff = new Staff;
$staff->name = '左冷禪';
$staff->salary = 5000;
$staff->hiredate = '2014-10-23';
if($staff->save()){
return '新員工'.staff->name.',ID:'.$staff->id.',添加成功';
} else {
return $staff->getError();
}
這樣會(huì)往數(shù)據(jù)庫中添加一個(gè)轉(zhuǎn)化為時(shí)間戳的hiredate则酝。