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 8706521
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:38:04+00:00 2026-06-13T03:38:04+00:00

As we all know that cakephp has default save command for inserting record into

  • 0

As we all know that cakephp has default save command for inserting record into the database.
But i want to know can i create my own save command in cakephp or modify the existing one.
Can I do this?

You must be asking why i’m asking why i’m saying that? let me give you an live example for this—-
Suppose i have an textbox which contains the username entered by the user.I’m not taking this as unique, hence more than one user will insert same username(possible..).

Ex-My name is prakash Gupta and i’m taking the username ‘prakash’. There will be other users also whose name can be prakash gupta and they will provide the same username.Now inorder to solve this i’m using random function and attaching some digits behind the username, so that
it will be different for every one.
Now ‘save’ command will take the username which i entered in the textbox and insert into the database but i want to insert the modified username into the database which i generated
by random function. can this be possible???
if yes let me know….

  • 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-13T03:38:05+00:00Added an answer on June 13, 2026 at 3:38 am

    Normally you don’t change the standard one but either add another one or use the beforeSave etc. hooks.

    The save functions are part of the Models. Normally you modify them with: Behaviors. See: http://book.cakephp.org/2.0/en/models/behaviors.html They let you modify the models, saving etc. without having to code all directly into your models.

    Based on your edit:

    Unique constraint

    As I see you want a unique username. There are multiple solutions for that. First make sure to set a constraint. Even your random() trick will possibly generate a duplicate since random can create also the same. So username.random(4) could generate multiple times: username1234. You cannot be sure.

    Constraints should be set at for example your model level. Start this unique check with validations of CakePHP.

    When do you know the username is already used

    You will know when the validation failed. So first just validate the record. So before save call model::validate() to check whether your unique constraint is ok. If yes just save.
    http://book.cakephp.org/2.0/en/models/data-validation.html

    If not you can add your random string and check again. Call model::validate to see if it is now ok. This part could be implemented for example in a behaviour in the beforeSave() method.

    Now you have a unique one save your record. Make sure to check whether it succeeded because it is possible that in the meantime another record with that username was added. In that case re-run the process.

    Lots of work

    It is hard because multiple clients can add data same time in a database before you even know. Most simple is to catch the error, so just call save with your username. If it fails because of your unique constraint add the random() string and try again and again. It should be possible to save. Trick here with your random string because if it is short and a username is used a lot you can run into issues. If your random string is 3 numbers you can have the same username 1000 times, from 000 to 999.

    Other option

    What you sometimes see is suggestions, the interface suggests some free names based on your username input. You still need the check though.

    Another option
    Just tell the user it is in use. Maybe add an ajax check so you can instantly show the result by validating the field.

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

Sidebar

Related Questions

I want to save all but some excluded fields. I know that I can
We all know that openId has become such a popular method for user authentication,
We all know that a hash table has O(1) time for both inserts and
We all know the effects that lots of thrown exceptions can have over the
I want to reduce the number of fields returned by cakephp's find('all') but don't
We all know that $content_for_layout is now deprecated in cakephp 2.1 and changed to
We all know that CSS sprite images are great to reduce the amount of
We all know that most applications out there assume class names to follow the
as you all know that if we use flash object in web page in
as we all know that html_safe work in controller and view i am sending

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.