如何在 CakePHP 中将数据库记录从控制器传递到视图

发布于 2025-01-07 03:50:57 字数 1387 浏览 1 评论 0原文

我已经在 CakePHP 中启动了一个项目。我没有这方面的经验,我的大部分经验都是关于 Symfony2 的。

现在,我已经构建了一个视图文件、一个控制器和一个用于该项目及其数据库的模型。在一个控制器中,我想将一条记录从数据库传递到视图以在主页上使用。但是,我无法传递记录,因为我收到此错误:

注意(8):未定义的变量:content [APP\View\Contents\home.ctp, 第 42 行]

这是我的控制器代码:

function home() {
    $content = $this->Content->query("SELECT title, content FROM content WHERE id = 1;");
    $this->set('pagecontent',$content); 
}

这是我用来在视图文件中显示数据的代码:

<?php $content['content']; ?>

我错过了什么?


编辑: 我已经更改了 与罗斯的答案中概述的更改一致。然而,现在出现了这个错误:

通知(8):未定义索引:内容[APP\View\Contents\home.ctp,行 43]

我使用了 viewVars); ?> 这是其输出:

 app\View\Contents\home.ctp (line 42) Array (
     [pagecontent] => Array
         (
             [0] => Array
                 (
                     [content] => Array
                         (
                             [title] => <h1>This is a title</h1>
                             [content] => <p>this is some test text</p>
                         )

                 )

         )

 )

I've started a project in CakePHP. I have no experience of it, most of my experience is with Symfony2.

Right now, I have built a view file, a controller, and a model for use with this project and it's database. In one controller I want to pass one record from the database to the view for use on the homepage. However, I'm unable to pass the record as I get this error:

Notice (8): Undefined variable: content [APP\View\Contents\home.ctp,
line 42]

Here is my controller code:

function home() {
    $content = $this->Content->query("SELECT title, content FROM content WHERE id = 1;");
    $this->set('pagecontent',$content); 
}

And here is the code I'm using to display the data in the view file:

<?php $content['content']; ?>

What have I missed?


EDIT:
I have changed the <?php $content['content']; ?> to <?php echo $pagecontent['Content']['content']; ?> inline with the changes outlined in the answer from Ross. However, there is now this error:

Notice (8): Undefined index: Content [APP\View\Contents\home.ctp, line
43]

I have used the <?php echo debug($this->viewVars); ?> and this is the output from that:

 app\View\Contents\home.ctp (line 42) Array (
     [pagecontent] => Array
         (
             [0] => Array
                 (
                     [content] => Array
                         (
                             [title] => <h1>This is a title</h1>
                             [content] => <p>this is some test text</p>
                         )

                 )

         )

 )

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰魂雪魄 2025-01-14 03:50:57

$this->set('pagecontent',$content); - 这是您设置视图变量的位置。

您正在尝试访问名为 $content 的变量,而实际上它存储为 $pagecontent

同样基于 Cake 约定,关键 content 实际上应该是 Content,因此您的输出可能应该是:

<h1><?php echo $pagecontent['Content']['title']; ?></h1>
<div class="page-content">
    <?php echo $pagecontent['Content']['content']; ?>
</div>

You can always do debug($this->viewVars ); 以准确查看您有权访问的内容。


查看输出会有所帮助;该错误是由于 cake 返回数据的方式造成的。

这可能是由于您使用了 query() 而不是 find()read()。 (还解释了为什么您的 content 键是小写的 - query() 使用表名称,而不是模型名称。

尝试:

$content = $this->Content->find('first', 
                              array('fields' => array('title', 'content'),
                                    'conditions' => array('Content.id' => 1)
                                   )
                               );

$this->set('pagecontent', $content); 


// view
echo $pagecontent['Content']['title'];
echo $pagecontent['Content']['content'];

现在应该可以工作。

另一种方法是仅通过数组索引访问数据:

echo $pagecontent[0]['content']['title']

不像“蛋糕”那样

Expand = query() 返回的数据结构与使用不同 可以使用其他方法。

$this->Content->id = 1;
$this->set('pagecontent', $this->Content->read(array('title', 'content')));

findread

$this->set('pagecontent',$content); - this is where you are setting view variables.

You are trying to access a variable called $content, when in fact it is stored as $pagecontent.

Also based on Cake conventions, the key content should actually be Content, so your output should probably be:

<h1><?php echo $pagecontent['Content']['title']; ?></h1>
<div class="page-content">
    <?php echo $pagecontent['Content']['content']; ?>
</div>

You can always do debug($this->viewVars); to see exactly what you have access to.


Seeing the output helps; the error is due to how cake is returning the data.

This is likely due to you using query() rather than find() or read(). (also explains why your content key is lowercase - query() uses the table name, rather than the model name.

Try:

$content = $this->Content->find('first', 
                              array('fields' => array('title', 'content'),
                                    'conditions' => array('Content.id' => 1)
                                   )
                               );

$this->set('pagecontent', $content); 


// view
echo $pagecontent['Content']['title'];
echo $pagecontent['Content']['content'];

should now work.

The alternative is to just access the data through the array index:

echo $pagecontent[0]['content']['title']

which isn't as "cake" like.

to expand = query() returns data in a different structure than using find or read. There's other ways you can do this too.

$this->Content->id = 1;
$this->set('pagecontent', $this->Content->read(array('title', 'content')));

should also be valid.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文