简体   繁体   中英

Cakephp SQL Error 1054 Unknown Column In The Field List

I am trying to update multiple records in one field in my database. For some reason I keep getting SQL Error: 1054: Unknown column '520947b9' in 'field list'. 502947B9 is apart of my ID. Im not understanding why that value is being seen as a field list. Here is my code. That said, Im not sure Im updating these records correctly. If Im not please point it out to me. Thanks!!

public function findPolicyIds($coverageId = null) {
    $policyid = $this->Policy->find('all', array(
        'recursive' => -1,
        'conditions' => array('Policy.coverage_id' => $coverageId),
        'fields' => array('Policy.id')));

        foreach($policyid as $id) {
        $all[] = $id['Policy']['id'];

        foreach ($all as $key) {
        $this->Policy->Declination->updateAll(
              array('Declination.policy_id' => $key),
              array('Declination.coverage_id <=' => $coverageId)
            );
        }



    }

}

Here are my errors

Query: UPDATE declinations AS Declination LEFT JOIN policies AS Policy ON ( Declination . policy_id = Policy . id ) SET Declination . policy_id = 520947b9-0210-4067-94ea-70f8ae78509d WHERE Declination . coverage_id <= '520947b9-1fa0-45db-992e-70f8ae78509d'

Query: UPDATE declinations AS Declination LEFT JOIN policies AS Policy ON ( Declination . policy_id = Policy . id ) SET Declination . policy_id = 520947b9-0694-4724-b353-70f8ae78509d WHERE Declination . coverage_id <= '520947b9-1fa0-45db-992e-70f8ae78509d'

By the looks of your query, updateAll is not recognizing $key as a string. Either cast it as such, or add the ' characters yourself. Example:

$this->Policy->Declination->updateAll(
          array('Declination.policy_id' => "'".$key."'"),
          array('Declination.coverage_id <=' => $coverageId)
        );

That's the SQL error.

Now

"That said, Im not sure Im updating these records correctly."

... Well, what do you want to do? Reading your code, You are getting an array of Policy's ids and updating all Declinations with a coverage_id <= $coverageId , which doesn't make much sense, since that foreach is updating the policy_id for that same condition, so in the end you will perceive the last change: last policy_id of the foreach on every Declination with coverage_id equal or less than $coverage_id .... Doesn't make much sense to me, even not knowing what you need to do.

Based on the SQL and assuming you are using an ORM, it appears to me that policy_id is defined as an numeric field in your Declination model when it really needs to be a string. Coverage_id field is working correctly, so compare the two definitions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM