所选表的cakephp字段

所选表的cakephp字段,php,mysql,cakephp,Php,Mysql,Cakephp,控制器中的代码如下所示: public function leftsidebar(){ $this->layout=false; $productlist = $this->Product->Category->find('all'); pr($productlist); $this->set(compact('productlist')); $this->render('left

控制器中的代码如下所示:

public function leftsidebar(){
        $this->layout=false;
        $productlist = $this->Product->Category->find('all');
        pr($productlist);
        $this->set(compact('productlist'));
        $this->render('leftsidebar');
    }
它输出为

Array
(
    [0] => Array
        (
            [Category] => Array
                (
                    [id] => 1
                    [name] => Cat New Name
                    [image] => Cat New Name_9547.jpg
                    [description] => Description  heeeererer
                    [active] => 1
                    [created] => 2015-02-17 12:07:43
                )

            [Product] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [category_id] => 1
                            [productname] => CCTV products
                            [sortorder] => 0
                            [productimage] => CCTV products_4939.jpg
                            [description] => Some Description for product goes here
                            [created] => 2015-02-17 02:00:48
                            [modified] => 2015-02-17 02:00:48
                        )

                    [1] => Array
                        (
                            [id] => 3
                            [category_id] => 1
                            [productname] => Product Name
                            [sortorder] => 0
                            [productimage] => Cat Names _23749.jpg
                            [description] => Description
                            [created] => 0000-00-00 00:00:00
                            [modified] => 0000-00-00 00:00:00
                        )

                )

        )

    [1] => Array
        (
            [Category] => Array
                (
                    [id] => 2
                    [name] => Name
                    [image] => Cat New Name_13850.jpg
                    [description] => Description 
                    [active] => 0
                    [created] => 2015-02-17 12:18:18
                )

            [Product] => Array
                (
                )

        )

)
现在,从Product table中,我只需要id、category_id、productname等选定字段,从category table中只需要id和名称,
那么我该如何实现它呢?提前谢谢你

你只需要在find函数中传递一个fields数组。您必须已设置要使用以下技巧的模型关系

public function leftsidebar(){
        $this->layout=false;
        $productlist = $this->Product->Category->find('all', array(
"fields" => array("Product.id", "Product.category_name","Product.name", "Category.id", "Category.name")
));
        pr($productlist);
        $this->set(compact('productlist'));
        $this->render('leftsidebar');
    }

有关运行时关系,请参阅包含CAKEPHP的行为。

只需传递第二个参数即可找到方法:

    $fields = array("Product.id", "Product.category_name","Product.name", "Category.id", "Category.name");
    $productlist = $this->Product->Category->find('all',$fields);
继续你的常规代码

$this->Product->Category->find('all',array(array("Product.id", "Product.category_name","Product.name", "Category.id", "Category.name"););
在Cakephp中,字段是一个属性,您可以在其中定义表字段名称,如果您想定义所有使用带有模型名的*运算符的字段,例如Product.*。下面是您的代码,请检查此项

     public function leftsidebar(){
            $this->layout=false;
            $this->loadModel('Category');
            $productlist = $this->Product->Category->find('all',
            'fields' => array('Product.id', 'Product.category_name','Product.name', 'Category.id', 'Category.name')
            );
            pr($productlist);
            $this->set(compact('productlist'));
            $this->render('leftsidebar');
        }