CakePhp模型找到关联

时间:2013-11-20 07:15:38

标签: cakephp model associations

似乎是,我太傻了。我在这里和其他论坛上阅读了文档和maaaaany问题,但我仍然无法获得带有关联运行的find方法... 似乎有一个初学者的错误...

好的,这里是代码:

ScriptParam - 型号:

class ZToolScriptParam extends ScriptBuilderAppModel {
public $useTable ="z_tool_script_params";
public $primaryKey = "script_param_id"; 

 public $script_id;
public $parameter_name= "";
public $parameter_type= "";
 public $parameter_len= "";
public $parameter_right_len= "";

public $belongsTo = array(
    'ScriptBuilder.ZToolScript' => array(
        'className' => 'ScriptBuilder.ZToolScript',
        'foreignKey' => 'script_id',
    )
);
public $validate = array(

);

}

脚本 - 型号:

class ZToolScript extends ScriptBuilderAppModel {
public $useTable ="z_tool_scripts";
public $primaryKey = "script_id";

public $script_name= "";
public $script_path= "";
public $script_file= "";

public $validate = array(
    'script_name' => array(
        'rule' => 'notEmpty'
    ),

);
public $hasMany = array(
    'ScriptBuilder.ZToolScriptParam' => array(
        'className' => 'ScriptBuilder.ZToolScriptParam',
        'foreignKey' => 'script_id',
        'dependent' => false
    )
);
}

我希望在调用find-method时,我会得到模型和相关模型。

$scripts = $this->ZToolScript->find('all');

但我没有得到相关的模型:

Array
(
[0] => Array
    (
        [ZToolScript] => Array
            (
                [script_name] => test_sp2
                [script_path] => \webroot\scripte\
                [script_file] => test_sql.txt
                [script_id] => 1
            )

    )

我希望我犯了一个简单的错误但不幸的是我没有看到错误。有人能帮助我吗?

格尔茨

V

编辑:1

我更正了“ZToolScript”的链接。

编辑:2

添加模型的表格架构:

CREATE TABLE [dbo].[z_tool_scripts](
    [script_name] [nvarchar](50) NULL,
    [script_path] [nvarchar](255) NULL,
    [script_file] [nvarchar](255) NULL
) 
GO
ALTER TABLE dbo.z_tool_scripts
   ADD script_id INT IDENTITY   

ALTER TABLE dbo.z_tool_scripts
   ADD CONSTRAINT PK_z_tool_scripts
   PRIMARY KEY(script_id)
GO
------------------------------------------------------------------------
CREATE TABLE [dbo].[z_tool_script_params](
    [parameter_name] [nvarchar](50) NULL,
    [parameter_type] [nvarchar](50) NULL,
    [parameter_len] [int] NULL,
    [parameter_right_len] [nchar](10) NULL,
    [script_id] [int] NOT NULL,
    [change_date] [date] NULL)

GO
ALTER TABLE dbo.z_tool_script_params
   ADD script_param_id INT IDENTITY 

ALTER TABLE dbo.z_tool_script_params
   ADD CONSTRAINT PK_z_tool_script_params
   PRIMARY KEY(script_param_id)

ALTER TABLE dbo.z_tool_script_params
add constraint z_tool_script_params_script_id_FK FOREIGN KEY ( script_id ) references  z_tool_scripts(script_id)

编辑3:

好的,这是一个很长的问题: 我有另一个但类似的问题。我尝试创建一个多对多的关联:

<?php
App::uses('ProjectAdminAppModel', 'ProjectAdmin.Model');
class ZToolProject extends ProjectAdminAppModel {
public $useTable ="z_tool_project_steps";
public $primaryKey = "id";
public $name= "";
public $desc = "";
public $activ_step = true;
public $order = 0;
public $project_id;

public $hasAndBelongsToMany = array(
    'ZToolScript' =>
        array(
            'className' => 'ScriptBuilder.ZToolScript',
            'joinTable' => 'z_tool_psteps_scripts',
            'foreignKey' => 'step_id',
            'associationForeignKey' => 'script_id',
            'unique' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'with' => ''
        )
);
}

连接的脚本模型与上面描述的相同。 以下是更新的Create-SQL-Scripts:

CREATE TABLE [dbo].[z_tool_project_steps](
[name] [nvarchar](50) NULL,
[desc] [text] NULL,
[activ_step] [bit] NULL,
[order] [int] NULL,
[project_id] [int] NULL,
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

ALTER TABLE dbo.z_tool_project_steps
   ADD id INT IDENTITY  

ALTER TABLE dbo.z_tool_project_steps
   ADD CONSTRAINT PK_z_tool_projects_steps
   PRIMARY KEY(id)

ALTER TABLE dbo.z_tool_project_steps
add constraint z_tool_project_steps_project_id_FK FOREIGN KEY ( project_id ) references z_tool_projects(id)

    CREATE TABLE [dbo].[z_tool_psteps_scripts](
    [step_id] [int] NULL,
    [script_id] [int] NULL
) ON [PRIMARY]

GO

ALTER TABLE dbo.z_tool_psteps_scripts
   ADD con_id INT IDENTITY  

ALTER TABLE dbo.z_tool_psteps_scripts
   ADD CONSTRAINT PK_z_tool_psteps_scripts
   PRIMARY KEY(con_id)

ALTER TABLE dbo.z_tool_psteps_scripts
add constraint z_tool_psteps_scripts_script_id_FK FOREIGN KEY ( script_id ) references z_tool_scripts(script_id)

ALTER TABLE dbo.z_tool_psteps_scripts
add constraint z_tool_psteps_scripts_step_id_FK FOREIGN KEY ( step_id ) references z_tool_project_steps(id)

问题似乎是一样的。我也没有与正常的find-method相关联。这是主要问题吗?我无法想象,这是一个很大的问题。 Pleeeeeease可以帮助我吗???

1 个答案:

答案 0 :(得分:0)

好的,我发现了我的问题。它没有显示在上面显示的代码上....我的问题是,我加载模型如:

$this->loadModel("ZToolScript");

问题是,该模型是一个插件,所以我必须定义插件,如:

$this->loadModel("ScriptBuilder.ZToolScript");

我没有说过这个问题,因为其他任何事情都没有问题,只有关联......

也许这可以帮助任何其他人。

格尔茨

V