User will set two parameters $from
and $to
which will be then used to make query to fetch related data info.
So i want to access these variables in model instead of repeating them each time in query
Controller
function index()
{
$this->CompCalculation->from="2013-07-01";
$this->CompCalculation->to="2013-07-01";
pr($this->CompCalculation->find('first'));
}
Model
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
),
),
);
}
I have already tried following solutions
Php Variable in Cakephp Model
accessing controller variables in model CakePHP
Update
Controller
function index()
{
$this->CompCalculation->sett('2010-09-09');
}
Model
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);
}
}
Result
01-01-01
2010-09-09
SQL Dump show query using 01-01-01 (Instead of 2010-09-09)
2010-09-09