gpt4 book ai didi

php - CakePHP 3 级深度模型关联

转载 作者:行者123 更新时间:2023-12-02 06:44:10 25 4
gpt4 key购买 nike

我是 CakePHP 的新手,我对文档做得很好,但是几周来我一直在努力寻找解决这个问题的方法,但我似乎没有找到解决方案,我确信它是很简单,甚至可以自动实现,但我只是不知道如何找到它(也许我不知道这类事情的行话)

我的模型结构是这样的:

<?php
class Trip extends AppModel {
var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
),
'Start' => array(
'className' => 'Place',
'foreignKey' => 'start_id'
),
'End' => array(
'className' => 'Place',
'foreignKey' => 'end_id'
),
'Transport' => array(
'className' => 'Transport',
'foreignKey' => 'transport_id'
)
);

}
?>

<?php
class Place extends AppModel {

var $belongsTo = array(
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
),
'Country' => array(
'className' => 'Country',
'foreignKey' => 'country_id'
),
'State' => array(
'className' => 'State',
'foreignKey' => 'state_id'
),
'City' => array(
'className' => 'City',
'foreignKey' => 'city_id'
)
);
var $hasMany = array(
'PlaceStart' => array(
'className' => 'trip',
'foreignKey' => 'start_id',
'dependent' => false
),
'PlaceEnd' => array(
'className' => 'trip',
'foreignKey' => 'end_id',
'dependent' => false
)
);

}
?>

<?php
class State extends AppModel {

var $belongsTo = array(
'Country' => array(
'className' => 'Country',
'foreignKey' => 'country_id',
'conditions' => '',
'fields' => '',
'order' => ''
)
);

var $hasMany = array(
'City' => array(
'className' => 'City',
'foreignKey' => 'city_id',
'dependent' => false
)
);

}
?>

...等等用户、城市、国家和交通模型。

我想要实现的是在搜索Trip时获取整棵树的所有信息。

<?php
class TripController extends AppController {
function index() {
debug($this->Trip->find('first'));
}
}

输出

Array
(
[Trip] => Array
(
[id] => 6
[created] => 2010-05-04 00:23:59
[user_id] => 4
[start_id] => 2
[end_id] => 1
[title] => My trip
[transport_id] => 1
)

[User] => Array
(
[id] => 4
[name] => John Doe
[email] => john.doe@mailinator.com
)

[Start] => Array
(
[id] => 2
[user_id] => 4
[country_id] => 1
[state_id] => 1
[city_id] => 1
[direccion] => Lincoln Street
)

[End] => Array
(
[id] => 1
[user_id] => 4
[country_id] => 1
[state_id] => 1
[city_id] => 4
[address] => Fifth Avenue
)

[Transport] => Array
(
[id] => 1
[name] => car
)
)

问题如下:如何在一次查询中获取树下的所有信息?

我想要一个类似的东西

Array
(
[Trip] => Array
(
[id] => 6
[created] => 2010-05-04 00:23:59
[User] => Array
(
[id] => 4
[name] => John Doe
[email] => john.doe@mailinator.com
)
[Start] => Array
(
[id] => 2
[user_id] => 4
[Country] => Array
(
[id] => 1
[name] = Spain
)
[State] => Array
(
[id] => 1
[name] = Barcelona
)
[City] => Array
(
[id] => 1
[name] = La Floresta
)
[address] => Lincoln Street
)
[End] => (same as Start)
[title] => My trip
[Transport] => Array
(
[id] => 1
[name] => car
)
)
)

CakePHP 可以创建这种数据吗?不仅适用于 $this->Model->find(),还适用于 $this->paginate(),例如:

    // filter by start
if(isset($this->passedArgs['start'])) {
//debug('isset '.$this->passedArgs['start']);
$start = $this->passedArgs['start'];
$this->paginate['conditions'][] = array(
'OR' => array(
'Start.address LIKE' => "%$start%",
'Start.State.name LIKE' => "%$start%",
'Start.City.name LIKE' => "%$start%",
'Start.Country.name LIKE' => "%$start%"
)
);
$this->data['Search']['start'] = $start;
}

这似乎是一个粗略的问题,但我确信这已被广泛完成并记录在案,我非常感谢任何帮助。

最佳答案

您似乎一切都设置好了。您所要做的就是将递归设置为 2,这应该为您的数据提供额外的级别(假设您已正确设置所有关系)

var $recursive =2;

关于php - CakePHP 3 级深度模型关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2966145/

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