Any reason why selecting specific fields from associated model would fail in 4.x?

In my Menus controller, I am using:

$recipes = $this->Menus->MenuSections->MenuEntries->Recipes->find('all')
->select(['name','id', 'Ingredients.name', 'Ingredients.id', 'IngredientTypes.name'])
->contain(['Ingredients', 'Ingredients.IngredientTypes']);

The aim being to print a table of recipes, each with its primary ingredient and associated ingredient type. Then, I can design a new menu by choosing a recipe according to the specific ingredient (or ingredient type) I want to use at the time. (In the future, this will be a jQuery lookup routine, but for now Iā€™m just trying to get my app working from start to finish).

Cake outputs an SQL error:

Column not found: 1054 Unknown column ā€˜Ingredients.nameā€™ in ā€˜field listā€™

If I remove the select statement, the query runs fine, but it gives me all the columns from all three tables.

The docs led me to believe the query Iā€™m trying to assemble should be easily supported.

Well. It seems inserting a new entity with new associated data is also not supported ā€” also despite being prominently stated in the docs.

And now for a rant:

ā€œWith a refreshed application skeleton design, CakePHP 4.0.0 comes with a streamlined API making your development and application faster.ā€ ā€” CakePHP.org

Categorically, sadly: ā€œnoā€.

This platform has, above all else, maintained a talent for being as frustrating as it is helpful. I had hoped that with version 4 this kind of insanity would be ended. Is it the documentationā€¦or the platform? I believe itā€™s the former and always has been.

'It just works" has rarely been an applicable phrase for any new release of CakePHP. Sure, for a simple blog, as has been the customary tutorial in every single version of the docs, it does an ok job. Even though I canā€™t count how many inconsistencies between the docs and the tutorials Iā€™ve encountered over the years. Even as the blog tutorial sidesteps and glosses over any remotely robust UX/UI experience or user/access control paradigm or slightly complex data schema.

I may have to fall back to 3.9. Presumably that version should be working correctly. The last version I used with any degree of success was 3.2 a few years back.

Maybe I expect too much of it. It is free after all. I guess the company backing this project has a vested interest in the platform being just hampered enough to attract consulting/support clients.

Has anyone out there, not involved with the project, actually built a complex, data-driven application using 4.x?

/rant

Ingredients is presumably a belongsToMany relationship, so it doesnā€™t do a join to that table, but rather a second query. Hence why the field youā€™re trying to select isnā€™t available. Does putting it on the contain level work?

$recipes = $this->Menus->MenuSections->MenuEntries->Recipes->find('all')
    ->select(['name','id'])
    ->contain(['Ingredients' => [
        'queryBuilder' => function (Query $q) {
            return $q->select(['Ingredients.name', 'Ingredients.id', 'IngredientTypes.name']);
        },
        'IngredientTypes'
    ]]);

Quite sure this question has been raised before, here and/or on StackOverflow.

Ingredients is actually a belongsTo relationship because I am categorizing each recipe as being based on a primary ingredient.

You donā€™t have multiple recipes that use the same ingredient? Very much seems like it should be a many-to-many relation.

I do. But thatā€™s handled by a different relationship using a bridge table.

Iā€™ve had my share of rants. Iā€™ve built a fairly large site on CakePHP 2 and to be honest, I was very happy with it. Iā€™ve been able to achieve everything Iā€™ve needed to achieve. Iā€™ve become so comfortable with it, I donā€™t feel like thereā€™s anything I canā€™t accomplish. APIā€™s, complex data structures, etc. With version 2 loosing support though, Iā€™m trying to upgrade to version 4. Itā€™s been painful to say the least. Iā€™ve come into it not really knowing anything about version 3 or 4. Itā€™s coming along, but not without some serious headaches. In some cases it seems like things have been made more complicated rather than simple. Itā€™s turned into a complete rewrite of the whole application without any method going untouched.

1 Like

I have given up on trying to get the data to save elegantly in a ā€œCakeā€ way.

Instead, Iā€™m simply assembling each nested entity by hand in the controllerā€¦ and saving each, one by oneā€¦ after obtaining the id from the one saved before.

In 20 minutes I got this working ā€” after spending 2 whole days trying to make it work the Cake way.

And the fact is, I realize now that I never did manage to get nested inserts to work ā€” not even back on Cake 2.0. I thought I had done it in 3.2, but it turns out that project didnā€™t have nested inserts. The last time I had to make a series of nested inserts, I had to break it up and do one by one back then too.

So much for the example in the docs. I have NEVER seen it working.

FWIW, Iā€™ve tried debugging my top entity after patchEntity and before calling save andā€¦ plain as day, itā€™s totally, utterly, pretending none of my other entities even exist. Just dropping them. Not even a note to tell me. It just ignores their existence. Even when I try to build the entire tree manually beforehand. Even if I turn off validation in patchEntity.

I give up.

I do it all the time. I donā€™t know why itā€™s not working for you. If you can read the entity structure that you want from pre-existing records, then just replicate that in the way you name your fields.