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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:24:01+00:00 2026-06-09T09:24:01+00:00

Im having a weird issue. My employee table has id ->int(11) as primary key.

  • 0

Im having a weird issue. My employee table has id->int(11) as primary key. It also has code->varchar(36), password and some other fields. This code and password are the two fields employees use to login to the system. I used cakePHP form helper to build the login page with Code field and Password field; and things were going fine.

Today when i tried to update the employee table’s another field using $this->Model->save, it inserted a new row instead of updating. Thats when i learned that i need to set code as my primary key. So i put $primaryKey = 'code' in my Employee model.

The moment i put that, the Code field in my login page disappeared, leaving only Password field. When I removed the $primaryKey = 'code', the Code field came back.

So it has something to do with setting code as primary key in the Model. Is there any work around this?

I need to be able to update the Employee rows using code and retain my Code field in the login page.

  • 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-09T09:24:03+00:00Added an answer on June 9, 2026 at 9:24 am

    It sounds like code is essentially a username field. If code is not defined as the primary key in your database, it is best not to set the primaryKey Model property as code.

    I think most would recommend to use the primary key field id to tell Cake which Employee record to update. But if you must use code, try the following:

    Assuming your code field has a unique constraint in your database or is uniquely validated using Cake validation, you can use code to find the record you want to update. Once you have that record ($current in the below case), you can then get the primary key id, which is required so Model::save knows which record to update.

    public function edit($code = null) {
        $current = $this->Employee->find('first', array(
            'conditions' => array(
                'Employee.code' => $code
            )
        ));
        $this->Employee->id = $current['Employee']['id'];
    
        if (!$this->Employee->exists()) {
            throw new NotFoundException(__('Invalid employee'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->Employee->save($this->request->data)) {
                $this->Session->setFlash(__('The employee has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The employee could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $current;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having some weird issue here. I have a database table which has
Having a weird issue with jQuery/JavaScript. I'm using this code: var minimumPercent = $('.donationTextBox').val();
I am having a weird issue with CSS that will has to be simple
I seem to be having a weird issue here. An extended component has the
I'm having a weird issue - in my code I am fetching data from
I'm having a really weird issue with some content that is being inserted with
This is a weird issue I'm having, I have a table and try to
having this weird issue while inserting to MongoDB using PHP. My insertion code is
Im having a weird issue here. Its probably just something stupid but I dont
Im having a weird issue here; The following .htaccess works but now (after changing

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.