Combine table fields in templates

Hey everyone,

i have a table where a Adress is splittet in different fields.
street, house_no, zip, city

If i now want to show them together on a page do i always do that?

$user->house->street.' '.$user->house->house_no.' '.$user->house->postcode.' '.$user->house->city

and if yes how do i produce a line break inside there.

."\n".
or
.'\n'.
didn’t work for me.

Thank you !!

See Virtual Fields

1 Like

"\n" will produce a line break in the generated HTML, but that doesn’t translate into a line break in what gets rendered. For that, you’ll need <br/>.

Hey Kevin, thank you so much.
That worked so far and i understood the Virtual Fields. :star_struck:

Im still not sure how to proceed.

protected function _getFullAdress()
    {
        return $this->street . '  ' . $this->house_no . ' ' . $this->postcode . ' ' . $this->city;
    }

When i want to just show the Virtual Field i can simply

$user->house->full_adress

But when i want to show it in the EDIT or NEW i was struggeling what to do.

  echo $this->Form->control('house_id', [
                        'options' => $houses,

The only way i found was to set the DisplayField to my created Virtual Field

$this->setDisplayField('full_adress');

Is that the way you would do that?

Hi Zuluru,

protected function _getFullAdress()  {
   return $this->street . '  ' . $this->house_no . "<br/>" . $this->postcode . ' ' . $this->city; 
}

doesn’t work here


  1. HTML select elements (or more directly option element) can’t contain further HTML. Its a browser standard as e.g. mobile devices show a completely different view for selecting an option as your desktop browser does. If you want to style a select, you need to use a JS library like select2 or slim-select to transform the HTML native select into “other” HTML which represents a select and can be styled.

  2. Those links don’t “properly” use the <br/> because CakePHP automatically escapes Title’s for generated links to prevent XSS. If you don’t know what that is you have much to learn :wink:
    If you don’t care about XSS or truley trust your users (which you shouldn’t) you can prevent that logic via e.g.

echo $this->Html->link(
    "Some Text with a <br/> break", 
    ['controller' => 'Pages', 'action' => 'display', 'home'], 
    ['escapeTitle' => false]
);
1 Like