我正面临着我无法解决的CakePHP2问题。我已经通过"belongsTo“定义将模型关联到另一个模型,并且它很好,但是当我尝试检索关联的数据时,查询死了以下错误。它似乎在表字段名方面有问题。会计和CLetter表都有"date“列。我应该如何构建查询,以便从包含日期列的两个表中提取所有数据?
更好的是,这种配置可以在会计模型中完成。所以我可以像这样做任何我想做的事情(目前产生了以下错误消息)
$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模型关联:
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')]],
]);EDIT2
好的。我可以通过在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);
}EDIT3
问题仍然出现。EDIT2中的解决方案会导致可容纳行为完全停止工作。这里有什么建议吗?
发布于 2014-04-22 17:59:50
指定模型之间的关系时,还可以设置默认情况下要检索的字段
从manual
字段:在获取关联的模型数据时要检索的字段列表。默认情况下返回所有字段。
发布于 2014-04-22 19:49:53
好的。问题很奇怪,但很明显: 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(*)");https://stackoverflow.com/questions/23211486
复制相似问题