gpt4 book ai didi

cakephp - 在 cakephp 中使用 read() 来检索包含数据数组的行

转载 作者:行者123 更新时间:2023-12-01 11:54:44 25 4
gpt4 key购买 nike

我想知道是否可以使用类似于以下的方法从数据库中检索一行:

    if (!empty($this->params['form'])) {
$place = array();
$place['city'] = $this->params['form']['city'];
$place['area'] = $this->params['form']['state'];
$place['country'] = $this->params['form']['country'];
$place['iso'] = $this->params['form']['iso'];

$this->Place->set($place);
$place_found = $this->Place->read();
}

有什么方法可以使用数组在 Place 模型中预设数据,然后使用 Place 读取。我正在寻找像往常一样简单的东西:

$this->Place->id = 7;
$place_found = $this->Place->Read();

我也试过这样做:

$this->Place->city = blah;
$this->Place->area = foo; etc....

$place_found = $this->Place->read();

但是,这也行不通。

最佳答案

你没用过find()吗? ?! read()只获取带有传递的 ID 的行。

$place_found = $this->Place->find('first', array(
'conditions' => array(
'Place.city' => $city,
'Place.area' => $area
// etc
)
));

如果您需要手动构建条件,您可以创建一个条件数组来传递,如下所示:

$placeConditions = array();
$placeConditions['city'] = $city;
if($area) {
$placeConditions['area'] = $area;
}

$places = $this->Place->find('first', array('conditions' => $placeConditions));

我建议您阅读我链接的页面,您很快就会发现没有理由使用 read() 方法。

关于cakephp - 在 cakephp 中使用 read() 来检索包含数据数组的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8324844/

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