0

I have two models called Batch and User

Batch has the following

public $belongsTo = array(

    'Customer' => array(
        'className' => 'User',
        'foreignKey' => 'customer_id',
        'conditions' => array('Customer.group_id' => CUSTOMERS),
        'fields' => '',
        'order' => '', 
    ),
);

When I do the following:

$customers = $this->Batch->Customer->find('list');

I fully expected to get back just the users whose group_id matches CUSTOMERS. It returns ALL the records in the users table.

However, I actually have to write

$customers = $this->Batch->Customer->find('list', array('conditions' => array('Customer.group_id' => CUSTOMERS)));

Is there a way so that the chained model User knows that it is called as Customer by Batch and therefore automatically reads the correct conditions in the associations found in Batch model?

I want to make my code more readable hence the motivation for this question.

I want to write simply

$customers = $this->Batch->Customer->find('list');

or something similarly straightforward.

Of course, I realized that if I do the following:

$batches = $this->Batch->find('all');

The condition stated in the associations will be used. But I don't want to find batches. I want to find just customers.

I am using CakePHP 2.4

2 Answers 2

1

I think you can't

but you can create custom find types in User model file

public $findMethods = array('customer' =>  true); //this enable a custom find method named 'customer'

protected function _findCustomer($state, $query, $results = array()) {
        if ($state === 'before') {
            $query['conditions'] = array('group_id' => CUSTOMERS);
        }
        return parent::_findList($state, $query, $results);
    }

and in BatchesController

$this->Batch->Customer->find('customer');
0

There are several ways to do this.

1)

do nothing.

Continue to use code like

$customers = $this->Batch->Customer->find('list', array('conditions' => array('Customer.group_id' => CUSTOMERS)));

2)

create a custom find method as suggested by arilia.

3)

write a getCustomers method inside Batch model

where it looks something like this:

public function getCustomers($type, $query = array()) { 
    if (empty($query['conditions'])) {
        $query['conditions'] = array();
    }
    $query['conditions'] = array_merge($query['conditions'], array('Customer.group_id' => CUSTOMERS));
    return $this->Customer->find($type, $query);
}

then you can call

$customers = $this->Batch->getCustomers('list');

UPDATE:

I have written a Plugin that helps with this kind of behavior, utilizing the 3rd solution.

 class Batch extends AppModel { 
   public $name = 'Batch'; 

   public $actsAs = array('UtilityBehaviors.GetAssoc'); 
   public $belongsTo = array(
      'Customer' => array(
           'className' => 'User',
           'foreignKey' => 'customer_id',
           'conditions' => array('Customer.group_id' => 7),
           'fields' => '',
           'order' => '', 
       ),
   ); 
 }

You can fetch just the customer data when you are in BatchesController this way:

 $customers = $this->Batch->getAssoc('Customer', 'list');
 $customers = $this->Batch->getAssoc('Customer', 'all');
 $customerCount = $this->Batch->getAssoc('Customer', 'count');

This behavior has tests at travis and you can read about the tests written at github.

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.