I need to know if it is possible to sort the "sales" table by the "date" field of the related "SaleBooking".
$query = $this->_salesTable->find()
->where([
'product_id IN' => $ids,
'status IN' => $status,
'type IN' => $types
])
->select([
'id',
'product_id',
'entity_id',
'Sales.type',
'status',
'quantity',
])
->contain([
'Product',
'SaleBooking'
]);
I have tried with the following code but it orders me the records of the contain and what I need is to order the main table by a field of the related table:
$query->contain(['SaleBooking' => ['sort' => ['SaleBooking.date' => 'DESC']]]);
The model is:
class SalesTable extends FilterTable
{
public function initialize(array $config)
{
$this->table('sa_sales');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->hasMany('SaleBooking', [
'className' => 'SaleBookings',
'foreignKey' => 'sale_id',
'dependent' => true,
'propertyName' => 'saleBookings',
]);
}
}
class SaleBookingsTable extends BaseTable
{
public function initialize(array $config)
{
$this->table('sa_sale_bookings');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('BookingSale', [
'className' => 'Sales',
'foreignKey' => 'sale_id',
'propertyName' => 'saleBookings'
]);
}
}
Thank you very much for the help in advance. Greetings.