Leftjoin and table object date

If I perform leftjoin in a query, the connection table does not have a date field - it is a string.

'UsersDocumentsOrders' => [
   'date' => '2020-07-02',
]

Why?

How do I make leftjoin so that a column is an object (Cake\I18n\FrozenTime) ??

Thank you

What does your join code look like, and what are the relevant associations?

Query:

        ->select([
            'UsersDocuments.id',
            'UsersDocumentsOrders.date',
        ])
        ->leftJoin(['UsersDocumentsOrders' => 'users_documents_orders'],'UsersDocumentsOrders.users_document_id = UsersDocuments.id')
        ->leftJoin(['UsersDocumentsItems' => 'users_documents_items'],'UsersDocumentsItems.users_document_id = UsersDocumentsOrders.users_document_id');

Model/Table/UsersDocumentsTable.php

    $this->hasMany('UsersDocumentsItems', [
        'foreignKey' => 'users_document_id',
        'dependent' => true,
    ]);
    $this->hasOne('UsersDocumentsOrders', [
        'foreignKey' => 'users_document_id',
        'dependent' => true,
    ]);

Model/Table/UsersDocumentsOrdersTable.php

    $this->belongsTo('UsersDocuments', [
        'foreignKey' => 'users_document_id',
        'joinType' => 'INNER',
        'dependent' => true,
    ]);

Model/Table/UsersDocumentsItemsTable.php

    $this->belongsTo('UsersDocuments', [
        'foreignKey' => 'users_document_id',
        'joinType' => 'INNER',
        'dependent' => true,
    ]);

By using leftJoin, I believe that you are telling Cake specifically what table to use under what alias, thus circumventing the association and not loading the table object, which is where it gets the types from. The documentation for adding joins includes details on how you can tell it the datatypes for columns in the join table.

But I think that if you instead use leftJoinWith, your code will get much simpler. Try just ->leftJoinWith('UsersDocumentsOrders') and see how that works.

1 Like

leftJoinWith it works…
And I have to call this:

$item->_matchingData['UsersDocumentsOrders']->date

I add to query:

        ->leftJoinWith('UsersDocumentsOrders')
        ->leftJoinWith('UsersDocumentsItems')
        ->contain('UsersDocumentsOrders');

And print this item:

$item->users_documents_order->date

Its ok?

Thank You!

1 Like