gpt4 book ai didi

php - cakephp 选定表的字段

转载 作者:行者123 更新时间:2023-11-29 07:42:53 25 4
gpt4 key购买 nike

我在 Controller 中的代码如下所示:

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、产品名称,并且从类别表中只需要 id 和名称,那么我怎样才能实现它。提前致谢

最佳答案

In Cakephp fields is the property where you can define your table fields name, if you want to define all the fields you use * operator with Model name such as Product.*. below is your code please check this

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

关于php - cakephp 选定表的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28562132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com