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.