我正面临着我无法解决的CakePHP2问题。当我尝试检索具有以下错误的关联数据查询模具时,我将模型与#34; belongsTo"定义相关联并且它很好。它似乎与表字段名称有问题。会计和CLetter表都有" date"列。我应该如何构建查询以从包含日期列的两个表中获取所有数据?
更好的是,这种配置可以在会计模型中完成。所以我可以做任何我想要的地方(目前产生followinf错误信息)
$Accounting->find('all', 'recursive' => 2);
错误消息
Running AccountingTestCase
PDOEXCEPTION
SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'date' in field list is ambiguous
Test case: AccountingTestCase(testCancelAccounting)
Stack trace:
C:\xampp\htdocs\Galactica\lib\Cake\Model\Datasource\DboSource.php : 460
C:\xampp\htdocs\Galactica\lib\Cake\Model\Datasource\DboSource.php : 426
C:\xampp\htdocs\Galactica\lib\Cake\Model\Datasource\DboSource.php : 666
C:\xampp\htdocs\Galactica\lib\Cake\Model\Datasource\DboSource.php : 1077
C:\xampp\htdocs\Galactica\lib\Cake\Model\Model.php : 2750
C:\xampp\htdocs\Galactica\lib\Cake\Model\Model.php : 2722
C:\xampp\htdocs\Galactica\app\Plugin\AccountingPlugin\Test\Case\Model\AccountingTest.php : 34
AccountingTestCase::testCancelAccounting
C:\xampp\php\pear\PHPUnit\Framework\TestCase.php : 976
C:\xampp\php\pear\PHPUnit\Framework\TestCase.php : 831
C:\xampp\php\pear\PHPUnit\Framework\TestResult.php : 648
C:\xampp\php\pear\PHPUnit\Framework\TestCase.php : 776
C:\xampp\htdocs\Galactica\lib\Cake\TestSuite\CakeTestCase.php : 84
C:\xampp\php\pear\PHPUnit\Framework\TestSuite.php : 775
C:\xampp\php\pear\PHPUnit\Framework\TestSuite.php : 745
C:\xampp\php\pear\PHPUnit\TextUI\TestRunner.php : 349
C:\xampp\htdocs\Galactica\lib\Cake\TestSuite\CakeTestRunner.php : 63
C:\xampp\htdocs\Galactica\lib\Cake\TestSuite\CakeTestSuiteCommand.php : 97
C:\xampp\htdocs\Galactica\lib\Cake\TestSuite\CakeTestSuiteDispatcher.php : 247
C:\xampp\htdocs\Galactica\lib\Cake\TestSuite\CakeTestSuiteDispatcher.php : 100
C:\xampp\htdocs\Galactica\lib\Cake\TestSuite\CakeTestSuiteDispatcher.php : 117
C:\xampp\htdocs\Galactica\app\webroot\test.php : 92
1/1 test methods complete: 0 passes, 0 fails, 1 assertions and 1 exceptions.
查询代码本身(产生错误消息):
$accountingsList = $Accounting->find('all', [
'contain' => ['CLetter'],
'conditions' => ['Accounting.date' => date('Y-m-d')]
]);
会计模型协会:
public $belongsTo = [
'CLetter' => ['foreignKey' => 'f_letter'],
'ProfitLetter' => ['className' => 'CLetter', 'foreignKey' => 'f_profit_accounting_letter'],
'ContactBusiness' => ['foreignKey' => 'f_contact_business']
];
CLetter model association:
public $hasMany = array(
'Accounting' => array('foreignKey' => 'f_letter'),
'PayContract' => array('foreignKey' => 'f_letter'),
'PayContractLastInstallment' => array(
'className' => 'PayContract',
'foreignKey' => 'f_last_installment_letter'));
这很有效,但我真的希望将这个字段部分移动到模型关联中,我可以使用find->(' all')检索数据,而不需要额外的参数。这可能吗?
$accountingsList = $Accounting->find('all', [
'fields' => ['CLetter.*', 'Accounting.*'],
'conditions' => ['Accounting' => ['Accounting.date' => date('Y-m-d')]],
]);
确定。我能够通过向AppModel添加以下代码来解决这个问题,但这是否真的是解决这个问题的唯一方法?我认为从表中选择字段时,这应该是CakePHP中的默认行为。
public function beforeFind($queryData)
{
if (empty($queryData['fields'])) {
$schema = $this->schema();
foreach (array_keys($schema) as $field) {
$queryData['fields'][] = $this->alias . '.' . $field;
}
return $queryData;
}
return parent::beforeFind($queryData);
}
问题仍然存在。 EDIT2中的解决方案导致可包含的行为完全停止工作。有什么建议吗?
答案 0 :(得分:0)
答案 1 :(得分:0)
确定。问题很奇怪但显而易见:在CLetter模型中有以下定义,确实是一种暧昧:
public $virtualFields = array('Y' => "YEAR(date)", 'M' => "MONTH(date)", 'n' => "count(*)");
通过将其更改为:
来解决此问题public $virtualFields = array('Y' => "YEAR(CLetter.date)", 'M' => "MONTH(CLetter.date)", 'n' => "count(*)");