Cakephp 3 loading HasOne inside HasMany association -
i using cakephp 3.2.10
i able load associated data hasone association , hasmany association. however, have 1 hasone inside table associated hasmany, , gives error.
i have following tables
- companies
- company_revenues
- currencies
- states
- countries.
companiestable class has hasone associations states,countries , works. companiestable has hasmany association companyrevenuestable, , works. companyrevenues table has hasone association currencies, gives error.
my relevant code :
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' ] ] ]); } } ?>
my companiescontroller.php
profile action
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); } }
the debug on $$companyrevenues
gives error
\plugins\admin\src\controller\companiescontroller.php (line 92) object(cake\orm\query) { (unable export object: companyrevenues not associated currencies) }
i think error because of _ in table company_revenues.
can 1 guide me please ?
actually, in profiles action, not want load company revenues separately, come along company.
i tried following :
$company = $this->companies->find() ->where(['company_id' => $id]) ->contain( [ 'companyrevenues' => ["currencies"] ])->first();
but give error :
companyrevenues not associated currencies invalidargumentexception caused using auto-tables? of table objects in application created instantiating "cake\orm\table" instead of other specific subclass. cause exception. auto-tables created under following circumstances: class specified table not exist. table created typo: tableregistry::get('atricles'); class file has typo in name or incorrect namespace: class atricles extends table file containing class has typo or incorrect casing: atricles.php table used using associations association has typo: $this->belongsto('atricles') table class resides in plugin no plugin notation used in association definition. please try correcting issue following table aliases: companyrevenues
i suggest adding reverse relationships , see happens. reverse relationships mean belongsto in currencies , companyrevenues classes.
- currencies belongto companyrevenues ,
- companyrevenues belongto company class.
see debugging provides.
Comments
Post a Comment