0
內部3裝載HasOne我使用CakePHP的3.2.10CakePHP的的hasMany關聯
我能夠與hasOne關聯和的hasMany關聯加載相關聯的數據。但是,我有一個與hasMany關聯的表中有一個,並且出現錯誤。
我有以下各表
- 公司
- company_revenues
- 貨幣
- 狀態
- 國家。
公司表類已經與州,國家有聯繫,它的工作。 CompaniesTable與CompanyRevenueTable有許多關聯,並且工作正常。 CompanyRevenues表與貨幣有一個hasOne關聯,這會產生錯誤。
我的相關代碼:
CompaniesTable.php
<?php
namespace Admin\Model\Table;
use Cake\ORM\Table;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
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 Admin\Model\Table;
use Cake\ORM\Table;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
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' ]
]
]);
}
}
?>
我CompaniesController.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);
}
}
調試上$$ companyRevenues
給出錯誤
\plugins\Admin\src\Controller\CompaniesController.php (line 92)
object(Cake\ORM\Query) {
(unable to export object: CompanyRevenues is not associated with Currencies)
}
我認爲錯誤是因爲_在我的表company_revenues。
任何人都可以引導我嗎?
實際上,在配置文件操作中,我不想單獨加載公司收入,因爲它們與公司一起出現。
我試過本來以下幾點:
$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 "Cake\ORM\Table" 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