ikkez/f3-cortex

Has on cascading relations generates an array_shift() error

sejef opened this issue · 4 comments

sejef commented

Hi Christian and thank you for Cortex and all the sugars you developed for F3. They are a real pleasure to use.

I'm having some difficulties to use the "has" filter on cascading One-to-Many entities :
Activity->Levels->Schedules and Teacher->Schedules.
I'd like to fetch all the activites of a teacher.

My models are defined as follow (code is simplified):

Level 1: Activity

class Activity extends Cortex {
    protected $fieldConf = [ 
        'title' => ['type' => 'VARCHAR256'],
        'levels' => ['has-many' => ['Models\Level','activity']]
    ];
}

Level 2: Levels and teachers

class Level extends Cortex {
    protected $fieldConf = [ 
        'activity' => ['belongs-to-one' => 'Models\Activity'],
        'schedules' => ['has-many' => ['Models\Schedule','level']]
    ];
}
class Teacher extends Cortex {
    protected $fieldConf = [ 
        'schedules' => ['has-many' => ['Models\Schedule','teacher']]
    ];
}

Level 3: Schedules, which links teachers and levels

class Schedule extends Cortex {
    protected $fieldConf = [ 
        'level' => ['belongs-to-one' => 'Models\Level'],
        'teacher' => ['belongs-to-one' => 'Models\Teacher']
    ];
}

For a given teacher (by it's id) I'm trying to fetch all the Activities he is linked to with (in my TeacherController.php):

$activity = new Activity();
$activity->has('levels.schedules', ['teacher = ?', $teacherId]);
$activities = $activity->find();

Which results in a
Internal Server Error
array_shift() expects parameter 1 to be array, null given

[vendor/bcosca/fatfree-core/base.php:2261] Base->error()
[vendor/ikkez/f3-cortex/lib/db/cortex.php:1154] array_shift()
[vendor/ikkez/f3-cortex/lib/db/cortex.php:737] DB\Cortex->mergeFilter()
[vendor/ikkez/f3-cortex/lib/db/cortex.php:622] DB\Cortex->filteredFind()
[app/Controllers/TeacherController.php:42] DB\Cortex->find()
[vendor/bcosca/fatfree-core/base.php:1873] Controllers\TeacherController->edit()
[vendor/bcosca/fatfree-core/base.php:1673] Base->call()
[index.php:10] Base->run()

Am I mistaking on the structure or on the functionality of the "has" filter ?
Thanks in advance for your help.

ikkez commented

Hi... no it's probably more the relation setup.. when you define a belongs-to-one field, you wrote:

'belongs-to-one' => ['Models\Activity']

I'm not sure if this is properly covered with fallbacks in the code yet. Either write:

'belongs-to-one' => 'Models\Activity' (without the array definition), or the array definition, but explicitly adding a foreign key here:

'belongs-to-one' => ['Models\Activity', 'id']

Perhaps that's the issue.

sejef commented

Thank you very much. I messed up a bit with the copy-paste. All my "belongs-to-one" relations are defined without braquets. I tried with the braquet&id version but same results.

ikkez commented

The latest commit should fix that problem. Could you test it please?! :)

sejef commented

Works fine ! Thank you ! 👍