http://docs.phalconphp.com/en/0.6.0/reference/phql.html
用法示例
为了更好地解释PHQL如何工作,请考虑以下示例。我们有两个型号“汽车”和“品牌”:
<?php
class Cars extends Phalcon\Mvc\Model
{
public $id;
public $name;
public $brand_id;
public $price;
public $year;
public $style;
/**
* This model is mapped to the table sample_cars
*/
public function getSource()
{
return 'sample_cars';
}
/**
* A car only has a Brand, but a Brand have many Cars
*/
public function initialize()
{
$this->belongsTo('brand_id', 'Brands', 'id');
}
}
每辆车都有品牌,所以品牌有很多车型:
<?php
class Brands extends Phalcon\Mvc\Model
{
public $id;
public $name;
/**
* The model Brands is mapped to the "sample_brands" table
*/
public function getSource()
{
return 'sample_brands';
}
/**
* A Brand can have many Cars
*/
public function initialize()
{
$this->hasMany('id', 'Brands', 'brand_id'); // here
}
}
答案 0 :(得分:1)
你是对的。关系应该是hasMany
而不是Cars
Brands
。更正后的示例如下
http://docs.phalconphp.com/en/latest/reference/phql.html
<?php
class Cars extends Phalcon\Mvc\Model
{
public $id;
public $name;
public $brand_id;
public $price;
public $year;
public $style;
/**
* This model is mapped to the table sample_cars
*/
public function getSource()
{
return 'sample_cars';
}
/**
* A car only has a Brand, but a Brand have many Cars
*/
public function initialize()
{
$this->belongsTo('brand_id', 'Brands', 'id');
}
}
每辆车都有品牌,所以品牌有很多车型:
<?php
class Brands extends Phalcon\Mvc\Model
{
public $id;
public $name;
/**
* The model Brands is mapped to the "sample_brands" table
*/
public function getSource()
{
return 'sample_brands';
}
/**
* A Brand can have many Cars
*/
public function initialize()
{
$this->hasMany('id', 'Cars', 'brand_id');
}
}