Cakephp 3在HasMany协会中加载HasOne

时间:2016-06-11 12:47:12

标签: cakephp cakephp-3.0

我正在使用 Cakephp 3.2.10

我可以使用hasOne关联和hasMany关联加载关联数据。但是,我在与hasMany关联的表中有一个hasOne,这会产生错误。

我有以下表格

  1. 公司
  2. company_revenues
  3. 货币
  4. 状态
  5. 国家。
  6. CompaniesTable类与州,国家有关联,并且有效。 CompaniesTable与CompanyRevenuesTable有很多关联,它有效。 CompanyRevenues表与货币有一个hasOne关联,这会产生错误。

    我的相关代码:

    CompaniesTable.php

    USER_ID

    CompanyRevenuesTable.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;
        }   
    }
    ?>
    

    My CompaniesController.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' ]
    
                ]
            ]);
        }
    }
    ?>
    

    $$ companyRevenues上的调试

    给出错误

    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);
        }
    }
    

    我认为错误是由于我的表company_revenues中的_。

    任何人都可以指导我吗?

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

    我最初尝试了以下内容:

     \plugins\Admin\src\Controller\CompaniesController.php (line 92)
    
    object(Cake\ORM\Query) {
    (unable to export object: CompanyRevenues is not associated with Currencies)
     }
    

    但那会给出错误:

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

1 个答案:

答案 0 :(得分:0)

我建议添加反向关系,看看会发生什么。 通过反向关系,我的意思是属于您的货币和公司收入类。

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

查看调试内容。