Question

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

Was it helpful?

Solution

This is not a very good practice but you can try something like this in your controller function:

$this->CompCalculation->hasMany['Timesheet']['conditions']=
       array('STR_TO_DATE(Timesheet.from_date,"%b %d,%Y") >= ' =>
       '2013-06-01','STR_TO_DATE(Timesheet.from_date,"%b %d,%Y") <= ' =>
       '2013-07-01');

$this->CompCalculation->hasMany['Leave']['conditions']=
       array('STR_TO_DATE(Leave.from,"%W %d %M %Y") >= ' =>
       '2013-06-01','STR_TO_DATE(Leave.to,"%W %d %M %Y") <= ' => '2013-07-01');

and so on for others...

By doing this you will be setting the conditions in controller and pass it on to model. Otherwise you must be getting NULL value in SQL conditions while trying to set the variable in model because you can't set variables like that.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top