Cakephp 3 loading HasOne inside HasMany association - cakephp

I am using Cakephp 3.2.10
I am able to load associated data with hasOne association and hasMany association. However, I have one hasOne inside the table associated with hasMany, and that gives error.
I have following tables
companies
company_revenues
currencies
states
countries.
CompaniesTable class has hasOne associations with states,countries and it works. CompaniesTable has hasMany association with CompanyRevenuesTable, and it works. CompanyRevenues table has a hasOne association with currencies, this 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 to export object: CompanyRevenues is not associated with Currencies)
}
I think the error is because of _ in my table company_revenues.
Can any one guide me please ?
Actually, in the profiles action, i do not want to load company revenues separately, as they come along with company.
I tried the following originally :
$company = $this->Companies->find()
->where(['company_id' => $id])
->contain(
[
'CompanyRevenues' => ["Currencies"]
])->first();
But that give error :
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

I would suggest adding the reverse relationships as well and see what happens.
By reverse relationships I mean belongsTo in your currencies and companyRevenues classes.
Currencies belongTo companyRevenues and
CompanyRevenues belongTo company class.
See what your debugging provides.

Related

How to modify join conditions CakePHP

That is my Query object dump from debug...
I want to add more condition in method JOIN clause or i want setting default condition when execute joins by CakePHP.
My code:
public function beforeFind(Event $event, Query $query)
{
if ($query->join()) {
foreach ($query->join() as $key => &$join) {
$join['conditions'] = new QueryExpression('"aaaa" = "bbbbb"');
}
return $query;
}
}
You can check CakePHP Docs.
I believe you searching something like this:
class CustomerTable extends Table
{
public function initialize(array $config)
{
$this->hasOne('User')
->setName('User')
->setConditions(['User.active' => '1']) // or any other additional clause
->setDependent(true);
}
}
I whould sugggest you to use innerJoinWith, if you need to hook your normal association behaviour.
As a hardcode solution (that what I cann see from your code samples), read join method docs:
$query->join([
'user' => [
'table' => 'user',
'type' => 'INNER',
'conditions' => '"aaaa" = "bbbbb"'
]
]);
Now some telepathy,.. abracadabra,... wait-wait,... try this:
public function beforeFind(Event $event, Query $query)
{
$joins = $query->join();
if ($joins) {
foreach ($joins as $key => &$join) {
if ($key == 'user')
$join['conditions'] = ($join['conditions'] ? ' AND ' : '') . ' "aaaa" = "bbbbb"';
}
return $joins ? $query->join($joins) : $query;
}
}

Cakephp 3.5 - Entity - Virtual Field not showing

I have defined in an entity this :
protected $_virtual = [ 'full_name' ];
protected function _getFullName()
{
return( $this->_properties['firstname'] . ' ' . $this->_properties['lastname'] );
}
But the full_name field is not retrieved by any query ( paginator or find('all') ) ... when the table is referred as an associated table.
The main table is GenPersons.
In that case, the field is showed correctly.
But then the I make
$this->paginate = [ 'contain' => ['GenPersons'] ]
from the AerPilots controller ( AerPilots is a model ) ; and try to get the field as
$aerPilot->gen_person->full_name;
nothing is showed.
Am I forgetting something ?
I found it.
The problem was in the association on the model.
The GenPersons table belongs to General plugin.
The AerPilots table belongs to Aeronautical plugin.
When I baked the model for AerPilots, it generated this code :
$this->belongsTo('GenPersons', [
'foreignKey' => 'gen_person_id',
'className' => '**Aeronautical**.GenPersons'
]);
And it must be :
$this->belongsTo('GenPersons', [
'foreignKey' => 'gen_person_id',
'className' => '**General**.GenPersons'
]);

CakePHP 3 : Unknown method

I am creating a function in model to find all related services.
function in ServiceCategory.php
class ServiceCategory extends Entity
{
public function relatedServices($id)
{
return $this->find('all', [
'conditions' => [
'where' => [
'id !=' => $id
],
'limit' => 5
]
]);
}
}
And calling in ServiceCategoriesController.php
public function view($id = null)
{
$serviceCategory = $this->ServiceCategories->get($id, [
'contain' => ['Services']
]);
$relatedServices = $this->ServiceCategories->relatedServices($id);
$this->set('serviceCategory', $serviceCategory);
$this->set('relatedServices', $relatedServices);
$this->set('_serialize', ['serviceCategory']);
}
But it gives Unknown method 'relatedServices'
Is there any thing wrong I am doing ?
The code is in the wrong class
In the question:
class ServiceCategory extends Entity
This is an entity class
$relatedServices = $this->ServiceCategories->relatedServices($id);
This is making a call on a table object, table objects and entities do not inherit from each other, the method is unavailable to the table class.
Move the code to the table class
The direct solution is to move the code to the table class:
// src/Model/Table/ServiceCategoriesTable.php
namespace App\Model\Table;
class ServiceCategoriesTable extends Table
{
public function relatedServices($id)
{
return $this->find('all', [
'conditions' => [
'where' => [
'id !=' => $id
],
'limit' => 5
]
]);
}
Though the arguably correct/better way to do that is to implement a finder:
// src/Model/Table/ServiceCategoriesTable.php
namespace App\Model\Table;
use Cake\ORM\Query;
use \InvalidArgumentException;
class ServiceCategoriesTable extends Table
{
public function findRelatedServices(Query $query, array $options)
{
if (!isset($options['id'])) {
$message = sprintf('No id in options: %s', json_encode($options));
throw new InvalidArgumentException($message);
}
$query->where(['id !=' => $options['id']);
return $query;
}
Which would be called in exactly the same way as other find calls:
$relatedServices = $this->ServiceCategories->find(
'relatedServices',
['id' => $id]
);

Saving associated models in Cakephp 3

I have a form that collects data about an Article, and I want to save that data, as well as for a model called Abstract, where an Article hasMany Abstracts. My models look like this:
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class AbstractsTable extends Table
{
public function initialize(array $config)
{
$this->belongsTo('Articles');
}
public function validationDefault(Validator $validator)
{
$validator
->notEmpty('body');
return $validator;
}
}
And
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Validation\Validator;
class ArticlesTable extends Table
{
public function initialize(array $config)
{
$this->addBehavior('Timestamp');
$this->hasMany('Abstracts');
}
public function validationDefault(Validator $validator)
{
$validator ->notEmpty('category')
return $validator;
}
}
My input form has a field named 'abstracts.body', and in my ArticlesController I have this function:
public function add()
{
$data = $this->request->data;
$article = $this->Articles->newEntity($data, [
'associated' => ['Abstracts']
]);
if ($this->request->is('post')) {
$article->user_id = $this->Auth->user('id');
$data['abstracts']['user_id'] = $article->user_id;
$data['abstracts']['approved'] = 0;
$article = $this->Articles->patchEntity($article, $data, [
'associated' => ['Abstracts']
]);
if ($this->Articles->save($article, [ 'validate' => false,
'associated' => ['Abstracts']
]) )
{
$this->Flash->success(__('Your article has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('Unable to add your article.'));
}
$this->set('article', $article);
}
My Abstracts table is pretty straightforward:
CREATE TABLE 'abstracts' ('id' INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, 'article_id' INTEGER , 'user_id' INTEGER , 'body' TEXT, 'approved' BOOLEAN )
From debugging I can see that I have the correct 'abstracts' array within my $data (in add()), but it doesn't appear to ever try to save it to the database. Can someone please point out my error? Thanks!
Got it.
I started going wrong here:
My input form has a field named 'abstracts.body'
Because it's a hasMany relationship, I need to have that input be 'abstracts.0.body'
Then the rest of LeWestopher's answer will work-- adding an index to the fields I want to fill in from the Controller, so $data[abstracts][0]['user_id'] => ... and so on. Thanks!
You're post processing your $data['abstracts'] array incorrectly resulting in the association not saving. $data['abstracts'] is expected to be an array of Abstracts. Your issue lies here:
$data['abstracts']['user_id'] = $article->user_id;
$data['abstracts']['approved'] = 0;
You should be able to fix this pretty easily by changing this to:
foreach($data['abstracts'] as $index => $abstract) {
$abstract['user_id'] = $article->user_id;
$abstract['approved'] = 0;
$data['abstracts'][$index] = $abstract;
}
This should correctly iterate over your array of abstracts, set the user_id and approved keys appropriately and then it should save correctly.
CakePHP 3.x Documentation on Saving Associations
EDIT: Very interesting issue indeed. Try it without using patchEntity, and use newEntity by itself instead:
public function add()
{
if ($this->request->is('post')) {
$data = $this->request->data;
// Post process abstracts objects
foreach($data['abstracts'] as $index => $abstract) {
$abstract['user_id'] = $article->user_id;
$abstract['approved'] = 0;
$data['abstracts'][$index] = $abstract;
}
// Build newEntity
$article = $this->Articles->newEntity($data, [
'associated' => ['Abstracts']
]);
// Save our entity with associations
if ($this->Articles->save($article, [
'validate' => false,
'associated' => ['Abstracts']
])) {
$this->Flash->success(__('Your article has been saved.'));
return $this->redirect(['action' => 'index']);
}
// On save fail
$this->Flash->error(__('Unable to add your article.'));
$this->set('article', $article);
}
}
EDIT 2: Your issue looks like it's definitely in your form helper. Your current form helper input creates an $data array that looks like this:
$data = [
'abstracts' => [
'body' => 'example text'
],
'category' => 'Science'
];
Which SHOULD look like:
$data = [
'abstracts' => [
['body' => 'example text'],
['body' => 'Im your second abstract'],
['body' => 'Abstract three!']
],
'category' => 'Science'
];
The issue lies in:
abstracts.body
Which should read as (in array dot notation):
// abstracts.0.body
echo $this->Form->input('abstracts.0.body', [
'label' => 'summary of article',
'maxlength' =>'440',
'rows' => '7'
]);
I believe that should be the last issue you run into.

Cakephp 3, Saving form data on association tables

Trying to set up an example on how to use join table with extra data I have the following set:
table students: id, name, [...]
table courses: id, title, [...]
join table courses_students: id, course_id, student_id, grade, hours_attended
The two base table's :
class StudentsTable extends Table {
public function initialize(array $config) {
$this->belongsToMany('Courses', [
'alias' => 'Courses',
'foreignKey' => 'student_id',
'targetForeignKey' => 'course_id',
'joinTable' => 'courses_students',
'through' => 'CoursesStudents',
]);
}
class CoursesTable extends Table {
public function initialize(array $config) {
$this->belongsToMany('Students', [
'alias' => 'Students',
'foreignKey' => 'course_id',
'targetForeignKey' => 'student_id',
'joinTable' => 'courses_students',
'through' => 'CoursesStudents',
]);
}
And the association table:
class CoursesStudentsTable extends Table {
public function initialize(array $config) {
$this->belongsTo('Courses', [
'alias' => 'Courses',
'foreignKey' => 'course_id'
]);
$this->belongsTo('Students', [
'alias' => 'Students',
'foreignKey' => 'student_id'
]);
}
Having some courses available in the table, I try to add and edit student records. Setting
[courses] => [_ids]
in the student record creates the records in both students table and the association table.
How should the post data array be formed in order to be able to store the grade and hours_attended fields in the association table when saving the student record?
You should configure your form field as the following assuming you are in the Courses form.
echo $this->Form->create($courses);
echo $this->Form->input("Courses.id");
echo $this->Form->input("Courses.title");
echo $this->Form->input("Courses.courses_students.$i.grade");
echo $this->Form->input("Courses.courses_students.$i.hours_attended");
The basic idea is that your forms should exactly follow how the data is formatted when retrieved in your controller.
This will then format your data correctly for you.
Then in your controller, you'll need to pass the associations to patch your entity.
$courses = $this->Courses->patchEntity($this->request->data(), ['associations' => ['CoursesStudents']]);
This will merge your associated request data in your entity, so you can save it.

Resources