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

  • Home
  • SEARCH
  • 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 8657937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:38:41+00:00 2026-06-12T15:38:41+00:00

I try to use existing model data inside a models callback function in CakePHP

  • 0

I try to use existing model data inside a models callback function in CakePHP 2.1 but can’t get it working.
What I do is I try to get a users role in the beforeValidate() callback and check if it’s empty. If yes, I’ll set it. Normally I do it like this, and for the first creation of the record it works pretty well.

if (empty($this->data[$this->alias]['role']))
    $this->data[$this->alias]['role'] = 'user';

The problem is, every time an existing record (user) gets updated, the role will be set again.

Question: So, how do I check if the field role is already set in the record data, not the post data (seems like $this->data[$this->alias] only contains POST data)?

  • 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-06-12T15:38:42+00:00Added an answer on June 12, 2026 at 3:38 pm

    There are three solutions to this problem as I can see it.

    1. Set a default value in the database column (easiest, best?)
    2. Add role to your inputs everytime.
    3. Lookup the user and add a role if it’s missing

    The first two options seem obvious, so I’ll just illustrate the last.

    public function beforeValidate() {
      if (!empty($this->id) {
        $this->data[$this->alias][$this->primaryKey] = $this->id;
      }
      if (!empty($this->data[$this->alias][$this->primaryKey])) {
        // user exists, check their data
        $id = $this->data[$this->alias][$this->primaryKey];
        $user = $this->find('first', array(
          'conditions' => array($this->primaryKey => $id)
        ));
        $this->data[$this->alias]['role'] = $user[$this->alias]['role'];
      }
      if (empty($this->data[$this->alias]['role'])) {
        // new user but missing a role
        $this->data[$this->alias]['role'] = 'user';
      }
      return true;
    }
    

    This callback will check if an ID was passed, and if so it will look up the user and populate the role field. Then, it checks for an empty role and fills it with the default if necessary. Obviously he more code you have, the more possibilities for bugs, so I suggest the first option for default column values.

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

Sidebar

Related Questions

I try to use a variable in my kshell script but can't get a
I try use string as a regular expression pattern but I've following errors PHP
i try to use jquery .text() but not really work : Before <a id=group_name
I try to use the columnFilter add-on of jquery datatable but I couldn't make
I have an existing web application that I am converting to use CakePHP. The
I'm trying to use django-evolution to modify some models from an existing project. Now
i try to get the name attribute from an element and use that string
I try use a integer array in java with the code below: public static
I try to use the great particle emitter which Michael Daley build for his
I try to use SQL SERVER VIEW within RIA Services. So I created 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.