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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?