手动打包 CakePHP 的 save() 数据

发布于 2024-12-08 02:04:00 字数 248 浏览 0 评论 0原文

我正在尝试为 cakephp 中的 save() 函数打包一些数据。我是 PHP 新手,所以我对如何在代码中实际编写以下内容感到困惑:

Array
(
    [ModelName] => Array
        (
            [fieldname1] => 'value'
            [fieldname2] => 'value'
        )
)

谢谢!

I'm trying to package up some data for the save() function in cakephp. I'm new to PHP, so I'm confused about how to actually write the below in code:

Array
(
    [ModelName] => Array
        (
            [fieldname1] => 'value'
            [fieldname2] => 'value'
        )
)

Thank you!

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

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

发布评论

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

评论(2

淡忘如思 2024-12-15 02:04:00

要回答您的问题,您可以创建所需的数组结构并保存它,方法如下:

<?php
$data = array(
    'ModelName' => array(
        'fieldname1' => 'value',
        'fieldname2' => 'value'
    )
);
$this->ModelName->save($data);
?>

请注意:
根据您在上面的评论中所写的内容,您似乎没有遵守 CakePHP 约定。可以这样做,但是如果您决定尽可能坚持 CakePHP 默认值,并且只有在有充分理由时才按照自己的方式进行操作,那么您将为自己节省大量时间和麻烦。

需要记住的几件事是:

  1. 型号名称应该是单数。这意味着您的模型应该称为 Follower 而不是 Followers。
  2. 数据库中模型的主键应仅命名为 id,而不是 followers_id,并且应在数据库中设置为 PRIMARY KEY 和 AUTO_INCRMENT。

如果您决定不遵循惯例,您可能会发现自己摸不着头脑,想知道为什么每一步都不起作用。尝试查看 CakePHP 文档 了解更多详细信息。

To answer your question, you can create the array structure you need, and save it, by doing this:

<?php
$data = array(
    'ModelName' => array(
        'fieldname1' => 'value',
        'fieldname2' => 'value'
    )
);
$this->ModelName->save($data);
?>

Please note:
Based on what you've written above in your comments it looks like you're not keeping to the CakePHP conventions. It's possible to do things this way but you'll save yourself a lot of time and trouble if you decided to stick with the CakePHP defaults as much as possible, and only do it your own way when you have a good reason to.

A couple things to remember are:

  1. Model names should be singular. This means that your model should be called Follower instead of Followers.
  2. The model's primary key in the database should be named just id, not followers_id, and should be set as PRIMARY KEY and AUTO_INCREMENT in your database.

If you decide not to follow the conventions you'll probably find yourself scratching your head, wondering why things aren't working, every step of the way. Try having a look at the CakePHP documentation for more details.

落花随流水 2024-12-15 02:04:00

我认为你需要像下面这样做:

$this->Followers->create();
$this->data['Followers']['user_id'] = $user_id;

$this->data['Followers']['follower_id'] = $follower_id; // If it is primary and auto increment than you don't need this line.

$this->Followers->save($this->data)

I think you need to do like below:

$this->Followers->create();
$this->data['Followers']['user_id'] = $user_id;

$this->data['Followers']['follower_id'] = $follower_id; // If it is primary and auto increment than you don't need this line.

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