cakephp:如何访问另一个模型,它没有关联 [英] cakephp: how to access another model, which has no association
问题描述
我有3个表:
项目
,设施
和 project_
I've 3 tables:
project
, amenities
and project_amenities
.
我只添加项目表单,我想添加新的设施,首先添加到设施表,然后project_amenities表。
I add project form only, i want to add new amenities, which first added to amenities table and then project_amenities table.
所以我定义了这些关系:
So I defined these relations:
Project hasMany Facilities
Amenity属于项目
c $ c> projects_controller :
var $ uses = array('Amenity');
I declared in projects_controller
:
var $uses = array('Amenity');
这里给我添加操作的错误
This gives error for my add action here
if (!empty($this->data)){
$this->Project->create();
为
Notice (8): Undefined property: ProjectsController::$Project
[APP\controllers\projects_controller.php, line 60]
编辑
此外,我不能:
edit Also, I cannot do:
$this->Amenity->create();
$insertData = array('name' => $this->data['Project']['Amenity'][$i]['name']);
$this->Amenity->save($insertData);
推荐答案
我建议您使用 $ this-> loadModel('ModelName');
。然后你可以使用下面的模型
I would like to suggest you to use $this->loadModel('ModelName');
in the function where you needed. Then you can use the model as below
$this->ModelName->function();
使用 $ uses
到整个控制器。这会影响您的效果。
By using $uses
array it make available to entire controller. Which will affect your performance.
这篇关于cakephp:如何访问另一个模型,它没有关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!