我在我的数据库中有一个表“assets”(id,name,serial),以及一个存储在外部数据库中的只读表ASSET(大写)(ID,NAME,SERIAL)(在app.php中声明)
在我的app.php中,我把连接:
'Datasources' => [
'default' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'db1',
'encoding' => 'utf8',
'quoteIdentifiers' => false,
'url' => env('DATABASE_URL', null),
],
'external_db' => [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'persistent' => false,
'host' => 'domain.com',
'port' => '3306',
'username' => 'my_user',
'password' => 'my_password',
'database' => 'db2',
'encoding' => 'utf8',
'timezone' => 'UTC',
'flags' => [],
'cacheMetadata' => false,
'quoteIdentifiers' => false,
'log' => false,
'url' => env('DATABASE_URL', null),
],
我能够:蛋糕烘焙所有资产(来自默认数据库)。由于蛋糕烘焙不支持大写,我手动创建:ExternalAssetsTable.php:
class ExternalAssetsTable extends Table {
public function initialize(array $config)
{
parent::initialize($config);
$this->ExternalAsset->setDataSource('external_db');
$this->setTable('ASSET');
$this->setDisplayField('NAME');
$this->setPrimaryKey('ID');
然后,我尝试使用以下命令创建控制器和模板视图:
cake bake controller external_assets
cake bake template external_assets
和:蛋糕烘焙控制器ASSET -c external_db蛋糕烘焙模板ASSET -c external_db
并且代码不起作用。
我的第一个问题:我的代码出了什么问题?
我的第二个需求如下:
“assets”包含从ASSET导入的数据,以及在我的数据库中手动输入的其他数据。
SERIAL可能偶尔会改变,我需要将其导入资产。所以foreach name = NAME,如果是串行<> SERIAL则串口:= SERIAL。
在资产index.ctp和view.ctp中,我需要显示以下列:id,name,serial,以及相应的ASSET.NAME,ASSET.SERIAL(如果存在)。
我试图在AssetsController.php中添加一个函数
public function getRemoteData(){
$conn1 = ConnectionManager::get('k1000db'); #Remote Database 1
$sql = "SELECT * FROM ASSET";
$query = $conn1->prepare($sql);
$query->execute();
$result = $query->fetchAll(); #Here is the result
}
我的问题是:如何在AssetsController index(),view()函数和Assets中调用此函数index.ctp?
非常感谢您的帮助。
在你的第一个问题中:
cake bake
根据您在数据库中的表格制作所有内容,因此,您的代码是:
cake bake ASSETS -c your_external_db_here
如果使用上面的代码,你可以检查Cakephp所有可以使用bake
的表:
cake bake -c your_external_db_here
在你制作之后,手动将你的ASSETS
重命名为ExternalAssets
,Controller
,Template folder
和其他你baked
的东西,以更好地识别表格。但要小心这个cake bake
不会取代你以前烤过的其他assets
。
在第二个问题中:
您将在AssetTable中制作一个Association
:
public function initialize(array $config)
{
parent::initialize($config);
$this->hasOne('ExternalAssets')->setForeignKey('fk_here')->('pk_here');
}
在您的控制器中:
public function index()
{
$assets = $this->Assets->find('all')
->select([ 'id', 'name', 'serial'])
->contain(['ExternalAssets' => [
'fields' => [
'NAME','SERIAL'
]
]);
//if you wanna paginate your resut
//$assets = $this->paginate($assets);
$this->set(compact('assets'));
$this->set('_serialize', ['assets']);
}
public function view($id = null)
{
$assets= $this->Assets->get($id, [
'contain' => []
]);
$this->set('assets', $assets);
$this->set('_serialize', ['assets']);
}
我不可能用蛋糕烘烤外表,因为它不遵守cakephp惯例。所以作为替代方案,我在我的index()中使用了它:
$connection = ConnectionManager::get('db2');
$machines = $connection->execute('SELECT * FROM MACHINE ORDER BY NAME ASC');
$this->set(compact('machines'));
$this->set('_serialize', ['machines']);
然后,我手动创建了视图。