2016-06-11 71 views
0

内部3装载HasOne我使用CakePHP的3.2.10CakePHP的的hasMany关联

我能够与hasOne关联和的hasMany关联加载相关联的数据。但是,我有一个与hasMany关联的表中有一个,并且出现错误。

我有以下各表

  1. 公司
  2. company_revenues
  3. 货币
  4. 状态
  5. 国家。

公司表类已经与州,国家有联系,它的工作。 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 

回答

0

我建议增加反向关系,以及看看会发生什么。 通过反向关系我的意思是在您的货币和公司收益类别belongsTo。

  • 货币belongsTo关系companyRevenues和
  • CompanyRevenues通过belongsTo公司类。

看看你的调试提供了什么。