[英]CakePHP having trouble saving data (HABTM)
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 我将其设置为用户拥有心愿单,而心愿单具有产品的原因是,这样我可以拥有一个网址,例如wish_lists / add /:product_id
I created a table called wish_lists
with id
, user_id
, and name
. 我用id
, user_id
和name
创建了一个名为wish_lists
的表。
I also created a table called products_wish_lists
with wish_list_id
and product_id
. 我还创建了一个名为wish_list_id
和product_id
的表products_wish_lists
。
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. 当我转到localhost/wish_lists/add/1
它每次保存时都会告诉我。 but no data is being added to the database. 但没有数据被添加到数据库中。
Not sure what I am doing wrong? 不确定我在做什么错吗?
I never do it that way, I always build a $data
array to pass as parameter one to the save function. 我从来没有那样做,我总是建立一个$data
数组作为参数1传递给save函数。
I'm not sure, therefore, whether that syntax will allow you to specify the model as you have done, ie 'Model.field'
. 因此,我不确定该语法是否允许您像完成操作一样指定模型,即'Model.field'
。 In any case, $this->WishList->save()
will only save the Wishlist part. 无论如何, $this->WishList->save()
将只保存Wishlist部分。
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) (或者类似的东西,最近三个月我一直在编程ColdFusion,而我的PHP可能会有点儿混乱)
you are setting the array wrong. 您将数组设置错误。 it should be $data['User']['id'] = 123; 应该是$ data ['User'] ['id'] = 123; $data['Product']['id'] = 321; $ data ['Product'] ['id'] = 321;
$this->Wishlist->saveAll($data); $ 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 您可以在这里查看代码以获取更多想法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 https://github.com/Infinitas-Plugins/shop/blob/master/controllers/components/shop.php#L62
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.