CakePHP 4 Concatenation
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).
$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.