简体   繁体   English

Cakephp模型忽略数据库设置

[英]Cakephp Model ignoring database settings

Update: please see my EDIT section below where I explain what I have discovered after testing 更新:请参阅下面的“编辑”部分,其中解释了测试后发现的内容

I'm working on a website with CakePHP 2.5.1, and I'm experiencing a very strange behavior: one of the 2 models I have seems to refuse to use a different database configuration (other than the default one) if I don't place it in Cake's default Model folder (if I place it there everything works great). 我正在使用CakePHP 2.5.1在网站上,和我遇到一个很奇怪的现象:2个模型我有一个 似乎拒绝使用不同的数据库配置(而不是默认的),如果我不”请将其放置在Cake的默认“ 模型”文件夹中(如果在该文件夹中,一切正常)。

Both models are located in the same folder, which happens to be not the default Model folder (this is because those are shared with other websites and I have a central place where I put all the shared ones). 两种模型都位于同一文件夹中,该文件夹恰好不是默认的“模型”文件夹 (这是因为它们与其他网站共享,并且我将所有共享的文件放在一个中央位置)。

To make Cake find those models I use the following line of code in the bootstrap.php file: App::build(array('Model' => array(dirname(ROOT) . DS . '_shared/Cake_v2/Model'))); 为了使Cake找到那些模型,我在bootstrap.php文件中使用以下代码行: App::build(array('Model' => array(dirname(ROOT) . DS . '_shared/Cake_v2/Model')));

The 2 models I have there are called Campanya and EjercicioMultChoice . 我在那儿拥有的2个模型分别是CampanyaEjercicioMultChoice

Everything seems to be setup properly: 一切似乎都已正确设置:

  • If I print App::objects('Model') I can see both models listed there, which means Cake is finding them in that external folder 如果我打印App::objects('Model')我会看到那里列出的两个模型,这意味着Cake在该外部文件夹中找到了它们。
  • If I print get_class_vars('DATABASE_CONFIG') I can see both database configurations: the default one, used by Campanya , and the custom one (called BD_Contenidos ), used by EjercicioMultChoice . 如果我打印get_class_vars('DATABASE_CONFIG') ,则可以看到两种数据库配置: Campanya使用的默认数据库配置和EjercicioMultChoice使用的自定义配置数据库(称为BD_Contenidos )。
  • Both models are included in the AppController.php with the $uses variable array. 两种模型都包含在AppController.php中,并带有$uses变量数组。

I can call the Campanya model in the controller without any problem. 我可以在控制器中调用Campanya模型,没有任何问题。 However, when call the EjercicioMultChoice model, I get the following error: 但是,当调用EjercicioMultChoice模型时,出现以下错误:

MISSING DATABASE TABLE
Error: Table ejercicio_mult_choices for model EjercicioMultChoice was not found in datasource default.

This is how the EjercicioMultChoice model file looks like: 这就是EjercicioMultChoice模型文件的样子:

class EjercicioMultChoice extends AppModel {

public $name = 'EjercicioMultChoice';
public $primaryKey = 'id';
public $useTable = 'ejercicios_multiple_choice';
public $useDbConfig = 'BD_Contenidos'; 
}

As you can see, that model indicates to use a specific database configuration, which Cake is aware of, but in the error shown above it's saying that it can't find the table in datasource default . 如您所见,该模型指示使用Cake知道的特定数据库配置,但是在上面显示的错误中,它表示无法在数据源default中找到该表。 It can't because it's not there of course, it's in another table in another database (which by the way this other database is in the same server as the default one and I use it without issues on other projects). 当然不能,因为它不存在,它在另一个数据库的另一个表中(顺便说一下,另一个数据库与默认数据库位于同一服务器中,而我在其他项目上使用它时都没有问题)。

As I mention above, if I move that exact same EjercicioMultChoice.php model file to Cake's default app/Model/ folder, then I don't have any issues. 正如我上面提到的,如果我将完全相同的EjercicioMultChoice.php模型文件移动到Cake的默认app / Model /文件夹中,那么我没有任何问题。

So I'm a bit lost here, I don't know what else I can check to find what the issue is. 所以我在这里有点迷失了,我不知道还有什么可以检查以找出问题所在。

EDIT 编辑

So I believe I've discovered what the problem is, and a workaround about it, but I still would like to know why this is happening and how can I make it work as it should. 因此,我相信我已经找到了问题所在,并找到了解决方法,但我仍然想知道为什么会发生这种情况,以及如何使它正常工作。

If I add these 2 lines of code ( both of them ) before calling the model then it all works fine: 如果我在调用模型之前添加了这两行代码( 两者 ),那么一切都很好:

$this->EjercicioMultChoice->setDataSource('BD_Contenidos');
$this->EjercicioMultChoice->setSource('ejercicios_multiple_choice');

This clearly means that even though Cake finds the EjercicioMultChoice.php model file, which indicates the database and table names, it ignores those 2 variables ( $useTable and $useDbConfig ). 显然,这意味着即使Cake找到了指示数据库和表名称的EjercicioMultChoice.php模型文件,它仍会忽略这两个变量( $ useTable$ useDbConfig )。 The only way to make it work is by manually setting those 2 variables with the methods that I wrote above. 使其工作的唯一方法是使用我上面编写的方法手动设置这两个变量。

Why is this happening? 为什么会这样呢? This does not happen if the model file is in the default folder... 如果模型文件位于默认文件夹中,则不会发生这种情况。

Well well well, it seems that the problem was that I didn't put the trailing / in the App::build() path. 好吧,好吧,看来问题是我没有在App :: build()路径中添加尾随/ The correct one should be App::build(array('Model' => array(dirname(ROOT) . DS . '_shared/Cake_v2/Model/'))); 正确的应该是App::build(array('Model' => array(dirname(ROOT) . DS . '_shared/Cake_v2/Model/')));

What made it hard for me to figure this out is that Cake was actually finding the models in that folder, even without the trailing / , but for some reason that prevented it from loading the internal contents of those model files. 使我很难弄清楚的是,即使没有后缀/ ,Cake仍在该文件夹中实际上找到了模型,但由于某种原因,阻止了它加载那些模型文件的内部内容。

Anyway, it works now! 无论如何,现在就可以使用!

As far as i can tell, cake is NOT loading your provided model file! 据我所知,cake没有加载您提供的模型文件! You can find out by putting an exit(); 您可以通过添加一个exit();来找出答案exit(); call in the first line of that (shared) file and see if your app honors that (by showing a white page ,-)). 在该(共享)文件的第一行中调用,以查看您的应用是否支持该操作(通过显示白页,-)。

If not, your model file does not get picked up by cakephp. 否则,cakephp将不会拾取您的模型文件。

Another way to check if your model file gets picked up is to define a custom member variable like public $myModelTestVar = 'sharedModel'; 检查模型文件是否被拾取的另一种方法是定义一个自定义成员变量,例如public $myModelTestVar = 'sharedModel'; to the file and just check for the variable in the controller that uses the model (just debug($this->EjercicioMultChoice) in an action). 到文件,然后在使用模型的控制器中检查变量(只需在操作中debug($this->EjercicioMultChoice) )。

If you can confirm/proove that the file is picked up, I would volunteer to set this scenario up and try myself whats going on there - but am very certain I am right. 如果您可以确认/证明文件已被拾取,我将自愿设置此方案并尝试自己发生的事情-但可以肯定的是,我是对的。

debug(dirname(ROOT) . DS . '_shared/Cake_v2/Model')

to proove it's pointing to where you expect. 证明它指向您的期望。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM