وقتی میخوام از دیتابیس اون دیتایی که ثبت کردم بگیرم با این ارور مواجه میشم همه چی رو. با خود استاد چک کردم متوجه خطام نمیشم
با توجه به اینکه تمامی کدها در پروژه ی قبلی تست شدن
ممنون میشم کمک کنید.
سلام محمد جان، لطفاً کدتون رو هم بفرستین تا بتونیم راهنمایی تون کنیم.
صادق برزگر۲۶ شهریور ۱۴۰۲، ۱۴:۲۸
namespace App\\Controllers;
use App\\Models\\Contact;
class HomeController {
private $contactModel ;
public function __construct(){
$this->contactModel = new Contact();
}
public function index()
{
echo "hi from controller of phonebook";
$allContacts = $this->contactModel->getAll();
nice_dump($allContacts);
}
}
rahnama۲۶ شهریور ۱۴۰۲، ۱۴:۳۳
namespaceApp\\Models\\Contracts;
useException;
useMedoo\\Medoo;
abstractclassMysqlBaseModelextendsBaseModel{
publicfunction__construct($id = null){
//$this->connection = new \\PDO( "mysql:dbname={$_ENV['DB_NAME']};host=$_ENV{['DB_HOST']}",$_ENV['DB_USER'],$_ENV['DB_PASS']);try {
$this->connection= new Medoo([
'type' => 'mysql',
'host' => $_ENV['DB_HOST'],
'database' => $_ENV['DB_NAME'],
'username' => $_ENV['DB_USER'],
'password' => $_ENV['DB_PASS'],
// [optional]'charset' => 'utf8mb4',
'collation' => 'utf8mb4_general_ci',
'port' => 3306,
// [optional] The table prefix. All table names will be prefixed as PREFIX_table.'prefix' => '',
// [optional] To enable logging. It is disabled by default for better performance.'logging' => true,
// [optional]// Error mode// Error handling strategies when the error has occurred.// PDO::ERRMODE_SILENT (default) | PDO::ERRMODE_WARNING | PDO::ERRMODE_EXCEPTION// Read more from https://www.php.net/manual/en/pdo.error-handling.php.'error' => \\PDO::ERRMODE_EXCEPTION,
// [optional]// The driver_option for connection.// Read more from http://www.php.net/manual/en/pdo.setattribute.php.// [optional] Medoo will execute those commands after the database is connected.'command' => [
'SET SQL_MODE=ANSI_QUOTES'
]
]);
} catch (Exception$a) {
echo"Connection Faild : " . $a->getMessage();
}
if (!is_null($id)) {
nice_dump( $this->find($id)) ;
}
}
# Creat (insert)publicfunctioncreate($new_data) : int{
$this->connection->insert($this->table,$new_data);
return(int)$this->connection->id();
}
# Read (select)publicfunctionfind($id)
{
$record = (object)$this->connection->get($this->table,"*", [$this->primaryKey=>$id]);
foreach($recordas$col => $val)
$this -> attributes[$col]=$val;
return$this;
}
publicfunctiongetAll(){
return$this->connection->select($this->table,['*']); ;
}
publicfunctionget(array$columns, array$where) : array{
return$this->connection->select($this->table, $columns, $where);
}
# Updatepublicfunctionupdate(array$data,array$id) : int{
return (int)$this->connection-> update($this->table, $data,[$this->primaryKey=>$id]);
}
# Delete publicfunctiondelete(array$id) : int{
return (int)$this->connection-> delete($this->table,[$this->primaryKey=>$id]);
}
publicfunctionremove():int{
$record_id =$this->{$this->primaryKey} ;
return$this->delete([$this->primaryKey=>$record_id]);
}
publicfunctionsave():int{
$record_id =$this->{$this->primaryKey} ;
return$this->update($this->attributes,[$this->primaryKey=>$record_id]);
}
}