Skip to content
CODE THE PIXEL
CakePHP

CakePHP 4 Concatenation

Asyraf Wahi Anuar Updated 1 min read

The most popular concatenation in programming is combining the first and last name field to form a full name. This tutorial shows the code of concatenation in CakePHP 4.

This tutorial may be outdated

It was last reviewed 4 years ago. Some steps may differ in newer versions.

Tech stack

  • CakePHP 4.x

In formal language theory and computer programming, concatenation is the operation of joining character strings end-to-end. For example, the concatenation of "snow" and "ball" is "snowball". The most popular concatenation in programming is combining the first and last name field to form a full name. This tutorial shows the code of concatenation in CakePHP 4.

Let assume that you have a users and documents table. The user's table contains (id, name, email, role, status) and the document's table contains (id, user_id, title, attachment). In the user table, you can select the name of the users as input. However, it will only show either id or name(depending on your DisplayField() value). Use the following code in your controller to stitch the name and role value (eg: John - Administrator) (eg: public function add).

PHP
$users = $this->Documents->Users->find('list', [
	'valueField' => function ($row) {
		return $row['fullname'] . ' - ' . $row['role'];
	}
]);
$this->set(compact('documents', 'users'));

That all. Happy coding :)

Did this tutorial help?

273 readers found this helpful

Comments

No comments yet. Questions about this tutorial are welcome here: the answer helps the next reader too.

Leave a comment

Markdown works: `code`, ``` blocks, **bold**, links.

First comments are reviewed before they appear.

CakePHP

Using Bahasa Melayu Table Names in CakePHP

Learn how to use Bahasa Melayu table names like "pengguna" in CakePHP while following the framework’s naming conventions. Includes tips for using the Bake tool and avoiding pluralization issues.

5 min read

Tools & Software

Formatting PHP in VSCode Using Prettier and PHP Intelephense

Learn how to set up Visual Studio Code (VSCode) for automatic PHP file formatting using the Prettier and PHP Intelephense extensions. This step-by-step tutorial will help you maintain a clean and organised codebase, enhancing your PHP development experience.

2 min read