Retrieve related data (hasMany) in CakePHP 3
<pre><code>Events hasMany TicketTypes
TicketTypes belogsTo Events
</code></pre>

<p>I am trying to retrieve all events with associated ticket types:</p>

<pre><code>$query= $this-&gt;Events
    -&gt;find()
    -&gt;select(['id', 'name'])
    -&gt;autoFields(false)
    -&gt;contain(['TicketTypes' =&gt; function($q) { 
        return $q-&gt;select(['TicketTypes.id', 'TicketTypes.name']); }])
;
</code></pre>

<p>SQL query generated:</p>

<pre><code>SELECT Events.id AS `Events__id`, Events.name AS `Events__name` FROM events Events
</code></pre>

<p>But what I expected is:</p>

<pre><code>SELECT Events.id AS `Events__id`, Events.name AS `Events__name`, TicketTypes.id AS `TicketTypes__id`, TicketTypes.name AS `TicketTypes__name` FROM events Events LEFT JOIN ticket_types TicketTypes ON Events.id = (TicketTypes.event_id)
</code></pre>

<p>This is how my models are configured:</p>

<pre><code>class EventsTable extends Table
{
    public function initialize(array $config)
    {
        $this-&gt;displayField('name');

        $this-&gt;addAssociations([
            'hasMany'=&gt; ['TicketTypes']
        ]);
    }
}

class TicketTypesTable extends Table
{
    public function initialize(array $config)
    {
        $this-&gt;displayField('name');

        $this-&gt;addAssociations([
            'belongsTo' =&gt; ['Events']
        ]); 
    }
}
</code></pre>

<p>Here is the result of debugging my find query:</p>

<pre><code>object(Cake\ORM\Query) {

    '(help)' =&gt; 'This is a Query object, to get the results execute or iterate it.',
    'sql' =&gt; 'SELECT Events.id AS `Events__id`, Events.name AS `Events__name` FROM events Events',
    'params' =&gt; [],
    'defaultTypes' =&gt; [
        'Events.id' =&gt; 'integer',
        'id' =&gt; 'integer',
        'Events.name' =&gt; 'string',
        'name' =&gt; 'string',
        'Events.datetime_start' =&gt; 'datetime',
        'datetime_start' =&gt; 'datetime',
        'Events.datetime_end' =&gt; 'datetime',
        'datetime_end' =&gt; 'datetime',
        'Events.created' =&gt; 'datetime',
        'created' =&gt; 'datetime',
        'Events.modified' =&gt; 'datetime',
        'modified' =&gt; 'datetime',
        'Events.slug' =&gt; 'string',
        'slug' =&gt; 'string',
        'TicketTypes.id' =&gt; 'integer',
        'TicketTypes.event_id' =&gt; 'integer',
        'event_id' =&gt; 'integer',
        'TicketTypes.name' =&gt; 'string',
        'TicketTypes.description' =&gt; 'text'
    ],
    'decorators' =&gt; (int) 0,
    'executed' =&gt; false,
    'hydrate' =&gt; true,
    'buffered' =&gt; true,
    'formatters' =&gt; (int) 0,
    'mapReducers' =&gt; (int) 0,
    'contain' =&gt; [
        'TicketTypes' =&gt; [
            'queryBuilder' =&gt; object(Closure) {

            }
        ]
    ],
    'matching' =&gt; [],
    'extraOptions' =&gt; [],
    'repository' =&gt; object(App\Model\Table\EventsTable) {

        'registryAlias' =&gt; 'Events',
        'table' =&gt; 'events',
        'alias' =&gt; 'Events',
        'entityClass' =&gt; 'App\Model\Entity\Event',
        'associations' =&gt; [
            (int) 0 =&gt; 'tickettypes'
        ],
        'behaviors' =&gt; [],
        'defaultConnection' =&gt; 'default',
        'connectionName' =&gt; 'default'

    }

}
</code></pre>

<p>And here is the result of debugging <code>$query-&gt;all()</code>:</p>

<pre><code>object(Cake\ORM\ResultSet) {

    'items' =&gt; [
        (int) 0 =&gt; object(App\Model\Entity\Event) {

            'id' =&gt; (int) 101,
            'name' =&gt; 'qwertyuiop',
            'ticket_types' =&gt; [],
            '[new]' =&gt; false,
            '[accessible]' =&gt; [
                '*' =&gt; true
            ],
            '[dirty]' =&gt; [],
            '[original]' =&gt; [],
            '[virtual]' =&gt; [],
            '[errors]' =&gt; [],
            '[repository]' =&gt; 'Events'

        },
    ...
</code></pre>

<p>As you can see in this line <code>'ticket_types' =&gt; []</code> ticket types are not being returned by the query.</p>

<p>What can I do to retrieve <code>TicketTypes</code> data?</p>

<p>Thanks.</p>