nach einigen Recherchen finde ich diese Aussage erstellt, aber es funktioniert nicht wie erwartet:cakephp 3.x Sub wählen Sie einen Zählwert erhalten
public $virtualFields = array(
'count' => "SELECT COUNT(plans_trainings.id) FROM plans_trainings, trainings, projects WHERE plans_trainings.training_id = trainings.id AND projects.location_id = locations.id AND plans_trainings.project_id = projects.id"
);
public function trainingsPerLocationChartData(){
$this->loadModel('Locations');
$locationsDiagramData = $this->Locations->find('all', array(
'fields' => array(
'locations.id',
'locations.description',
'locations.count' /* virtuel field */
)
));
Die Erklärung wird wie folgt aussehen:
'sql' => 'SELECT locations.id AS `locations__id`, locations.description AS `locations__description`, locations.count AS `locations__count` FROM locations Locations'
Das virtuelle Feld wird überhaupt nicht erkannt. Was mache ich falsch?
UPDATE: Ich fand heraus, dass ich es so nennen haben:
'count' => "(SELECT COUNT(plans_trainings.id) FROM plans_trainings, trainings, projects WHERE plans_trainings.training_id = trainings.id AND projects.location_id = locations.id AND plans_trainings.project_id = projects.id) AS `counter`"
Ich habe() um die Auswahl.
$locationsDiagramData = $this->Locations->find('all')
->select(['locations.id', 'locations.description', $this->virtualFields['count']]);
Aber ich immer noch eine falsche SQL-Syntax erhalten:
SELECT locations.id AS `locations__id`, locations.description AS `locations__description`, (SELECT COUNT(plans_trainings.id) FROM plans_trainings, trainings, projects WHERE plans_trainings.training_id = trainings.id AND projects.location_id = locations.id AND plans_trainings.project_id = projects.id) AS `counter` AS (SELECT COUNT(plans_trainings__id) FROM plans_trainings, trainings, projects WHERE plans_trainings FROM locations Locations
das "letzte" Problem scheint nur diesen Teil nach AS counter
:
AS (SELECT COUNT (plans_trainings__id) FROM pläne_trainings, trainings, projekte WO plant_trainings
Das ist was die Aussage bricht! Irgendwelche Ideen, um das zu lösen? Wenn ich die Anweisung in phpmyadmin ausführen und diesen erwähnten Teil entfernen, funktioniert die Anweisung gut und bringt die richtigen Ergebnisse!
UPDATE 2: das ist die einfache Erklärung:
select
locations.id,
locations.description,
(
select
count(plans_trainings.id)
from
plans_trainings,
trainings,
projects
where
plans_trainings.training_id = trainings.id
and projects.location_id = locations.id
and plans_trainings.project_id = projects.id
) as 'Anzahl'
from
locations;
Es gibt keine virtuellen Felder in CakePHP 3 mehr ... eine ordnungsgemäße Verwendung [** Unterabfrage **] (http://book.cakephp.org/3.0/en/orm/query-builder.html#subqueries) und übergebe es an [** 'select()' **] (http: // book. cakephp.org/3.0/en/orm/query-builder.html#selecting-data). – ndm