How to change the join type of a contained association per find() call? - cakephp

How to reset the type of joins in different places?
Here are my tables:
Tenancy Table
class TenancyTable extends Table
{
/**
* Initialize method
*
* #param array $config The configuration for the Table.
* #return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->table('tenancy');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('Properties', [
'foreignKey' => 'property_id',
'className' => 'property',
'joinType' => 'INNER'
]);
Property Table
class PropertyTable extends Table
{
/**
* Initialize method
*
* #param array $config The configuration for the Table.
* #return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->table('property');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->hasMany('Tenancies', [
'foreignKey' => 'property_id',
'className' => 'tenancy',
'joinType' => 'LEFT'
]);
For example change the Tenancy join type to Right
$setting = [
'contain' => [
'Tenancies' => [
'joinType' => 'RIGHT'
]
]
];
$properties = $this->find('all', $setting)
->hydrate(false)
->select(['Property.id', 'Property.company_id', 'Property.address1', 'Property.postcode'])

You would do it exactly the way you are showing it, ie by specifying the joinType option in contain settings.
However, since you are querying from the Property table, this will have no effect, as hasMany associations are being retrieved in a separate query, so no joins involved. It would work for hasOne and belongsTo associations, which are being retrieved in the same query.

Related

Could not view with composite pK CakePHP4

In cakephp-4.x I could not access Controller's view action for composite primary key table. (http://localhost:8765/invoice-items/view/1)
Here are samples of the code created by cake bake:
InvoiceItemsTable calss in which the primary key is defined as composite.
class InvoiceItemsTable extends Table
{
/**
* Initialize method
*
* #param array $config The configuration for the Table.
* #return void
*/
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('invoice_items');
$this->setDisplayField(['item_id', 'invoice_id', 'unit_id']);
$this->setPrimaryKey(['item_id', 'invoice_id', 'unit_id']);
$this->addBehavior('Timestamp');
$this->belongsTo('Items', [
'foreignKey' => 'item_id',
'joinType' => 'INNER',
]);
$this->belongsTo('Invoices', [
'foreignKey' => 'invoice_id',
'joinType' => 'INNER',
]);
$this->belongsTo('Units', [
'foreignKey' => 'unit_id',
'joinType' => 'INNER',
]);
}
...
InvoiceItemsController view method:
/**
* View method
*
* #param string|null $id Invoice Item id.
* #return \Cake\Http\Response|null|void Renders view
* #throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$invoiceItem = $this->InvoiceItems->get($id, [
'contain' => ['Items', 'Invoices', 'Units'],
]);
$this->set(compact('invoiceItem'));
}
Finally a screen shot of invoice_items table structure from phpmyadmin:
I have tried to access the view like (http://localhost:8765/invoice-items/view/1,1,6) but I got the same error ... with primary key ['1,1,6']. I do not know how to represent the composite primary key in the URL? or what is the problem?
I use CakePHP version 4.4.2
Table::get() expects composite keys to be passed as arrays, eg [1, 1, 6].
Assuming you're using the fallback routes from the default app skeleton, you can pass additional arguments as path parts, eg:
/invoice-items/view/1/1/6
and accept them in your controller action like:
public function view($itemId, $invoiceId, $unitId)
and build an array from the accordingly to pass to get() as the "id":
$this->InvoiceItems->get([$itemId, $invoiceId, $unitId], /* ... */)
In case you're using custom routes with fixed parameters, add additional ones in whatever form you like, for example with dashes:
$routes
->connect(
'/invoice-items/view/{itemId}-{invoiceId}-{unitId}',
['controller' => 'InvoiceItems', 'action' => 'view']
)
->setPass(['itemId', 'invoiceId', 'unitId'])
->setPatterns([
'itemId' => '\d+',
'invoiceId' => '\d+',
'unitId' => '\d+',
]);
then your URL would look like:
/invoice-items/view/1-1-6
See also
Cookbook > Routing > Route Elements
Cookbook > Routing > Passing Parameters to Action
Cookbook > Routing > Fallbacks Method

cakephp 4 find method with contain is skipping some records from subtable

I have master table portfolios and there are 2 child tables portfoliotags and p_snaps
models are as below
class PortfoliosTable extends Table
{
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('portfolios');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->hasMany('PSnaps', [
'foreignKey' => 'portfolio_id',
]);
$this->hasMany('PortfolioTags', [
'foreignKey' => 'portfolio_id',
]);
}
}
class PortfolioTagsTable extends Table
{
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('portfolio_tags');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->belongsTo('Portfolios', [
'foreignKey' => 'portfolio_id',
'joinType' => 'INNER',
]);
$this->belongsTo('Tags', [
'foreignKey' => 'tag_id',
'joinType' => 'INNER',
]);
}
}
class PSnapsTable extends Table
{
public function initialize(array $config): void
{
parent::initialize($config);
$this->setTable('p_snaps');
$this->setDisplayField('title');
$this->setPrimaryKey('id');
$this->belongsTo('Portfolios', [
'foreignKey' => 'portfolio_id',
'joinType' => 'INNER',
]);
}
}
find() method in controller is as below
$pfolios = $this->Portfolios->find('all')
->select(['Portfolios.id','Portfolios.client','Portfolios.country','Portfolios.url'])
->where(['Portfolios.status'=>1])
->order(['Portfolios.order_at'=>'asc','Portfolios.id'=>'asc'])
->limit(8)
->contain([
'PSnaps'=>function($q){
return $q
->select(['PSnaps.portfolio_id','PSnaps.snap'])
->where(['PSnaps.status'=>1])
->order(['PSnaps.order_at'])
->limit(1);
},
'PortfolioTags.Tags'=>function($q2){
return $q2
->order(['Tags.tag']);
}
])
->toList();
debug($pfolios);exit;
It is retuning PSnaps inside first record of Portfolio, while in all other records it is empty array
while I know there are records in database I tried using below query in mysql too
SELECT p.id,s.snap FROM `portfolios` p INNER join p_snaps s on p.id=s.portfolio_id
it is returning records as below
id snap
1 s1.png
2 pers.png
3 gmap.png
4 ita.png
5 soapd.png
6 chat.png
7 aissmo.png
8 zippy.png
2 pereport.png
This is because of limit() inside PSnaps, if it is commented it will show all PSnap records under each Portfolios records.
To handle this situation I fix it by adding hasOne relation in PortfoliosTable.php as
$this->hasOne('FirstPSnaps', [
'className' => 'PSnaps',
'foreignKey' => 'portfolio_id',
'strategy' => 'select',
'sort' => ['FirstPSnaps.order_at' => 'ASC'],
'conditions' => function ($e, $query) {
return [];
}]);
now call in controller will be changed to as below
$pfolios = $this->Portfolios->find('all')
->select(['Portfolios.id','Portfolios.client','Portfolios.country','Portfolios.url'])
->where(['Portfolios.status'=>1])
->order(['Portfolios.order_at'=>'asc','Portfolios.id'=>'asc'])
->limit(8)
->contain([
/********instead of PSnap I used FirstPSnaps *********/
'FirstPSnaps'=>function($q){
return $q
->select(['FirstPSnaps.portfolio_id','FirstPSnaps.snap'])
->where(['FirstPSnaps.status'=>1])
->order(['FirstPSnaps.order_at']);
},
'PortfolioTags.Tags'=>function($q2){
return $q2
->order(['Tags.tag']);
}
])
->toList();
debug($pfolios);exit;

Struggling to save in 2 tables with hasOne association with cakePHP 3

I’m struggling with the hasOne association. Here are my 2 tables :
Actually the ‘Etudiant’ (means student) has one User associated to it with the foreign key id present in both table which is the primary key of Users.
Here is the model EtudiantTable (student) :
class EtudiantTable extends Table
{
/**
* Initialize method
*
* #param array $config The configuration for the Table.
* #return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('etudiant');
$this->setDisplayField('ETUDIANT_ID');
$this->setPrimaryKey('ETUDIANT_ID');
$this->addBehavior('Timestamp');
$this->belongsTo('Maitredestage', [
'foreignKey' => 'MAITREDESTAGE_ID',
'joinType' => 'INNER'
]);
$this->belongsToMany('Tuteuriut', [
'foreignKey' => 'ETUDIANT_ID',
'targetForeignKey' => 'TUTEURIUT_ID',
'joinTable' => 'Suivre'
]);
$this->belongsTo('Users', [
'foreignKey' => 'ID',
'joinType' => 'INNER'
]);
}
And here is the add function in the EtudiantController :
public function add()
{
$etudiant = $this->Etudiant->newEntity();
if ($this->request->is('post')) {
$etudiant = $etutable->patchEntity($etudiant, $this->request->data(), [
'associated' => ['Users']
]);
if ($this->Etudiant->save($etudiant)){
$this->Flash->success(__('The etudiant has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The etudiant could not be saved. Please, try again.'));
}
$this->set(compact('etudiant'));
}
And my usersTable model is like this :
$this->belongsTo('Users', [ 'foreignKey' => 'ID', 'joinType' => 'INNER' ]);
I want to do this : when i insert an Etudiant it inserts a new user and both have the same id.
But currently when i insert an Etudiant i get an error saying “no default value…id is empty” but the id is supposed to be inserted automatically.
I'm completely stuck so i need your help. Thanks in advance.

CakePHP 3 - Users belongsToMany Users

I have a specific request, to build an association between users. This causes me confusion, how to reduce duplicate associations, query and results?
The starting point would look like this?
// UsersTable
$this->belongsToMany('Users', [
'through' => 'Connections',
]);
How to fetch all associations in one query, regardless of whether users key in "user_from" or "user_to" field?
How about using aliases?
Your users table:
class UsersTable extends Table
{
public function initialize(array $config)
{
$this->hasMany('ToConnections', [
'className' => 'Connections',
'foreignKey' => 'user_to'
]);
$this->hasMany('FromConnections', [
'className' => 'Connections',
'foreignKey' => 'user_from'
]);
}
}
And your connections table:
class ConnectionsTable extends Table
{
public function initialize(array $config)
{
$this->belongsTo('ToUsers', [
'className' => 'Users',
'foreignKey' => 'user_to'
]);
$this->belongsTo('FromUsers', [
'className' => 'Users',
'foreignKey' => 'user_from'
]);
}
}
You can then use contain() to load associated models as required.
$query = $conections->find()->contain([
'ToUsers',
'FromUsers'
]);
$recipients = TableRegistry::get('users');
$query = $recipients->find()->contain([
'ToConnections.FromUsers',
]);

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