Find using INNER JOIN over two tables. If I add the second table to the fields array, the return is empty; if I don’t add the second table to the fields array rows are returned.
CakePHP 2.3
/**
- Corporation model class
- hasMany associations
*/
public $hasMany = array(‘Order’ => array(‘className’ => ‘Products.Order’));
The orders table has the required field corporation_id.
/**
- Order model class
- belongsTo associations
*/
public $belongsTo = array(‘Corporation’ => array(‘className’ => ‘Products.Corporation’));
Corporation model class:
/**
- Pull orders from a specific corporation
- @param int $id of corporation
- @param array $options
-
@return array
/
public function getOrders($id, $options = array()) {
//
$return = array();
//
if (!$id) {
//
return $return;
}
//
$joins = array(
array(
‘table’ => ‘orders’
, ‘alias’ => ‘Order’
, ‘conditions’ => array(
'Order.corporation_id = ’ . $this->alias . ‘.id’
,
)
,
)
);
//
$data = $this->find(
‘all’
, array(
‘conditions’ => array(
$this->alias . ‘.id’ => $id
,
)
, ‘joins’ => $joins
, ‘fields’ => array(
$this->alias . '.’
, ‘Order.*’ // leaving this in results in an empty set
// remarking this out returns the appropriate results but with no fields from the orders table
,
)
,
)
);
//
return $data;
}