1

I have a Company model which is related to a MonthlyReturn model. A company can have many MonthlyReturns. I am trying to get an array of all companies which have a monthly return for a specified month.

The code I am using is as follows:

$this->Company->find('all', array('contain' => array(
    'MonthlyReturn' => array(
        'conditions' => array('MonthlyReturn.month' => "2012-01-01")
    )
)));

The Company Model:

public $hasMany = array(
        'Employee' => array(
            'className' => 'Employee',
            'foreignKey' => 'company_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        ),
        'MonthlyReturn' => array(
            'className' => 'MonthlyReturn',
            'foreignKey' => 'company_id',
            'dependent' => false,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'exclusive' => '',
            'finderQuery' => '',
            'counterQuery' => ''
        )
    );

    public $hasOne = 'Umuser';

}

This code is returning all companies rather than those with a return for the month. The 'MonthlyReturn.month' field will always be in the format above, i.e year and month will change but will always be the 1st of the month.

Any advice would be appreciated.

4
  • i think its hard to give a perfect answer without knowing the Company datamodel, but i will give it a try ;)
    – alexdd55
    Feb 14, 2012 at 14:31
  • 1
    the containable behavior is also available?
    – alexdd55
    Feb 17, 2012 at 8:52
  • Sorry @alexdd55, I don't understand your question? Can you elaborate please.
    – Cronin O'M
    Feb 17, 2012 at 10:51
  • 1
    did you ad this public $actsAs = array( 'Containable', );
    – alexdd55
    Feb 24, 2012 at 10:52

2 Answers 2

0

your code says, that you ask for a specific date, not a month.

try this:

$this->Company->find('all', array('contain' => array(
'MonthlyReturn' => array(
    'conditions' => array('MonthlyReturn.month LIKE' => "2012-01%")
))));
1
  • I have update my initial post now and as you will see the date will be in a specific format.
    – Cronin O'M
    Feb 14, 2012 at 14:41
0

I'm not sure if there is a better way but you can do it in two calls like this:

$company_ids = $this->MonthlyReturn->query("SELECT DISTINCT company_id FROM monthy_returns WHERE month = '2012-01-01';")

$this->Company->find('all', array(
    'conditions' => array('Company.id' => $company_ids[0]),
    'contain' => array('MonthlyReturn' => array(
        'conditions' => array('MonthlyReturn.month' => "2012-01-01")
    )
)));

That's the idea but some debugging may be required

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.