cakephp - 获取特定类别的项目

时间:2013-05-16 12:37:17

标签: cakephp controller

我没有获得特定项目的类别。 模型和关系

  • 1项目有许多ProjectCategory(中间表是NodeAssociation)
  • 我使用了hasAndBelongsToMany关系

    / 模型项目 /

    public $hasAndBelongsToMany = array(
        'ProjectCategory' => array(
            'className' => 'ProjectCategory',
            'joinTable' => 'NodeAssociation',
            'foreignKey' => 'skin_node_id',
            'associationForeignKey' => 'source_node_id'
        )
    );
    

/ SQL /

SELECT pc.id,pc.cname AS category

FROM project p

加入nodeassociation na

ON p.id = na.source_node_id和p.id = 10001

加入projectcategory pc

ON na.sink_node_id = pc.id AND sink_node_entity ='ProjectCategory';

FUNCTION in ProjectsController

    $joins = array(
                array('table' => 'nodeassociation',
                    'alias' => 'NodeAssociation',
                    'conditions' => array(
                        'Project.id = NodeAssociation.source_node_id',
                    )
                ),
                array('table' => 'projectcategory',
                    'alias' => 'ProjectCategory',
                    'conditions' => array(
                        'NodeAssociation.sink_node_id = ProjectCategory.id', "NodeAssociation.sink_node_entity='ProjectCategory'"
                    )
                )
            );

            $categories = $this->Project->ProjectCategory->find('all', array('joins' => $joins,
                'fields' => array('ProjectCategory.id, ProjectCategory.cname'),
                'recursive' => -1,
                'conditions' => array('Project.id' => 10001)));

错误出现在我身上

Error: SQLSTATE[42P01]: Undefined table: 7 ERRO: faltando entrada para tabela "Project" na cláusula FROM LINE 1: ...ublic"."nodeassociation" AS "NodeAssociation" ON ("Project".... ^

我做错了什么?

最好的问候

1 个答案:

答案 0 :(得分:0)

在项目模型中设置:

public $useTable = 'project';