codeigniter 4 查询生成器的问题 类
Problem with codeigniter 4 Query builder classes
在终端上,在 mysql 中,运行 以下查询给出此结果
mysql> SELECT DISTINCT(city) FROM outlets_data;
+-----------+
| city |
+-----------+
| Paris |
| New York |
| Kolkata |
| Moscow |
| Mumbai |
| Hyderabad |
| Delhi |
| Chennai |
+-----------+
8 rows in set (0.00 sec)
我想将这些城市的名称存储在一个数组中,在 codeigniter 4 模型 class 文件中。
Models/DashboardModels.php
<?php
namespace App\Models;
use CodeIgniter\Model;
class DashboardModel extends Model
{
protected $table = 'outlets_data';
protected $primaryKey = 'shop_id';
public function not_defined_yet()
{
$city_names = $this->select('city')->distinct(); // This should be equivalent to "SELECT DISTINCT(city) FROM outlets_data";
return $city_names;
}
}
Controller/Home.php
<?php
namespace App\Controllers;
use App\Models\DashboardModel;
use CodeIgniter\Model;
class Home extends BaseController
{
public function index()
{
$model = new DashboardModel();
$data['undefined'] = $model->not_defined_yet();
echo view('dashboard', $data);
}
}
Views/Dashboard.php
<?php echo "<pre>"; print_r($undefined); echo "</pre>"; ?>
我希望在输出数组中获取城市名称,但我将整个数据库作为关联数组获取。
你的函数应该是:
public function not_defined_yet()
{
$city_names = $this->select('city')->distinct(); // This should be equivalent to "SELECT DISTINCT(city) FROM outlets_data";
return $this;
}
那么你的函数就是
$data['undefined'] = $model->not_defined_yet()->findAll();
您可以使用的其他方法是加载数据库对象的新实例。
public function not_defined_yet()
{
$db = \Config\Database::connect();
$builder = $db->table('outlets_data');
$city_names = $builder->select('city')->distinct();
return $city_names->resultArray();
}
您甚至可以一起删除该功能,并在您的控制器中执行此操作:
$data['undefined'] = $model->select('city')->distinct()->findAll();
这会得到完全相同的结果。
在终端上,在 mysql 中,运行 以下查询给出此结果
mysql> SELECT DISTINCT(city) FROM outlets_data;
+-----------+
| city |
+-----------+
| Paris |
| New York |
| Kolkata |
| Moscow |
| Mumbai |
| Hyderabad |
| Delhi |
| Chennai |
+-----------+
8 rows in set (0.00 sec)
我想将这些城市的名称存储在一个数组中,在 codeigniter 4 模型 class 文件中。
Models/DashboardModels.php
<?php
namespace App\Models;
use CodeIgniter\Model;
class DashboardModel extends Model
{
protected $table = 'outlets_data';
protected $primaryKey = 'shop_id';
public function not_defined_yet()
{
$city_names = $this->select('city')->distinct(); // This should be equivalent to "SELECT DISTINCT(city) FROM outlets_data";
return $city_names;
}
}
Controller/Home.php
<?php
namespace App\Controllers;
use App\Models\DashboardModel;
use CodeIgniter\Model;
class Home extends BaseController
{
public function index()
{
$model = new DashboardModel();
$data['undefined'] = $model->not_defined_yet();
echo view('dashboard', $data);
}
}
Views/Dashboard.php
<?php echo "<pre>"; print_r($undefined); echo "</pre>"; ?>
我希望在输出数组中获取城市名称,但我将整个数据库作为关联数组获取。
你的函数应该是:
public function not_defined_yet()
{
$city_names = $this->select('city')->distinct(); // This should be equivalent to "SELECT DISTINCT(city) FROM outlets_data";
return $this;
}
那么你的函数就是
$data['undefined'] = $model->not_defined_yet()->findAll();
您可以使用的其他方法是加载数据库对象的新实例。
public function not_defined_yet()
{
$db = \Config\Database::connect();
$builder = $db->table('outlets_data');
$city_names = $builder->select('city')->distinct();
return $city_names->resultArray();
}
您甚至可以一起删除该功能,并在您的控制器中执行此操作:
$data['undefined'] = $model->select('city')->distinct()->findAll();
这会得到完全相同的结果。