[英]Deleting a record from a different model
I have the models: sales and follow. 我有以下模型:销售和关注。 The table follow has an id, an user_id and sale_id.
后面的表格有一个ID,一个user_id和sale_id。 What I want to do is: on the sales_controller I need to find one follow record that has the user_id that is Authenticated and the sale_id that is passed as parameter.
我想做的是:在sales_controller上,我需要找到一个跟随记录,该记录具有经过身份验证的user_id和作为参数传递的sale_id。
So here is what I've tried: 所以这是我尝试过的:
Attempt 1, 尝试1,
App::import('model','Follow');
$follow = new Follow();
$follow->user_id = $this->Auth->user('id');
$follow->sale_id = $id;
$follow->delete();
Attempt 2, 尝试2
App::import('model','Follow');
$follow = new Follow();
$follow->delete(array('Follow.user_id'=>$this->Auth->user('id'), 'Follow.sale_id'=>$id));
Attempt 3, 尝试3,
App::import('model','Follow');
$follow = new Follow();
$result = $follow->find('first',array('conditions'=>array('Follow.user_id'=>$this->Auth->user('id'), 'Follow.sale_id'=>$id)));
$result->delete()
Attempt 4, 尝试4,
var $uses = array('Sale', 'Follow');
(...)
$result = $this->Follow->find('first',array('conditions'=>array('Follow.user_id'=>$this->Auth->user('id'), 'Follow.sale_id'=>$id)));
$result->delete();
In attempt 3 and 4 $result
does return the value I was expecting, but delete doesn't work. 在尝试3和4中,
$result
确实返回了我期望的值,但是delete不起作用。
But none of these attempts worked out. 但是这些尝试都没有成功。
Try in the controller: 尝试在控制器中:
var $uses = array('MyOriginalModelName', 'Follow');
Then $this->Follow
will work. 然后
$this->Follow
将起作用。 And also It seems to be logical to add into the controller's "uses" list if you actually want to delete from it 而且,如果您确实要从控制器的“使用”列表中删除,这似乎是合乎逻辑的
On a side note you may want to check out the loadModel()
function . 附带说明一下,您可能需要检出
loadModel()
函数 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.