Skip to content

Integrity constraint violation: 1052 Column 'id' #26

Open
@mmguide2003

Description

@mmguide2003

I tried to adjust your code to allow for a bespoke sql with INNER JOIN.
However there is an issue with integrity constraint on where.

How would we get over integrity constraints using your bind method i.e :

Employee (e)
id   name   started 
1    John    2010-01-02

Sales (s)
id     month   employee_id   income   volume
34       1        1           50000     1500

MODEL

public static function findSales($where){

      $sql="SELECT e.id, e.name,s.volume,s.income  FROM employee as e INNER JOIN sales as s ON e.id=s.employee_id";
      
      $attributes=array_keys($where);
      
      $add_sql=implode(" AND ", array_map(fn ($attr) => "$attr=:$attr",$attributes));
      $statement=self::prepare("$sql WHERE $add_sql");
      foreach ($where as $key => $value) {
	   $statement->bindValue(":$key",$value);
      }
      $statement->execute();
      return $statement->fetchObject(static::class);
}

CONTROLLER**

$sale=Employee::findSales(['month'=>1,'id'=>1]);

As we cannot use findSales(['month'=>1,'e.id'=>1])
Is there a way to adjust your attribute function to specify the id for the where clause to avoid ambiguity on the id ?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions