1

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'])
ndm
  • 59,784
  • 9
  • 71
  • 110
Fury
  • 4,643
  • 5
  • 50
  • 80

1 Answers1

2

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.

ndm
  • 59,784
  • 9
  • 71
  • 110