Php 在模型内创建虚拟字段时出错
我很难在我的一个模型中设置虚拟场。我一直在我所有的模型中设置这样的虚拟场,没有任何问题。然而,由于某种原因,我不明白,它在这个模型中根本不起作用 错误:Php 在模型内创建虚拟字段时出错,php,cakephp,Php,Cakephp,我很难在我的一个模型中设置虚拟场。我一直在我所有的模型中设置这样的虚拟场,没有任何问题。然而,由于某种原因,我不明白,它在这个模型中根本不起作用 错误: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right sy
SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'databases WHERE id = `DataSourceName`.`database_id`) AS `DataSourceName__databa' at line 1
型号:
<?php
App::uses('AppModel', 'Model');
/**
* DataSourceName Model
*
* @property Database $Database
*/
class DataSourceName extends AppModel {
public $virtualFields = array(
'database' => 'SELECT name FROM databases WHERE id = DataSourceName.database_id',
);
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Database' => array(
'className' => 'Database',
'foreignKey' => 'database_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
更新1
SQL失败:
(SELECT name FROM databases WHERE id = `DataSourceName`.`database_id`) AS `DataSourceName__database`
以下是我最终用于SQL的内容:
SELECT name FROM my_db.databases WHERE id = `DataSourceName`.`database_id`
注意表数据库前面的数据库名称前缀,即my_db。可能是因为数据库是一个保留字
最终型号:
<?php
App::uses('AppModel', 'Model');
/**
* DataSourceName Model
*
* @property Database $Database
*/
class DataSourceName extends AppModel {
public function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
$fields = get_class_vars('DATABASE_CONFIG');
$this->virtualFields['database'] = sprintf(
'SELECT name FROM %s.databases WHERE id = DataSourceName.database_id', $fields['default']['database']
);
}
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Database' => array(
'className' => 'Database',
'foreignKey' => 'database_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}
我建议您阅读更多关于虚拟字段的内容。分享失败的实际SQL代码。我猜问题在于您语句中的id列不明确。请尝试“WHERE databases.id=DataSourceName.database_id`@AgRizzo查看失败SQL的更新。我尝试了你的建议,但没有解决我的问题。
<?php
App::uses('AppModel', 'Model');
/**
* DataSourceName Model
*
* @property Database $Database
*/
class DataSourceName extends AppModel {
public function __construct($id = false, $table = null, $ds = null) {
parent::__construct($id, $table, $ds);
$fields = get_class_vars('DATABASE_CONFIG');
$this->virtualFields['database'] = sprintf(
'SELECT name FROM %s.databases WHERE id = DataSourceName.database_id', $fields['default']['database']
);
}
//The Associations below have been created with all possible keys, those that are not needed can be removed
/**
* belongsTo associations
*
* @var array
*/
public $belongsTo = array(
'Database' => array(
'className' => 'Database',
'foreignKey' => 'database_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);
}