Phalcon中Model的关系和PHQL的Join的释疑
1.Model中返回的是当前的Model集
2.PHQL中返回的是查询集
前者支持魔术方法get/count加关联Model名,用可选参数来进行二次筛选;后者采用类似SQL的方法来关联表,这样查询集
里面就利用驼峰规则就可以访问所有的字段:
[php]
$phql = "SELECT Cars.*, Brands.* FROM Cars, Brands WHERE Brands.id = Cars.brands_id"
$rows = $manager->executeQuery($phql);
foreach ($rows as $row) {
echo "Car: ", $row->cars->name, "\n";
echo "Brand: ", $row->brands->name, "\n";
}
[/php]
还可以访问指定字段:
[php]
$phql = "SELECT Cars.name AS car_name, Brands.name AS brand_name WHERE Brands.id = Cars.brands_id";
$rows = $manager->executeQuery($phql);
foreach ($rows as $row) {
echo $row->car_name, "\n";
echo $row->brand_name, "\n";
}
[/php]
当然可以加别名访问:
[php]
$phql = "SELECT c.*, b.* FROM Cars c, Brands b WHERE b.id = c.brands_id";
$rows = $manager->executeQuery($phql);
foreach ($rows as $row) {
echo "Car: ", $row->c->name, "\n";
echo "Brand: ", $row->b->name, "\n";
}
[/php]
作者:z1988
链接:https://www.z1988.com/783.html
文章版权归作者所有,未经允许请勿转载。