我是cakephp的新手,一直在通过Apress一书“从新手到专业开始CakePHP”,但在烘焙章节后遇到了问题。 (如果你看过我最近的另一个问题,你就会知道我必须跳过那一章,因为我不能让我的win7机器上的烘焙控制台工作)。CakePHP协会弄糟
问题在于,我认为表格关联不再适用,即使它们在本书开始时开始使用博客应用程序示例时也是如此。
该博客实例具有以下表:
Users
Posts
用户模型:$的hasMany =阵列( '后'); Post模型:$ belongsTo = array('User');
我目前使用的脚手架只是为了测试一切,但有几个自定义视图和一个自定义add()动作的posts_controller。
问题是,当我为posts_controller使用add()操作时,用户(来自Users表)的id字段未放入Posts表的user_id外键字段中。因此,当我显示帖子时,视图的“作者”部分是空白的,因为找不到ID。如果我使用debug($posts)
,那么返回的数组没有任何关于每个帖子的user_id的信息,因此'User'数组中没有任何信息。
我以为user_id是为cakePHP添加表间关联的传统方式,但它似乎没有工作。
任何想法我需要做什么?
感谢这么多提前,
英菲尼迪菲斯
附:对不起所有人,如果我用cakephp问题压倒你。
PPS
差点忘了,我的职位add.ctp图如下所示:
<div class="posts form">
<?=$form->create('Post');?>
<fieldset>
<legend>Add Post</legend>
<?
e($form->input('name'));
e($form->input('date'));
e($form->input('content'));
e($form->input('User'));
?>
</fieldset>
<?=$form->end('Submit');?>
</div>
<div class="actions">
<ul>
<li><?=$html->link(__('List Posts', true),array('action'=>'index'));?></li>
<li><?=$html->link(__('List Users', true),array('controller','users', 'action'=>'index'));?></li>
<li><?=$html->link(__('New User', true),array('controller'=>'users', 'action'=>'add'));?></li>
</ul>
</div>
而且在posts_controller.php的add()行为:
function add()
{
if(!empty($this->data))
{
$this->Post->create();
if($this->Post->save($this->data))
{
$this->Session->setFlash('The Post has been saved', true);
$this->redirect(array('action' => 'index'));
}
else
{
$this->Session->setFlash('The Post could not be saved. Please try again.', true);
}
}
$users = $this->Post->User->find('list');
$this->set(compact('users'));
}
嗯,工作,非常感谢,我只是假设,会下降的user_id值,如1,2,3等,但耶是有效的。 那么这本书的第一个错误就是我已经找到了,最好圈一个:) – 2010-08-10 11:01:01