Hello, I wrote a small function to request City names but cakephp does not do the leftjoin with the correct table…
Function in CountriesController:
public function getAjaxcityList()
{
$this->autoRender = false;
if ($this->request->is('ajax')) {
$this->Authorization->skipAuthorization();
$name2 = $this->request->getQuery('term2');
$results = $this->Countries->find()
->contain(['Cities'])
->where(['id' => 19])
->where(['Cities.name LIKE' => $name2 . '%'])
->limit(10);
debug($results);
debug($results->toArray());
debug($results->sql());
$resultsArr = [];
foreach ($results as $result) {
$resultsArr[] =['label' => $result->cities.name, 'value' => $result->cities.id];
}
return $this->response->withType('application/json')->withStringBody(json_encode($resultsArr));
}
}
CountriesTable:
$this->hasMany(‘Cities’, [
‘foreignKey’ => ‘country_id’,
]);
Error Message:
object(Cake\ORM\Query) id:0 {
‘(help)’ => ‘This is a Query object, to get the results execute or iterate it.’
‘sql’ => ‘SELECT Countries.id AS Countries__id, Countries.name AS Countries__name FROM countries Countries WHERE (id = :c0 AND Cities.name like :c1) LIMIT 10’
‘defaultTypes’ => [
‘Countries__id’ => ‘integer’,
‘Countries.id’ => ‘integer’,
‘id’ => ‘integer’,
‘Countries__name’ => ‘char’,
‘Countries.name’ => ‘char’,
‘name’ => ‘char’,
‘Cities__id’ => ‘integer’,
‘Cities.id’ => ‘integer’,
‘Cities__country_id’ => ‘integer’,
‘Cities.country_id’ => ‘integer’,
‘country_id’ => ‘integer’,
‘Cities__name’ => ‘string’,
‘Cities.name’ => ‘string’,
]
{ “message”: “SQLSTATE[42S22]: Column not found: 1054 Unknown column \u0027Cities.name\u0027 in \u0027where clause\u0027”, “url”: “/countries/get-ajaxcity-list?term=hamburg”, “code”: 500, “file”: “/var/www/html//vendor/cakephp/cakephp/src/Database/Statement/MysqlStatement.php”, “line”: 39 }
What am i doing wrong here?