CakePHP 保存数据时遇到问题 (HABTM)

发布于 2024-10-15 11:46:25 字数 1402 浏览 2 评论 0原文

我正在尝试创建愿望清单。

有用户模型和产品模型。用户有一个愿望清单。愿望清单有很多产品。

我之所以让用户有愿望清单,而愿望清单有产品,是因为我可以有一个像wish_lists/add/:product_id这样的网址,


我用id创建了一个名为wish_lists的表, user_idname

我还使用 wish_list_idproduct_id 创建了一个名为 products_wish_lists 的表。

我在这里制作的是愿望清单控制器:

class WishListsController extends AppController 
{
    var $hasOne = 'User';
    var $hasMany = 'Product';

    function beforeFilter() 
    {        
        parent::beforeFilter();       
        $this->Auth->deny('add');    
    }

    function add($id)
    {
        $user =  $this->Session->read("Auth.User");

        $this->WishList->set(array(
            'User.id' => $user['id'],
            'Product.id'=>$id,
            'WishList.name'=>'default'
        ));

        if($this->WishList->save())
        {
            $this->Session->setFlash('This product has been added to your wishlist.', 'flash_good');
        }
        else
        {
            $this->Session->setFlash('Error: This product was not added to your wishlist.', 'flash_bad');   
        }

        $this->redirect(array("controller"=>"products","action"=>"view",$id));
    }
}

当我转到 localhost/wish_lists/add/1 时,它每次保存时都会告诉我。但没有数据被添加到数据库中。

不确定我做错了什么?

I am trying to create wish lists.

There are user and product models. A user has a wish list. A wish list has many products.

The reason I am making it user has wishlist and wishlist has products is so I can have a url like wish_lists/add/:product_id


I created a table called wish_lists with id, user_id, and name.

I also created a table called products_wish_lists with wish_list_id and product_id.

I made here is the wishlists controller:

class WishListsController extends AppController 
{
    var $hasOne = 'User';
    var $hasMany = 'Product';

    function beforeFilter() 
    {        
        parent::beforeFilter();       
        $this->Auth->deny('add');    
    }

    function add($id)
    {
        $user =  $this->Session->read("Auth.User");

        $this->WishList->set(array(
            'User.id' => $user['id'],
            'Product.id'=>$id,
            'WishList.name'=>'default'
        ));

        if($this->WishList->save())
        {
            $this->Session->setFlash('This product has been added to your wishlist.', 'flash_good');
        }
        else
        {
            $this->Session->setFlash('Error: This product was not added to your wishlist.', 'flash_bad');   
        }

        $this->redirect(array("controller"=>"products","action"=>"view",$id));
    }
}

When I go to localhost/wish_lists/add/1 It tells me everytime that it saved. but no data is being added to the database.

Not sure what I am doing wrong?

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

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

发布评论

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

评论(2

嘿看小鸭子会跑 2024-10-22 11:46:25

我从来不这样做,我总是构建一个 $data 数组作为参数一传递给保存函数。

因此,我不确定该语法是否允许您像您所做的那样指定模型,即 'Model.field'。无论如何,$this->WishList->save()只会保存Wishlist部分。

在我看来,更好的是:(

$saveData = array(
    'User'=>array('id'=>$user['id']),
    'Product'=>array('id'=>$id),
    'WishList'=>array('name'=>'default'));
$this->WishList->saveAll($saveData);

或者类似的东西,过去三个月我一直在编程 ColdFusion,我的 PHP 可能有点混乱)

I never do it that way, I always build a $data array to pass as parameter one to the save function.

I'm not sure, therefore, whether that syntax will allow you to specify the model as you have done, i.e. 'Model.field'. In any case, $this->WishList->save() will only save the Wishlist part.

Better, in my opinion, would be:

$saveData = array(
    'User'=>array('id'=>$user['id']),
    'Product'=>array('id'=>$id),
    'WishList'=>array('name'=>'default'));
$this->WishList->saveAll($saveData);

(or something like that, I've been programming ColdFusion for the last three months and my PHP can be a bit addled)

撞了怀 2024-10-22 11:46:25

您设置的数组错误。它应该是 $data['User']['id'] = 123; $数据['产品']['id'] = 321;

$this->愿望清单->saveAll($data);

保存名称是没有意义的,因为可以从产品表中找到该名称。

您可以在此处查看代码以获取更多想法 https://github.com/Infinitas-Plugins/shop

以下链接中有一个通用组件方法,可将产品保存到购物车或愿望清单(不同的数据库),因为它几乎是相同的东西。

https://github.com/Infinitas-Plugins /shop/blob/master/controllers/components/shop.php#L62

you are setting the array wrong. it should be $data['User']['id'] = 123; $data['Product']['id'] = 321;

$this->Wishlist->saveAll($data);

There is no point saving the name as that can be found from the product table.

you can have a look at the code here for more ideas https://github.com/Infinitas-Plugins/shop

there is a generic component method in the following link that saves products to the cart or wishlist (different db's) as its pretty much the same thing.

https://github.com/Infinitas-Plugins/shop/blob/master/controllers/components/shop.php#L62

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