Skip to content

Instantly share code, notes, and snippets.

@marcosx86
Last active October 21, 2016 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save marcosx86/cb60714c5d042d2083d8c32544b88535 to your computer and use it in GitHub Desktop.
Save marcosx86/cb60714c5d042d2083d8c32544b88535 to your computer and use it in GitHub Desktop.
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
class AlunosTable extends Table
{
public function initialize(array $config)
{
$this->table('public.alunos');
$this->primaryKey('cd_aluno');
$this->entityClass('App\Model\Entity\Aluno');
$this->belongsTo('Turma', [
'className' => 'Turmas',
'foreignKey' => 'cd_turma',
'bindingKey' => 'cd_turma'
]);
}
}
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
class EscolasTable extends Table
{
public function initialize(array $config)
{
$this->table('public.escolas');
$this->primaryKey('cd_escola');
$this->entityClass('App\Model\Entity\Escola');
$this->hasMany('Turmas', [
'className' => 'Turmas',
'foreignKey' => 'cd_turma',
'bindingKey' => 'cd_turma'
]);
}
}
$this->loadModel('Escolas');
$this->loadModel('Turmas');
$this->loadModel('Alunos');
$query = $this->Turmas->find()
->contain(['Alunos'])->where(['Alunos.st_aluno' => 0]);
print_r($query->sql());
//$query->all(); // FAILS
echo "\n\n";
$query = $this->Alunos->find()
->contain(['Turma'])->where(['Turma.st_turma' => 0]);
print_r($query->sql());
$query->all(); // OK
echo "\n\n";
$query = $this->Alunos->find()
->contain(['Turma' => function ($q) { return $q->where(['Turma.st_turma' => 0]); }]);
print_r($query->sql());
$query->all(); // OK, complex mode
<?php
namespace App\Shell;
use Cake\Console\Shell;
class TesteShell extends Shell
{
public function main()
{
$this->loadModel('Escolas');
$this->loadModel('Turmas');
$this->loadModel('Alunos');
echo "Limpando dados.\n";
$this->Alunos->query()->delete()->execute();
$this->Turmas->query()->delete()->execute();
$this->Escolas->query()->delete()->execute();
echo "Populando tabelas.\n";
foreach ([1, 0, 1] as $vEscola)
{
$entEscola = $this->Escolas->newEntity();
$entEscola->st_escola = $vEscola;
$this->Escolas->save($entEscola);
foreach ([0, 0, 1] as $vTurma)
{
$entTurma = $this->Turmas->newEntity();
$entTurma->st_turma = $vTurma;
$entTurma->cd_escola = $entEscola->cd_escola;
$this->Turmas->save($entTurma);
foreach ([2, 0] as $vAluno)
{
$entAluno = $this->Alunos->newEntity();
$entAluno->st_aluno = $vAluno;
$entAluno->cd_turma = $entTurma->cd_turma;
$this->Alunos->save($entAluno);
echo "Salvo Aluno[{$vEscola}, {$vTurma}, {$vAluno}]\n";
}
}
}
echo "Consultas.\n";
$query = $this->Alunos
->find()
->contain('Turma')
->where(['Turma.st_turma' => 0]);
echo $query->sql() . "\n";
var_dump($query->all());
$query = $this->Turmas
->find()
->contain('Escola')
->where(['Escola.st_escola' => 1]);
echo $query->sql() . "\n";
var_dump($query->all());
$query = $this->Escolas
->find()
->contain('Turmas')
->where(['Turmas.st_turma' => 1]);
echo $query->sql() . "\n";
var_dump($query->all());
echo "\n";
}
}
<?php
namespace App\Model\Table;
use Cake\ORM\Table;
class TurmasTable extends Table
{
public function initialize(array $config)
{
$this->table('public.turmas');
$this->primaryKey('cd_turma');
$this->entityClass('App\Model\Entity\Turma');
$this->hasMany('Alunos', [
'className' => 'Alunos',
'foreignKey' => 'cd_turma',
'bindingKey' => 'cd_turma'
]);
$this->belongsTo('Escola', [
'className' => 'Escolas',
'foreignKey' => 'cd_escola',
'bindingKey' => 'cd_escola'
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment