du512926 2012-07-02 22:51
浏览 46
已采纳

CakePHP HABTM发现...没有返回预期的结果

I'm hoping somebody can clear up this issue I am having. None of the other answers on SO seemed to help me out for some reason.

I have 2 tables with a HABTM relationship. Publications have many authors, and authors have many publications. In my case, I am attempting to output a list of all of the publications in the database, along with their corresponding authors.

I have the following tables:

publications:

id title

0 TestPublication

authors:

id firstname lastname middle

0 John Doe A

authors_publications:

id author_id publication_id

0 0 0

The 'id' column of each table is set as the primary key.

My Publication model looks like:

class Publication extends AppModel {

    var $name = 'Publication';
    var $hasAndBelongsToMany = array(
        'Author'=>array(
            'className'=>'Author'
        )
    );     
}

And finally, the PublicationsController has the following function:

function index() {
$publications = $this->Publication->find('all');
$this->set('publications', $publications);
}

Here is what publications now contains:

Array ( [0] => Array ( [Publication] => Array ( [id] => 0 [title] => TestPublica [type_id] => 1 ) [Author] => Array ( ) ) ) 

Why is this? I am expecting (perhaps that is my problem...) that the author John Doe should be present in the Author array. If it should be, where am I going wrong? Do I need a bindModel call somewhere in there?

Or...is the code actually executing the way it should and my expectations are incorrect? If so, how would I return a list of all of the publications along with all of their authors?

Thanks for your time!

  • 写回答

1条回答 默认 最新

  • dongtangze6393 2012-07-03 03:54
    关注

    I believe I found the problem. I was manually inserting records into the DB for testing purposes. I set the primary key id's on the first record of each table to zero. SQL DBs typically (or always?) start the first record with a primary key index of 1, not 0. I'm not sure WHY cake did not work with this, but changing the id's to 1 solved the problem. Weird...

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 nslt的可用模型,或者其他可以进行推理的现有模型
  • ¥15 arduino上连sim900a实现连接mqtt服务器
  • ¥15 vncviewer7.0安装后如何正确注册License许可证,激活使用
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并2
  • ¥66 关于人体营养与饮食规划的线性规划模型
  • ¥15 基于深度学习的快递面单识别系统
  • ¥15 Multisim仿真设计地铁到站提醒电路
  • ¥15 怎么用一个500W电源给5台60W的电脑供电
  • ¥15 请推荐一个轻量级规则引擎,配合流程引擎使用,规则引擎负责判断出符合规则的流程引擎模板id
  • ¥15 Excel表只有年月怎么计算年龄