0

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

5
  • the question is why do you want this variable ? You need to first read Cakephp models Commented Aug 19, 2013 at 11:11
  • @MoyedAnsari User will select these range in view. So i want all related tables data according to that range Commented Aug 19, 2013 at 11:13
  • 3
    You should add a function in model which return your desire date with given dates rather than changing models Commented Aug 19, 2013 at 11:15
  • Can you please provide me link or example. I am trying this from morning. @MoyedAnsari Commented Aug 19, 2013 at 11:16
  • @MoyedAnsari Question updated as suggested. Please have a look again and suggest what's missing Commented Aug 19, 2013 at 12:21

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@MoyedAnsari Have a look at this code. Its working. Did you mean something like this.
glad it worked for you. again, it's not a very good practice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.