Bake and join tables giving trouble

After a bake, I get a ‘# htmlspecialchars() expects parameter 1 to be string, resource given’ in the case of using a join table.

I created tables ‘blogs’, ‘images’ (with a blob string an image) ‘tags’, ‘blogs_images’ (with ‘blog_id’ and ‘image_id’) and ‘blogs_tags’ (with ‘blog_id’ and ‘tag_id’) and only apply ‘bake all --everything --prefix baked’.

Not getting the expected results (don’t remember having any trouble with this the last time I used CakePHP 4), started to rebake with and without the join tables, here are some observations:

  • when using only the ‘blogs_tags’, the view of the blog-record looks good, that is, the related tags are visible. When only using the ‘blogs_images’, the view of the blog-record gives the mentioned error.

  • when using the view of the tag, the mentioned error appears, with the join table in place. Removing the join table and rebaking, the view of the tag works just fine.

  • using the index view of the join table shows records, but also there is a notice ‘Undefined property: BlogsTagsController::$BlogsTags’ and ‘Unable to emit headers’.

  • After removing a join table, I have to bake twice. After the first bake, there is a problem with the blog controller, still referring to the removed join table. After a second bake, the error is gone. So, in all cases I bake twice, but I can’t be sure if generated code is ok.

  • Bake doesn’t generate code related to blobs (I manually add code to view the images)?

So, what do I have to check to get things working?

Why do you use blob as a column type for your image field?

A blob column type is mapped as a ressource in CakePHP

Use the blob-type to store image, don’t know any other type to store image.

Don’t know what the consequence is of that the blob is mapped as a resource. I experience no difficulties with showing the stored images, when not using the join table.

You shouldn’t store images inside the db. You should rather just save the path to where the image is being saved inside the DB and reference it like that.

You can also take a look at GitHub - brandcom/cakephp-assets: Backend Asset Management for CakePHP

Why shouldn’t I? Can’t CakePHP handle join tables in relation with stored images, or blobs in general?

Except from that, join table with blog and tags also gives the htmlspecialchars error, there are no images involved. And the undefined property when viewing the join table, they puzzle me because I only use baked code.

I am not going the explain all the different reasons why storing “raw files” in a database is a bad idea - google is your friend.

In the end CakePHP doesn’t do what you expect it to and I would rather recommend you either use the plugin I mentioned above or build your own file management system.

All I can say is that CakePHP indeed can work with blob data in a database but usually you have to tell CakePHP how to store the blob data and how to retrieve it again (e.g. in beforeSave() and beforeFind() or via a custom DatabaseType).

Was sloppy with environments, there was still a blob-field in the blog-record, didn’t mention that, a left over of a database-restructure. This caused the problem.

The error tells the input of htmlspecialchars is a resource, which says nothing to me. You tell me a blob is mapped as a resource, which says nothing to me either. But the two together say that the input of htmlspecialchars is a blob instead of a string. That did take me some time, especially when being sloppy and not realizing there was still a blob in the record.

Now I can reproduce the error in a new fresh project. To me it seems that when baking, the process takes into account that there is a blob in the blog-record. The view of the blog-record doesn’t mention the blob-field. But, when using a join table, the baking doesn’t seem to take into account the blob in the blog record? The tags-view shows/tries to show the blog-field in the related blog-record. Removing the blog-field in the tag view-file and the error is gone.

As to using blobs, read info about that years ago, after having a not that good experience with images stored in a filesystem, systemmanagement wise. To me it’s to blunt to state to never use blobs, it will depend on size/use/etc. of a system? Personally, I have never experienced a problem whatsoever using blobs, but all small apps, and using phpmyadmin to insert blobs.

You shouldn’t be using htmlspecialchars (or Cake’s h shortcut for that) with blob fields. It makes no sense. May be something to raise as an improvement to bake, but pretty trivial for you to edit your view to remove that call. Heck, there’s probably no point at all in trying to output the content of an image field in a view, but that’s something bake can’t possibly know; there’s more than one reason to use a blob, and some of them might well make sense to output.

If ‘you’ is ‘me’, I’m not using the ‘htmlspecialchars’, ‘bake’ is :grinning:.

‘Bake’ is doing right in the sense it ‘skips’ the blob-field, when building the ‘view.php’ of a table (although would be nice to have a comment in view.php that blob-field is skipped with short why) Don’t think it’s doing right when using a join table, the blob-field doesn’t seem to be ‘skipped’, hence the errors.

That’s no problem, if you get to the point to understand what’s happening.