Code The Pixel

CakePHP 4 Concatenation

Asyraf Wahi Anuar - May 18, 2020
Published in CakePHP 1681 Views Email This Article
Estimated reading time: 49 seconds

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).

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


That all. Happy coding :)


Cite this article (APA 6th Edition)