Hello, I need to paginate and sort a list with Herds, days in, and how many animals are alive, sold or dead.
A Herds hasMany Animals and Animals belongsTo Herds
Herds fields: id, name, from_date, to_date
Animals fields: herd_id, status (1 for alive, -1 for sold and -3 for dead)
The exact same problem was discussed here but things have changed a lot since cakephp 1.
This is my ugly, current working solution:
$this->paginate = [
'fields' => [
'Herds.name',
'days' => 'IF(Herds.to_date IS NULL, DATEDIFF(UTC_TIMESTAMP(), Herds.from_date), DATEDIFF(Herds.to_date, Herds.from_date))',
'alive' => '(SELECT COUNT(*) FROM Animals WHERE Animals.status = 1 AND Animals.herd_id = Herds.id)',
'sold' => '(SELECT COUNT(*) FROM Animals WHERE Animals.status = -1 AND Animals.herd_id = Herds.id)',
'dead' => '(SELECT COUNT(*) FROM Animals WHERE Animals.status = -2 AND Animals.herd_id = Herds.id)',
],
]
I’ve been trying to solve it the right way, using contain and finders:
HerdsController.php
$tropas = $this->Herds->find()->contain('Deadanimals');
AnimalsTable.php
public function findDead(Query $query, array $options)
{
return $query->select(['count' => 'COUNT(*)'])->where(['Animals.status' => -2]);
}
HerdsTable.php
$this->hasMany('Deadanimals')
->setClassName('Animals')
->setFinder('findDead')
->setForeignKey('herd_id')
->setBindingKey('id')
->setJoinType('INNER');
but I’m getting the error:
Unknown finder method “findDead” on App\Model\Table\AnimalsTable.