gpt4 book ai didi

arrays - Cakephp 在查找操作中使用数组变量

转载 作者:行者123 更新时间:2023-12-02 07:02:34 24 4
gpt4 key购买 nike

我正在创建一个存储在蛋糕 session 变量中的候选名单

$this->Session->read('Item.shorlist');

这包含逗号分隔的 ID 列表,例如。 1,2,3,4,5

$shortlist =  $this->Session->read('Item.shorlist');

我想在查找条件中使用此变量中的逗号分隔 ID 来执行查找操作,例如:

$shortlist = $this->Item->find('all', array('conditions' => array('Item.id' => array($shortlist))));

但是,这仅返回 1 组数据。如果我手动放入数组,例如:

$shortlist = $this->Item->find('all', array('conditions' => array('Item.id' => array(1,2,3,4,5))));

我收到了全部 5 条记录的返回。

有什么想法可以解决这个问题而不需要对每个 id 执行多次查找吗?如果我查看 SQL 转储,我可以看到 WHERE Item.id = ('1,2,3,4,5') 使用 $shortlist 变量,而如果我手动将其输入为逗号分隔的整数,例如: WHERE Item.id IN (1, 2, 3, 4) 然后sql查询就会按照我希望的方式工作。所以我想我的问题是如何将变量中的逗号分隔字符串转换为逗号分隔整数,以便 SQL 不会抛出错误?

最佳答案

当您使用此行检索 session 值时 $shortlist = $this->Session->read('Item.shorlist'); 将是一个字符串,请确保它是一个大批。

使用explode $short_list_array =explode(',', $shortlist);函数将其转换为数组并使用

$shortlist = $this->Item->find('all', array('conditions' => array('Item.id' => $short_list_array)));

关于arrays - Cakephp 在查找操作中使用数组变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7154830/

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