我正在使用CakePHP 3.2 我对CakePHP有点新鲜。我试图从Icases表中获取数据,其中创建的日期在10天内,但由于某种原因,它只是返回第一行。任何人都可以让我知道我做错了什么。
我的控制器IcasesController
namespace App\Controller;
use App\Controller\AppController;
/**
* Icases Controller
*
* @property \App\Model\Table\IcasesTable $Icases
*/
class IcasesController extends AppController
{
public function index()
{
$case_count_data = $this->Icases->getCaseCountByAge();
print_r($case_count_data);
}
}
我的表类IcasesTable
namespace App\Model\Table;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Cake\ORM\TableRegistry;
use DateTime;
class IcasesTable extends Table
{
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->table('icases');
$this->displayField('name');
$this->primaryKey('id');
$this->belongsTo('Clients', [
'foreignKey' => 'client_id'
]);
$this->hasMany('Documents', [
'foreignKey' => 'icase_id'
]);
$this->belongsToMany('Users', [
'foreignKey' => 'icase_id',
'targetForeignKey' => 'user_id',
'joinTable' => 'icases_users'
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
$validator
->dateTime('date_instruction_received')
->allowEmpty('date_instruction_received');
$validator
->dateTime('date_online_invitation_last_sent')
->allowEmpty('date_online_invitation_last_sent');
$validator
->dateTime('date_approved_for_allocation')
->allowEmpty('date_approved_for_allocation');
$validator
->dateTime('date_consent_received')
->allowEmpty('date_consent_received');
$validator
->dateTime('go_date')
->allowEmpty('go_date');
$validator
->dateTime('date_last_referred_to_client')
->allowEmpty('date_last_referred_to_client');
$validator
->dateTime('date_last_referred_to_qc_report')
->allowEmpty('date_last_referred_to_qc_report');
$validator
->dateTime('date_last_referred_qc')
->allowEmpty('date_last_referred_qc');
$validator
->dateTime('date_last_referred_sub')
->allowEmpty('date_last_referred_sub');
$validator
->dateTime('date_last_state_change')
->allowEmpty('date_last_state_change');
$validator
->dateTime('date_marked_for_archival')
->allowEmpty('date_marked_for_archival');
$validator
->allowEmpty('state');
$validator
->integer('weight')
->allowEmpty('weight');
$validator
->requirePresence('case_reference_i_d', 'create')
->notEmpty('case_reference_i_d')
->add('case_reference_i_d', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);
$validator
->allowEmpty('special_intructions');
$validator
->allowEmpty('international');
$validator
->allowEmpty('name');
$validator
->requirePresence('sms_enabled', 'create')
->notEmpty('sms_enabled');
$validator
->allowEmpty('client_reference');
$validator
->allowEmpty('position_applied_for');
$validator
->integer('age')
->allowEmpty('age');
$validator
->dateTime('date_deleted')
->allowEmpty('date_deleted');
$validator
->dateTime('date_modified')
->allowEmpty('date_modified');
$validator
->dateTime('date_created')
->requirePresence('date_created', 'create')
->notEmpty('date_created');
return $validator;
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
* @return \Cake\ORM\RulesChecker
*/
public function buildRules(RulesChecker $rules)
{
$rules->add($rules->isUnique(['case_reference_i_d']));
$rules->add($rules->existsIn(['client_id'], 'Clients'));
return $rules;
}
/**
* Gets Case count by age 0-10,10-15,15> days
* @return Array ['count0to10'=>00,'count10to15'=>00,'count15'=>00]
*/
public function getCaseCountByAge()
{
$casesTable = TableRegistry::get("Icases");
$query = $casesTable->find("all",['condition'=>['Icases.date_created >' => new DateTime('-10 days')]]);
$data = $query->execute();
return $data->fetch('assoc');
}
}
答案 0 :(得分:2)
您应该根据CakePHP official Doc
阅读并练习更多内容public function getCaseCountByAge()
{
$casesTable = TableRegistry::get("Icases");
$query = $casesTable->find("all",['condition'=>['Icases.date_created >' => new DateTime('-10 days')]]);
$data = $query->execute();
return $data->fetch('assoc');
}
可能非常简单
public function getCaseCountByAge()
{
return $this->find("all",['condition'=>['Icases.date_created >' => new DateTime('-10 days')]])->all();
}