简体   繁体   English

$ this->发现在cakePHP中不起作用

[英]$this->find not working in cakePHP

I have a model with the name Deal class Deal extends AppModel 我有一个名称为Deal class Deal extends AppModel

Now in my controller I call a method in the Deal model called getDeal() 现在,在控制器中,我在Deal模型中调用了一个名为getDeal()的方法。

$dealInfo = Deal::getDeal($dealID); $ dealInfo = Deal :: getDeal($ dealID);

I want the info returned to me but the var_dump displays blank 我希望信息返回给我,但var_dump显示为空白

function getDeal($dealID){

    $deal = $this->Deal->find('first', array(
                            'conditions' => 
                                'Deal.id' =>$dealID

                            ) ,
                            'fields' => array(
                                'Deal.id'
                            ) ,

                            'recursive' => 1,

                            ));
}

This is the first time I'm working in cakePHP, so this question might sound a bit dumb 这是我第一次使用cakePHP,因此这个问题听起来可能有些愚蠢

When you're just using an id as your find condition you can use CakePHP's dynamic finder methods. 当您仅使用id作为查找条件时,可以使用CakePHP的动态查找器方法。

Dynamic finders work like this. 动态查找器的工作原理是这样的。

$this->Model->findByModelFieldName();

In your example it would be 在您的示例中

$this->findById($dealId, array(
    'fields' => array('Deal.id'),
    'recursive' => 1
));

I don't know if I'm just being mental, but is this simply because you're not returning anything from getDeal() ? 我不知道我是否只是在头脑,这仅仅是因为您没有从getDeal()返回任何内容吗?

Try adding return $deal; 尝试添加return $deal; and see if that helps. 看看是否有帮助。 Your question doesn't state exactly where you're doing the var_dump so I might be well off. 您的问题并未明确说明您在哪里进行var_dump因此我可能会过得很好。

Edit: 编辑:

As per the discussion with you and 8vius, we've established that this isn't right, and you simply need to change $this->Deal->find() to $this->find() because its being run from the model. 根据与您和8vius的讨论,我们已经确定这是不对的,您只需要将$this->Deal->find()更改$this->find()因为它是从模型。

Check your function declaration, $deal_id does not exist and as you can see from the parameter you pass to the function which should me $deal_id and not dealID . 检查您的函数声明, $deal_id不存在,并且如您从参数中看到的那样,您将传递给应该$deal_id而不是dealID So you have a misdeclared function calling find with a variable that does not exist. 因此,您有一个错误声明的函数,该函数使用不存在的变量调用find。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM