我有两个表类别和产品。我想获得其id在查询条件数组中传递的产品的详细信息。问题是当我将条件作为数组传递时,它返回
无法将值转换为整数
其次,如果我通过只传递一个id来调试它,它返回类别记录,但它应该返回产品记录。请帮助解决我的问题。
我的代码如下:
{key: [list(t) for t in value] for key, value in a.items()}
模型 - ProductsTable.php
public function display()
{
$ids = array(1,2,3,4,5);
$c_List = $this->Products->Categories->find('all',array('conditions'=>array('Categories.category_id'=>$ids)));
$data = $c_List->toArray();
debug($data);
die();
}
Model - CategoriesTable.php
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('products');
$this->setDisplayField('product_id');
$this->setPrimaryKey('product_id');
$this->belongsTo('Products', [
'foreignKey' => 'product_id',
'joinType' => 'INNER'
]);
$this->belongsTo('Categories', [
'foreignKey' => 'category_id',
'joinType' => 'INNER'
]);
}
答案 0 :(得分:1)
当您根据ID数组查找记录时,请在条件子句中使用 IN 。
public function display()
{
$ids = array(1,2,3,4,5);
$c_List = $this->Products->Categories->find('all',array('conditions'=>array('Categories.category_id IN'=>$ids)));
$data = $c_List->hydrate(false)->toArray();
pr($data);
die();
}