dongyong8491 2013-03-26 14:52
浏览 68
已采纳

CakePHP:shell脚本的密钥“PRIMARY”的重复条目

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.

  • 写回答

3条回答 默认 最新

  • drygauost253590142 2013-03-26 14:58
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)