用户将设置两个参数$from
,$to
然后用于查询以获取相关数据信息。
所以我想在模型中访问这些变量,而不是每次在查询中重复它们
控制器
function index()
{
$this->CompCalculation->from="2013-07-01";
$this->CompCalculation->to="2013-07-01";
pr($this->CompCalculation->find('first'));
}
模型
class CompCalculation extends AppModel {
var $name = 'CompCalculations';
var $hasMany = array(
'Leave' => array(
'className' => 'Leave',
'foreignKey' => 'user_id',
'conditions' => array('from >= ' => $from,
'to <= ' => $to
),
),
'Timesheet' => array(
'className' => 'Timesheet',
'foreignKey' => 'user_id',
'conditions' => array('from >= ' => $from,
'to <= ' => $to
),
),
'CompLeave' => array(
'className' => 'CompLeave',
'foreignKey' => 'user_id',
'conditions' => array('from >= ' => $from,
'to <= ' => $to
),
),
'Attendance' => array(
'className' => 'Attendance',
'foreignKey' => 'user_id',
'conditions' => array('from >= ' => $from,
'to <= ' => $to
),
),
);
}
我已经尝试过以下解决方案
更新
控制器
function index()
{
$this->CompCalculation->sett('2010-09-09');
}
模型
class CompCalculation extends AppModel {
var $name = 'CompCalculations';
public $from='01-01-01';
public $from='11-11-11';
public function sett($fr)
{
echo $this->from;
echo $this->from=$fr;
pr($this->find('first'));
echo $this->from;
}
public function __construct($id = false, $table = null, $ds = null) {
$this->hasMany =array(
'Leave' => array(
'className' => 'Leave',
'foreignKey' => 'user_id',
'conditions' => array('STR_TO_DATE(Leafe.from,"%W %d %M %Y") >= ' => $this->from,
'STR_TO_DATE(Leafe.to,"%W %d %M %Y") <= ' => $this->to
),
),
'Timesheet' => array(
'className' => 'Timesheet',
'foreignKey' => 'user_id',
'conditions' => array('STR_TO_DATE(from_date,"%b %d,%Y") >= ' => $this->from,
'STR_TO_DATE(from_date,"%b %d,%Y") <= ' => $this->to
),
),
'CompLeave' => array(
'className' => 'CompLeave',
'foreignKey' => 'user_id',
),
'Attendance' => array(
'className' => 'Attendance',
'foreignKey' => 'user_id',
),
);
parent::__construct($id, $table, $ds);
}
}
结果
01-01-01
2010-09-09
SQL Dump 显示查询使用 01-01-01(而不是 2010-09-09)
2010-09-09