Domanda

I am creating a real estate website that has Listing ID's attached to each listing. I am running a script via shell in CakePHP that is parsing a csv file and should be updating any listing that already exists or inserting a new one if it does not.

The problem is that I keep getting a Duplicate entry '###' for key "PRIMARY' where ### is the Listing ID that is being provided by the CSV. This script is being run from the command line.

Here's a smaller version of what my table includes:

CREATE TABLE `listings` (
  `ListingID` int(11) NOT NULL,
  `AccessibilityYN` varchar(50) DEFAULT NULL COMMENT 'AccessibilityYN',
  `BathsFull` int(6) DEFAULT NULL COMMENT 'BathsFull',
  `BathsPartial` int(6) DEFAULT NULL COMMENT 'BathsPartial',
  `BathsTotal` decimal(5,1) DEFAULT NULL COMMENT 'BathsTotal',
  `Beds` int(11) DEFAULT NULL COMMENT 'Beds',
  PRIMARY KEY (`ListingID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

Here's my Listing Model (notice that I have public $primaryKey = 'ListingID';)

class Listing extends AppModel {
    public $name = 'Listing';
    public $primaryKey = 'ListingID'; 
    public $belongsTo = array(
        'Agent' => array(
            'className'    => 'Agent',
            'foreignKey'   => 'AgentID'
        )
    );
}

Here's the shell I'm running via command line:

class MyShell extends AppShell {

    public $uses = array('Listing');

    public function update_imported_listings() {

       /***SOME CODE HERE TO GET THE $entry FROM THE CSV***/

        $this->Listing->ListingID = $entry['ListingID'];

        if(!$this->Listing->exists()){
            $this->Listing->create();
        }

        if($this->Listing->save($entry)){
          echo "Saved Listing";
        } else {
          echo "Listing Failed";
        }
    }
}

I understand that CakePHP usually likes id to be the field used in the database, however I have set $primaryKey = 'ListingID' in my model. I've tried setting ListingID to Auto Increment in the DB but that didn't work.

Anybody have any ideas? I'm fresh out.

È stato utile?

Soluzione

Setting ListingID doesnt' do anything

This line is your problem:

$this->Listing->ListingID = $entry['ListingID'];.

Irrespective of what your actual primary key field is in database the primary key value is always specified using Model->id property. As such, change it to:

$this->Listing->id = $entry['ListingID'];

You don't need to call exists

There is not need to explicitly check if record with particular primary key values exists. Cake will automatically update and record instead of creating a new one if the data array passed to save() contains a valid primary key value which exists in db. Just ensure you call create() before saving if you are using save() in a loop.

Altri suggerimenti

Don't break CakePHP conventions, use 'id' field as primary key.

http://book.cakephp.org/view/24/Model-and-Database-Conventions

I was getting the same error running a cakePHP shell script, turns out the latest record's ID was at the maximum value allowed by the size of the column (whoever made the table used a medium int for the id column), so auto_increment wasn't working. Changed the column to allow for larger numbers to fix. Probably something obvious, but might help someone out there.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top