所选表格的cakephp字段

时间:2015-02-17 13:00:37

标签: php mysql cakephp

我在控制器中的代码如下:

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
                (
                )

        )

)

现在从产品表中我只需要选择字段作为id,category_id,productname,并且只需从类别表中选择id和name, 那我怎么能实现它。谢谢你提前

4 个答案:

答案 0 :(得分:2)

  

在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');
        }

答案 1 :(得分:0)

您只需要在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的行为。

答案 2 :(得分:0)

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

    $fields = array("Product.id", "Product.category_name","Product.name", "Category.id", "Category.name");
    $productlist = $this->Product->Category->find('all',$fields);

继续使用常规代码。

答案 3 :(得分:0)

$this->Product->Category->find('all',array(array("Product.id", "Product.category_name","Product.name", "Category.id", "Category.name"););