php-mvc-framework
php-mvc-framework copied to clipboard
Integrity constraint violation: 1052 Column 'id'
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 ?
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.
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?
$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();