Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Integrity constraint violation: 1052 Column 'id' #26

Open
mmguide2003 opened this issue Aug 2, 2022 · 3 comments
Open

Integrity constraint violation: 1052 Column 'id' #26

mmguide2003 opened this issue Aug 2, 2022 · 3 comments

Comments

@mmguide2003
Copy link

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 ?

@mmguide2003
Copy link
Author

I'd also like to add that using employee_id would not be the answer I'm looking for as in actual fact I'm joining three tables in the real code with employee_id in the other table.

@thecodeholic
Copy link
Owner

I think this is more a question how to solve this problem using PHP PDF rather than an issue of this PHP MVC framework.
Do you still have a problem or found the solution?

@RobertoVasquez01
Copy link

$users = DB::table('users')
->join('contacts', 'users.id', '=', 'contacts.user_id')
->join('orders', 'users.id', '=', 'orders.user_id')
->select('users.*', 'contacts.phone', 'orders.price')
->get();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants