如何在cakephp中的组件中运行查询?

时间:2013-02-07 07:21:23

标签: php cakephp cakephp-1.3 cakephp-2.0

我想在cakephp中的组件文件中运行查询,但它显示致命错误,当我在控制器中执行此操作时,它完全正常,但我想在组件中执行此操作。

 function product_check(){

 $this->User = ClassRegistry::init('User');
 $this->UserNode = ClassRegistry::init('UserNode');
 $this->ProductsUserNode = ClassRegistry::init('ProductsUserNode');

    $conditionss = array(
        # http://book.cakephp.org/2.0/en/models/retrieving-your-data.html
        'OR' => array(
            array('ProductsUserNode.'. NODE_ID => $this->Auth->user(NODE_ID)),
            array('ProductsUserNode.'. NODE_ID => $this->Auth->user(NODE_IDX)),
        ),
    );

     $productcheck = $this->User->UserNode
                                ->ProductsUserNode
                                ->find(
                                    'first'
                                    , array(
                                            'conditions' => $conditionss
                                    )   
                                );

     $active = $productcheck['ProductsUserNode']['is_active'];
    if ($active == "0")
    {
        $this->Session->setFlash('You Are Deactived For This Product'); 
        $this->redirect(array('controller'=>'events','action'=>'view_dashboard'));
    } 


}

但显示此错误

Fatal Error
Error: Call to a member function user() on a non-object 

我该如何解决这个问题?在此先感谢

2 个答案:

答案 0 :(得分:1)

我认为您没有指定您的组件使用Auth组件。您可以使用以下代码段执行此操作:

public $components = array('Auth');

另见Using other Components in your Component

答案 1 :(得分:0)

尝试通过

在product_check()中加载用户模型
  $this->ModelName = ClassRegistry::init('ModelName');

在你的情况下它是用户,这应该是理想的工作,只要确保你有:

  model : User.php 

和表格为

users
希望能帮到你!