ORM: Gestion du select (peut être vide)

This commit is contained in:
xdrm-brackets 2016-07-23 12:33:05 +02:00
parent e026bc0cc1
commit 7e19392bb0
3 changed files with 48 additions and 34 deletions

View File

@ -253,7 +253,7 @@
/* (2) On vérifie que chaque champ existe, sinon on le retire */
foreach($fields as $f=>$field)
if( !isset($this->schema['columns'][$field]) )
if( !isset($this->schema['columns'][$field]) && $field != '*' )
unset($fields[$f]);
/* (3) Permet d'avoir les indices de 0 à count-1 */
@ -510,30 +510,15 @@
/* (1) On initialise la requête */
$requestS = "SELECT ";
/* (2) Si aucun SELECT local, on prend tout */
$c = 0;
if( count($this->select) == 0 ){
$requestS .= $this->schema['table'].".*";
$c++;
}
/* (3) Si aucun SELECT de jointure, on prend tout */
foreach($this->joined as $rows)
if( count($rows->select) == 0 ){
if( $c == 0 ) $requestS .= $rows->schema['table'].".*";
else $requestS .= ', '.$rows->schema['table'].".*";
$c++;
}
/* (5) Sinon, on sélectionne les champs locaux */
/* (2) Sinon, on sélectionne les champs locaux */
foreach($this->select as $field){
if( $c == 0 ) $requestS .= $this->schema['table'].'.'.$field;
else $requestS .= ', '.$this->schema['table'].'.'.$field;
$c++;
}
/* (5) Sinon, on sélectionne les champs de jointure */
/* (3) Sinon, on sélectionne les champs de jointure */
foreach($this->joined as $rows)
foreach($rows->select as $field){
if( $c == 0 ) $requestS .= $rows->schema['table'].'.'.$field;

View File

@ -2,7 +2,8 @@
namespace manager\repo;
use \manager\Database;
use \manager\repo\uesr_cluster;
use \manager\ORM\Table;
use \manager\ORM\Rows;
class user extends parentRepo{

View File

@ -342,18 +342,18 @@
debug();
$warehouse =
Table::get('warehouse') // Access to table 'warehouse'
->whereName(['stef-montauban', Rows::COND_NOTEQ]); // condition : name = 'my-warehouse'
$myUser =
Table::get('user') // Access to table 'user'
->whereId([100, Rows::COND_INF]) // PRIMARY KEY (other condition on same field)
->whereUsername(['jo%', Rows::COND_LIKE]) // Dynamic getter 'getByMySuperColumn' -> 'my_super_column'
->select(['mail', 'username', 'firstname']) // Select clause
->select('id_user') // Select clause (added)
->join('id_warehouse', $warehouse) // joins warehouse (with name 'my-warehouse') to column 'id_warehouse'
// $warehouse =
// Table::get('warehouse') // Access to table 'warehouse'
// ->whereName(['stef-montauban', Rows::COND_NOTEQ]); // condition : name = 'my-warehouse'
//
//
// $myUser =
// Table::get('user') // Access to table 'user'
// ->whereId([100, Rows::COND_INF]) // PRIMARY KEY (other condition on same field)
// ->whereUsername(['jo%', Rows::COND_LIKE]) // Dynamic getter 'getByMySuperColumn' -> 'my_super_column'
// ->select(['mail', 'username', 'firstname']) // Select clause
// ->select('id_user') // Select clause (added)
// ->join('id_warehouse', $warehouse) // joins warehouse (with name 'my-warehouse') to column 'id_warehouse'
// ->unique() // unique result
@ -361,11 +361,39 @@
// ->fetch(); // Result
// UPDATE
->edit([
'id_warehouse' => Table::get('warehouse')->whereName('stef-montauban')->unique()->select('id_warehouse')->fetch()['id_warehouse']
// ->edit([
// 'id_warehouse' => Table::get('warehouse')->whereName('stef-montauban')->unique()->select('id_warehouse')->fetch()['id_warehouse']
// ]);
$id_user = 1;
$id_warehouse = 7;
$get_clusters = Database::getPDO()->prepare("SELECT c.*
FROM user_cluster as c, user_cluster_merge as cm, user as u
WHERE cm.id_user_cluster = c.id_user_cluster
AND cm.id_user = u.id_user
AND c.id_warehouse = u.id_warehouse
AND cm.id_user = :id_user
AND u.id_warehouse = :id_warehouse
ORDER BY c.name");
$get_clusters->execute([
':id_warehouse' => $id_warehouse,
':id_user' => $id_user
]);
var_dump( Database::delNumeric($get_clusters->fetchAll()) );
var_dump($myUser);
// With ORM
$c = Table::get('user_cluster')->whereIdWarehouse($id_warehouse)->select('*');
$cm = Table::get('user_cluster_merge')
->whereIdUser($id_user)
->join('id_user_cluster', $c);
$clusters = $cm->fetch();
var_dump($clusters);
?>