</> Code The Pixel

CakePHP 4 Concatenation

Asyraf Wahi Anuar - May 18, 2020

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)

Popular
CakePHP 4 Print PDF Using CakePDF
May, 17 2020
CakePHP 4 Authentication Using Auth...
May, 14 2020
CakePHP 4 Sending Email
February, 01 2022
CakePHP 4 jQuery Date Time Picker
October, 01 2018
CakePHP 4 Export To CSV
May, 29 2020
CakePHP 4 Authentication Using...
May, 11 2020
CakePHP 4 Find, Sort & Count
June, 02 2020
Share
Sharing Link
Click the icon to share