Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3612750
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T22:02:23+00:00 2026-05-18T22:02:23+00:00

Source: http://asciicasts.com/episodes/16-virtual-attributes I’d like to achieve a similar setup as below, but in CakePHP

  • 0

Source: http://asciicasts.com/episodes/16-virtual-attributes

I’d like to achieve a similar setup as below, but in CakePHP and where the virtual attributes are created using code, not SQL (as documented at http://book.cakephp.org/view/1070/Additional-Methods-and-Properties#Using-virtualFields-1590).

class User < ActiveRecord::Base  
  # Getter  
  def full_name  
    [first_name, last_name].join(' ')  
  end  

  # Setter  
  def full_name=(name)  
    split = name.split(' ', 2)  
    self.first_name = split.first  
    self.last_name = split.last  
  end  
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T22:02:24+00:00Added an answer on May 18, 2026 at 10:02 pm

    As you’ve rightly mentioned, virtualFields are used to create extra attributes for models using SQL. For generating fields using code, you should try the afterFind method:

    function afterFind($results) {
        foreach ($results as $key => $val) {
            if (isset($val['User']['first_name']) && isset($val['User']['last_name'])) {
                $results[$key]['User']['full_name'] = $this->formatName($val['User']['first_name'], $val['User']['last_name']);
            }
        }
        return $results;
    }
    
    function formatName($firstName, $lastName) {
        $firstName = trim($firstName);
        $lastName = trim($lastName);
        return $lastName . ', ' . $firstName;
    }
    

    You can read more on this from the book here. I’d assume you’ll have to test this a little further, and probably include the afterFind method’s second parameter $primary.


    EDIT: I just realized that another way of doing this would be to include such functions directly in your model:

    function getFullName($user = null) {
        if ($user) {
            $this->set($user);
        }
        return $this->data['User']['first_name'] . ' ' . $this->data['User']['last_name'];
    }
    

    And there’s your getter!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm following this article ( http://asciicasts.com/episodes/160-authlogic ), I'm not using nifty generator tho. I've
Taking Ryan Bates' asciicast as an example: http://asciicasts.com/episodes/163-self-referential-association He ends with two associations of
source http://technet.microsoft.com/en-us/library/ms162234%28SQL.100%29.aspx code //Connect to the local, default instance of SQL Server. { Server
Original source: http://twitter.com/tobeytailor/status/8998006366 (x=[].reverse)() === window // true I've noticed that this behavior affects
I'm using a MediaElement with this source: http://origin-community.ministryofsound.com/asx/radio/mosRadio.asx When I open it with Windows
Given the code block below (source: http://www.asp.net/learn/mvc/tutorial-31-cs.aspx )... I receive this error: Using the
I am trying to turn the the flash page flip (source: http://76design.com/shiftcontrol/index.php/2005/07/03/dynamic-page-flip/ ) into
I saw page when I was Searching with the key word Open source http://www.opensource.apple.com/release/mac-os-x-1064/
I'm looking for a open source .Net HTTP proxy library. Basically I want to
Hey! I was looking at this code at http://www.gnu.org/software/m68hc11/examples/primes_8c-source.html I noticed that in some

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.