I would like to put together a complete crud with relationships: Groups Users and Proposals
This does not save on GroupUsers. [UsuariosGrupos]
I am sorry for my english
My database relationship
link
https://ibb.co/jMZavw
<?php
namespace Some;
use Phalcon\DI,
Phalcon\Db\Column,
Phalcon\Db\RawValue,
Phalcon\Mvc\Model,
Phalcon\Events\Manager as EventsManager,
Phalcon\Db\Adapter\Pdo\MySQL as Connection,
Phalcon\Mvc\Model\Manager as ModelsManager,
Phalcon\Mvc\Model\Metadata\Memory as ModelsMetaData;
$eventsManager = new EventsManager();
$di = new DI();
$connection = new Connection(array(
"host" => "localhost",
"username" => "jan",
"password" => "1817698",
"dbname" => "socius",
));
$connection->setEventsManager($eventsManager);
$eventsManager->attach('db1',
function ($event, $connection) {
switch ($event->getType()) {
case 'beforeQuery':
echo $connection->getSqlStatement(), "\n";
print_r($connection->getSqlVariables());
break;
}
}
);
$modelsManager = new ModelsManager();
$modelsManager->setDi($di);
$di['db1'] = $connection;
$di['modelsManager'] = $modelsManager;
$di['modelsMetadata'] = new ModelsMetadata();
//Model Proposals
class Propostas extends Model {
public $id;
public $proposta;
public $vinculada;
public $aceitada;
public $rejeitada;
public function initialize() {
$this->setConnectionService('db1');
//auto-relacionamento
$this->belongsTo('propostas_id', 'Propostas', 'id', ['alias' => 'proposta']);
$this->belongsTo('usuarios_id', 'Usuarios', 'id');
$this->belongsTo('grupos_id', 'Grupos', 'id');
}
}
//Model UsersGroups
class UsuariosGrupos extends Model {
public $id;
public $usuarios_id;
public $grupos_id;
public $status_admin;
public function beforeCreate() {
$this->status_admin = true;
}
public function initialize() {
$this->setConnectionService('db1');
$this->belongsTo('usuarios_id', 'Usuarios', 'id', ['alias' => 'Usuario']);
$this->belongsTo('grupos_id', 'Grupos', 'id', ['alias' => 'Grupo']);
}
}
//Model Groups
class Grupos extends Model {
public $id;
public $nome;
public $tema;
public function initialize() {
$this->setConnectionService('db1');
$this->useDynamicUpdate(true);
$this->hasMany('id', 'Propostas', 'grupos_id');
$this->hasMany('id', 'UsuariosGrupos', 'grupos_id', ['alias' => 'UsuariosGrupos']);
$this->hasManyToMany('id','UsuariosGrupos', 'grupos_id', 'usuarios_id', 'Usuarios','id', ['alias' => 'Usuarios']);
}
}
//Model Users
class Usuarios extends Model {
public $id;
public $email;
public $nome;
public $senha;
public function initialize() {
$this->setConnectionService('db1');
$this->useDynamicUpdate(true);
$this->hasMany('id', 'Propostas', 'usuarios_id');
$this->hasMany('id', 'UsuariosGrupos', 'usuarios_id', ['alias' => 'UsuariosGrupos']);
$this->hasManyToMany('id', 'UsuariosGrupos','usuarios_id', 'grupos_id', 'Grupos', 'id',['alias' => 'Grupos']);
}
}
$usuarios = Usuarios::findById(1);
$grupos= new Grupos();
$grupos->nome = "grupo do x";
$grupos->tema = "tema";
$grupos->Usuarios = array($usuarios);
$grupos->save();
//example update, delete, find with Group [Grupos] , Users [Usuarios] and Proposals [Propostas]
// in which it saves in UsersGroups "UsuariosGrupos"
Thanks in advance