所选表的cakepp字段


cakephp fields of selected tables

我在控制器中的代码看起来是:

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表中,我只需要选择id、category_id、productname等字段,而从类别表中,只需要id和name,那么我该如何实现呢?提前感谢

在Cakephp中,字段是您可以定义表字段名称的属性,如果您想定义所有字段,请使用带有Model名称的*运算符,如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');
        }

您只需要在find函数中传递一个字段数组。您必须设置好模型关系才能使用以下技巧。

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"););