Alors voilà j'ai développé mon propre MVC et j'ai un problème sur mes models avec le retour PDO.
Quand je fais un left join j'aimerais bien que PDO le retourne un tableau de ce genre:
object(stdClass) {
["Recette"] => object(stdClass) {
["id"] => "4"
["img"] => "monimage.jpg"
["user_id"] => "1"
}
["User"] => object(stdClass) {
["id"] => "1"
["level"] => "1"
}
["Group"] => object(stdClass) {
["id"] => "1"
["rang"] => "admin"
["color"] => "ff0000"
}
["Comment"] => object(stdClass) {
[0] => object(stdClass) {
["id"] => "1"
["recette_id"] => "5"
["user_id"] => "1"
["message"] => "test"
["created"] => "2011-12-10 10:26:53"
}
[1] => object(stdClass) {
["id"] => "2"
["recette_id"] => "5"
["user_id"] => "1"
["message"] => "test"
["created"] => "2011-12-10 10:26:53"
}
[2] => object(stdClass) {
["id"] => "3"
["recette_id"] => "5"
["user_id"] => "1"
["message"] => "test"
["created"] => "2011-12-10 10:26:53"
}
}
}
Car pour le moment j'ai un retour comme ceci:
array(2) {
[0]=>
object(stdClass)#10 (34) {
["id"]=>
string(1) "4"
["title"]=>
string(4) "test"
["photos"]=>
string(0) ""
["ingred"]=>
string(14) "50g de beurre "
["etape"]=>
string(4) "test"
["astuce"]=>
string(0) ""
["vin"]=>
string(0) ""
["online"]=>
string(1) "1"
["slug"]=>
NULL
["category_id"]=>
string(1) "2"
["user_id"]=>
string(1) "1"
["created_at"]=>
string(19) "2011-12-05 22:37:55"
["modified_at"]=>
NULL
["username"]=>
string(5) "admin"
["password"]=>
string(40) "d033e22ae348aeb5660fc2140aec35850c4da997"
["mail"]=>
string(14) "[email protected]"
["firstname"]=>
string(0) ""
["lastname"]=>
string(0) ""
["born"]=>
string(0) ""
["sexe"]=>
string(0) ""
["website"]=>
string(0) ""
["pays"]=>
string(0) ""
["ville"]=>
string(0) ""
["bio"]=>
string(0) ""
["level"]=>
string(1) "1"
["active"]=>
string(1) "1"
["ban"]=>
string(1) "0"
["delete"]=>
string(1) "0"
["name"]=>
string(14) "Administrateur"
["color"]=>
string(6) "dc143c"
["order"]=>
string(2) "10"
["recette_id"]=>
string(1) "5"
["message"]=>
string(4) "test"
["created"]=>
string(19) "2011-12-10 10:26:53"
}
[1]=>
object(stdClass)#11 (34) {
["id"]=>
string(1) "5"
["title"]=>
string(4) "test"
["photos"]=>
string(0) ""
["ingred"]=>
string(14) "50g de beurre "
["etape"]=>
string(4) "test"
["astuce"]=>
string(0) ""
["vin"]=>
string(0) ""
["online"]=>
string(1) "1"
["slug"]=>
NULL
["category_id"]=>
string(1) "2"
["user_id"]=>
string(1) "1"
["created_at"]=>
string(19) "2011-12-05 22:37:55"
["modified_at"]=>
NULL
["username"]=>
string(5) "admin"
["password"]=>
string(40) "d033e22ae348aeb5660fc2140aec35850c4da997"
["mail"]=>
string(14) "[email protected]"
["firstname"]=>
string(0) ""
["lastname"]=>
string(0) ""
["born"]=>
string(0) ""
["sexe"]=>
string(0) ""
["website"]=>
string(0) ""
["pays"]=>
string(0) ""
["ville"]=>
string(0) ""
["bio"]=>
string(0) ""
["level"]=>
string(1) "1"
["active"]=>
string(1) "1"
["ban"]=>
string(1) "0"
["delete"]=>
string(1) "0"
["name"]=>
string(14) "Administrateur"
["color"]=>
string(6) "dc143c"
["order"]=>
string(2) "10"
["recette_id"]=>
string(1) "5"
["message"]=>
string(4) "test"
["created"]=>
string(19) "2011-12-10 10:27:58"
}
}
Vous voyez c'est bordel et à chaque commentaire il me renvoit un tableau contenant tout.Donc si vous savez comment je pourrais faire pour obtenir comme le premier tableau.
Voici comment je fais une requête vers mon model:
$r = $this->Recettes->find(array(
'conditions' => array('Recettes.id' => $id),
'joins' => array(
array(
'table' => 'users',
'type' => 'left',
'conditions' => 'Recettes.user_id = Users.id'
),
array(
'table' => 'groups',
'type' => 'left',
'conditions' => 'Users.level = Groups.id'
),
array(
'table' => 'recettescomments',
'type' => 'left',
'conditions' => 'Recettes.id = Recettescomments.recette_id'
)
)
));
Et voici ma fonction find():
public function find($d = null) {
$valid = true;
if($this->request->data) {
$valid = $this->validations($this->request->data);
}
if($valid) {
$sql = 'SELECT ';
if(isset($d['fields'])) {
if(isset($d['join'])) {
}
if(!is_array($d['fields'])) {
$sql .= $d['fields'];
} else {
$sql .= implode(', ', $d['fields']);
}
} else {
$sql .= '*';
}
$sql .= ' FROM '.$this->table.' as '.get_class($this);
if(isset($d['joins'])) {
for($i = 0; $i < count($d['joins']); $i++) {
$sql .= ' '.strtoupper($d['joins'][$i]['type']).' JOIN '.strtolower($d['joins'][$i]['table']).' '.ucfirst($d['joins'][$i]['table']);
$sql .= ' ON '.$d['joins'][$i]['conditions'];
}
}
if(isset($d['conditions'])) {
$sql .= ' WHERE ';
if(!is_array($d['conditions'])) {
$sql .= $d['conditions'];
} else {
$cond = array();
foreach ($d['conditions'] as $k => $v) {
//if(!is_numeric($v)) {
$v = '"'.mysql_escape_string($v).'"';
//}
$cond[] = "$k = $v";
}
$sql .= implode(' AND ', $cond);
}
}
if(isset($d['limit'])) {
$sql .= ' LIMIT '.$d['limit'];
}
if(isset($d['order_by'])) {
$sql .= ' ORDER BY '.$d['order_by'];
}
$pre = $this->db->prepare($sql);
$pre->execute();
return $pre->fetchAll(PDO::FETCH_OBJ);
} else {
return false;
}
}
Merci d'avance......