cakephp - Retrive only records without record associated in another table -


i have 2 tables clazzes , teachers, both joined third table clazzes_teachears. how can retrive clazzes has no teachers associated ? can return clazzes elements or without teacher associated.

clazzestable.php initialize

public function initialize(array $config) {     parent::initialize($config);      $this->table('clazzes');     $this->displayfield('name');     $this->primarykey('id');      $this->belongstomany('teachers', [         'foreignkey' => 'clazz_id',         'targetforeignkey' => 'teacher_id',         'jointable' => 'clazzes_teachers'     ]); } 

teacherstable.php initialize

public function initialize(array $config) {     parent::initialize($config);      $this->table('teachers');     $this->displayfield('id');     $this->primarykey('id');      $this->belongstomany('clazzes', [         'foreignkey' => 'teacher_id',         'targetforeignkey' => 'clazze_id',         'jointable' => 'clazzes_teachers'     ]); } 

note: doesn't exist clazzesteacherstable.php

method return clazzes with/without teachers associated (i need retrive clazzes without teachers)

public function getallclazzesrecursive(){     return $this         ->find('all')         ->contain([             'teachers' => function($q) {                 return $q->select(['id', 'registry', 'url_lattes', 'entry_date', 'formation', 'workload', 'about', 'rg', 'cpf', 'birth_date', 'situation']);             }         ])->hydrate(false)->toarray(); } 

teacher sql:

 create table if not exists `teachers` (   `id` int not null auto_increment,   `registry` varchar(45) not null,   primary key (`id`)); 

clazzes sql:

 create table if not exists `clazzes` (   `id` int not null auto_increment,   `name` varchar(45) not null,   primary key (`id`)); 

clazzes_teachers sql:

create table if not exists `clazzes_teachers` (   `clazz_id` int not null,   `teacher_id` int not null,   primary key (`clazz_id`, `teacher_id`)); 

use join query builder , see more http://book.cakephp.org/3.0/en/orm/query-builder.html#adding-joins

 public function getallclazzesrecursive(){     return $this         ->find('all')         ->join([           'table' => 'clazzes_teachers',           'alias' => 'ct',           'type' => 'left',           'conditions' => 'ct.clazz_id= clazzes.id',          ])        ->where('ct.id null')        ->hydrate(false)->toarray(); } 

you can use leftjoin() directly.


Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

How to get the ip address of VM and use it to configure SSH connection dynamically in Ansible -

javascript - Get parameter of GET request -