Cakephp3在HasMany关联中加载HasOne



我正在使用Cakephp 3.2.10

我能够加载具有hasOne关联和hasMany关联的关联数据。然而,我在与hasMany关联的表中有一个hasOne,这会产生错误。

我有以下表格

  1. 公司
  2. 公司_场地
  3. 货币
  4. 国家

CompaniesTable类与州、国家有hasOne关联,并且有效。CompaniesTable与CompanyRevenuesTable有许多关联,而且它很有效。CompanyRevenue表与货币有hasOne关联,这会产生错误。

我的相关代码:

CompaniesTable.php

<?php
namespace AdminModelTable;
use CakeORMTable;
use CakeORMQuery;
use CakeORMRulesChecker;
class CompaniesTable extends Table
{
public function initialize(array $config)
{
$this->primaryKey('company_id');
$this->addAssociations(
[
'hasOne' =>
[
'Countries' =>
[ 'className' => 'Countries','foreignKey' => 'id','bindingKey' => 'company_country_id' ]
,'States' =>
[ 'className' => 'States','foreignKey' => 'id','bindingKey' => 'company_state_id' ]
,'Sectors' =>
[ 'className' => 'Sectors','foreignKey' => 'id','bindingKey' => 'company_sector_id' ]
]
,'hasMany' =>
[
'CompanyRevenues' =>
[ 'className' => 'CompanyRevenues','foreignKey' => 'revenue_company_id','bindingKey' => 'company_id' ]
]
]);
}
public function buildRules(RulesChecker $rules)
{
$rules->add( $rules->isUnique(['company_name']) );
return $rules;
}

public function compsTotalCount( Query $query )
{
$result = $query->select(['companiesCount' => $query->func()->count('*')])->first();
return $result;
}   
public function findPopular( Query $query )
{
$result = $query->where(['times_viewed >' => 10]);
// debug($result);
return $result;
}   
}
?>

CompanyRevenuesTable.php

<?php
namespace AdminModelTable;
use CakeORMTable;
use CakeORMQuery;
use CakeORMRulesChecker;
class CompanyRevenuesTable extends Table
{
public function initialize(array $config)
{
$this->table('company_revenues');
$this->addAssociations(
[
'hasOne' =>
[
'Currencies' =>
[ 'className' => 'Currencies','foreignKey' => 'id','bindingKey' => 'revenue_currency_id' ]
]
]);
}
}
?>

我的公司控制器.php

剖面作用

public function profile( $id = null )
{
$company = $this->Companies->find()
->where(['company_id' => $id])
->contain(['CompanyRevenues'])->first();
if( ! $this->request->is('ajax') )
{
$companyRevenuesTable = TableRegistry::get('CompanyRevenues');
$companyRevenues = $companyRevenuesTable->find()
->where(['revenue_company_id' => $company->company_id])
->contain(['Currencies']);
debug($companyRevenues);
}

if( $this->request->is('ajax') )
{
$company = $this->Companies->patchEntity($company, $this->request->data);
$company->company_last_updated = date("Y-m-d H:i:s");
$ajaxRespArr = array();
if( $this->Companies->save($company) )
{
$ajaxRespArr["result"] = 1;
}
else
{
$ajaxRespArr["result"] = 0;
}
$this->set( 'ajaxRespArr',$ajaxRespArr );
$this->set('_serialize', ['ajaxRespArr']);
}
else
{
$this->set('company', $company);
}
}

$$companyRevenue 上的调试

给出错误

pluginsAdminsrcControllerCompaniesController.php (line 92)
object(CakeORMQuery) {
(unable to export object: CompanyRevenues is not associated with Currencies)
}

我想这个错误是因为我的company_renciences表中的_。

有人能给我指路吗?

事实上,在配置文件操作中,我不想单独加载公司收入,因为它们与公司一起出现。

我最初尝试了以下内容:

$company = $this->Companies->find()
->where(['company_id' => $id])
->contain(
[
'CompanyRevenues' => ["Currencies"]
])->first();

但这给出了错误:

CompanyRevenues is not associated with Currencies InvalidArgumentException
Could this be caused by using Auto-Tables?
Some of the Table objects in your application were created by instantiating "CakeORMTable" instead of any other specific subclass.
This could be the cause for this exception. Auto-Tables are created for you under the following circumstances:
The class for the specified table does not exist.
The Table was created with a typo: TableRegistry::get('Atricles');
The class file has a typo in the name or incorrect namespace: class Atricles extends Table
The file containing the class has a typo or incorrect casing: Atricles.php
The Table was used using associations but the association has a typo: $this->belongsTo('Atricles')
The table class resides in a Plugin but no plugin notation was used in the association definition.

Please try correcting the issue for the following table aliases:
CompanyRevenues

我建议添加反向关系,看看会发生什么。我所说的反向关系是指您的货币和公司收入类别中的belongsTo。

  • 货币属于公司收入和
  • 公司收入属于公司类别

查看您的调试提供了什么。

最新更新