catfan / Medoo

The lightweight PHP database framework to accelerate the development.
https://medoo.in
MIT License
4.84k stars 1.15k forks source link

Some requests : FETCH_CLASS | select_row | bypass error #308

Closed touchweb-vincent closed 3 years ago

touchweb-vincent commented 9 years ago

Hello,

This is my first post on Github, so i apologize if i don't do things in the right way.

I've discover your projet while an R&D session by night and i've decided to migrated my CMS from EZSQLcore to Medoo. You've done a great job on this class.

Here is my request (i could make a pull request in a second time if i take times to understand how it works) :

PDO::FETCH_ASSOC is replaced by PDO::FETCH_OBJ in your function "select".

This could be dynamic by adding a specific configuration in the constructor.

Same thing inside your function "query" : PDO::FETCH_CLASS (is there a bug here when you fire a SELECT ? it seems to duplicate array keys)

Something like that : public function select_row($table, $join, $columns = null, $where = null) { $query = $this->query($this->select_context($table, $join, $columns, $where));

return $query ? $query->fetchAll(
                (is_string($columns) && $columns != '*') ? PDO::FETCH_COLUMN : PDO::FETCH_OBJ
        )[0] : false;

}

Adding $protected show_errors in the class attributes and let us fix it how when want.

So within the catch of the constructor, it will be :

if ($this->show_errors) { $this->sql_statut = 0; throw new Exception($e->getMessage()); } else { $this->sql_statut = 0; }

$sql_statut will be equal to 1 if mysql is UP, 0 else. So we can catch it later.

Best regards, (and thank you for this amazing class)

Vincent

SyuTingSong commented 9 years ago

I love objects too.

SyuTingSong commented 9 years ago

I don't think change FETCH_ASSOC to FETCH_OBJ directly is a good idea. Because that would lose so much compatibility. And for different table I'd like to fetch different class instance.

I opened a pull request #313 to present another implementation.