提交 4648e8c7 编写于 作者: weixin_47267244's avatar weixin_47267244

支持为查询出来的关联模型指定字段

上级 59077366
...@@ -28,6 +28,10 @@ ...@@ -28,6 +28,10 @@
`@OneToMany(model="模型类名", order="排序规则字符串,例:age desc, id desc")` `@OneToMany(model="模型类名", order="排序规则字符串,例:age desc, id desc")`
为查询出来的模型指定字段:
`@OneToMany(model="模型类名", order="排序规则字符串,例:age desc, id desc", fields={"字段1", "字段2"})`
### @ManyToMany ### @ManyToMany
多对多关系声明 多对多关系声明
...@@ -36,6 +40,10 @@ ...@@ -36,6 +40,10 @@
`@ManyToMany(model="关联模型", middle="中间模型类名", rightMany="属性名,赋值为关联的模型对象列表", order="排序规则字符串,例:age desc, id desc")` `@ManyToMany(model="关联模型", middle="中间模型类名", rightMany="属性名,赋值为关联的模型对象列表", order="排序规则字符串,例:age desc, id desc")`
为查询出来的模型指定字段:
`@ManyToMany(model="关联模型", middle="中间模型类名", rightMany="属性名,赋值为关联的模型对象列表", order="排序规则字符串,例:age desc, id desc", fields={"字段1", "字段2"})`
### @PolymorphicOneToOne ### @PolymorphicOneToOne
多态一对一关系声明 多态一对一关系声明
...@@ -44,6 +52,10 @@ ...@@ -44,6 +52,10 @@
`@PolymorphicOneToOne(model="关联模型", type="多态类型字段名", typeValue="多态类型值")` `@PolymorphicOneToOne(model="关联模型", type="多态类型字段名", typeValue="多态类型值")`
为查询出来的模型指定字段:
`@PolymorphicOneToOne(model="关联模型", type="多态类型字段名", typeValue="多态类型值", fields={"字段1", "字段2"})`
### @PolymorphicOneToMany ### @PolymorphicOneToMany
多态一对多关系声明 多态一对多关系声明
...@@ -52,6 +64,10 @@ ...@@ -52,6 +64,10 @@
`@PolymorphicOneToMany(model="关联模型", type="多态类型字段名", typeValue="多态类型值", order="排序规则字符串,例:age desc, id desc")` `@PolymorphicOneToMany(model="关联模型", type="多态类型字段名", typeValue="多态类型值", order="排序规则字符串,例:age desc, id desc")`
为查询出来的模型指定字段:
`@PolymorphicOneToMany(model="关联模型", type="多态类型字段名", typeValue="多态类型值", order="排序规则字符串,例:age desc, id desc", fields={"字段1", "字段2"})`
### @PolymorphicManyToMany ### @PolymorphicManyToMany
多态多对多关系声明 多态多对多关系声明
...@@ -60,6 +76,10 @@ ...@@ -60,6 +76,10 @@
`@PolymorphicManyToMany(model="关联模型", middle="中间模型类名", rightMany="属性名,赋值为关联的模型对象列表", type="多态类型字段名", typeValue="多态类型值", order="排序规则字符串,例:age desc, id desc")` `@PolymorphicManyToMany(model="关联模型", middle="中间模型类名", rightMany="属性名,赋值为关联的模型对象列表", type="多态类型字段名", typeValue="多态类型值", order="排序规则字符串,例:age desc, id desc")`
为查询出来的模型指定字段:
`@PolymorphicManyToMany(model="关联模型", middle="中间模型类名", rightMany="属性名,赋值为关联的模型对象列表", type="多态类型字段名", typeValue="多态类型值", order="排序规则字符串,例:age desc, id desc", fields={"字段1", "字段2"})`
### @PolymorphicToOne ### @PolymorphicToOne
用于多态一对一、一对多关联被关联的模型中使用,查询对应的左侧模型 用于多态一对一、一对多关联被关联的模型中使用,查询对应的左侧模型
...@@ -70,6 +90,10 @@ ...@@ -70,6 +90,10 @@
`@PolymorphicToOne(model="关联模型", modelField="关联的模型用于关联的字段", field="当前模型用于关联的字段", type="多态类型字段名", typeValue="多态类型值")` `@PolymorphicToOne(model="关联模型", modelField="关联的模型用于关联的字段", field="当前模型用于关联的字段", type="多态类型字段名", typeValue="多态类型值")`
为查询出来的模型指定字段:
`@PolymorphicToOne(model="关联模型", modelField="关联的模型用于关联的字段", field="当前模型用于关联的字段", type="多态类型字段名", typeValue="多态类型值", fields={"字段1", "字段2"})`
### @PolymorphicToMany ### @PolymorphicToMany
用于多态多对多关联被关联的模型中使用,查询对应的左侧模型列表 用于多态多对多关联被关联的模型中使用,查询对应的左侧模型列表
...@@ -82,6 +106,10 @@ ...@@ -82,6 +106,10 @@
`@PolymorphicToMany(model="关联模型", modelField="关联的模型用于关联的字段", field="当前模型用于关联的字段", type="多态类型字段名", typeValue="多态类型值", order="排序规则字符串,例:age desc, id desc")` `@PolymorphicToMany(model="关联模型", modelField="关联的模型用于关联的字段", field="当前模型用于关联的字段", type="多态类型字段名", typeValue="多态类型值", order="排序规则字符串,例:age desc, id desc")`
为查询出来的模型指定字段:
`@PolymorphicToMany(model="关联模型", modelField="关联的模型用于关联的字段", field="当前模型用于关联的字段", type="多态类型字段名", typeValue="多态类型值", order="排序规则字符串,例:age desc, id desc", fields={"字段1", "字段2")`
### @JoinFrom ### @JoinFrom
一对一、一对多声明中,指定左表与右表关联用的字段名 一对一、一对多声明中,指定左表与右表关联用的字段名
......
...@@ -46,4 +46,11 @@ class ManyToMany extends RelationBase ...@@ -46,4 +46,11 @@ class ManyToMany extends RelationBase
* @var string * @var string
*/ */
public $order; public $order;
/**
* 为查询出来的模型指定字段.
*
* @var string[]|null
*/
public $fields = null;
} }
...@@ -37,4 +37,11 @@ class OneToMany extends RelationBase ...@@ -37,4 +37,11 @@ class OneToMany extends RelationBase
* @var string * @var string
*/ */
public $order; public $order;
/**
* 为查询出来的模型指定字段.
*
* @var string[]|null
*/
public $fields = null;
} }
...@@ -28,4 +28,11 @@ class OneToOne extends RelationBase ...@@ -28,4 +28,11 @@ class OneToOne extends RelationBase
* @var string * @var string
*/ */
public $model; public $model;
/**
* 为查询出来的模型指定字段.
*
* @var string[]|null
*/
public $fields = null;
} }
...@@ -60,4 +60,11 @@ class PolymorphicManyToMany extends RelationBase ...@@ -60,4 +60,11 @@ class PolymorphicManyToMany extends RelationBase
* @var string * @var string
*/ */
public $order; public $order;
/**
* 为查询出来的模型指定字段.
*
* @var string[]|null
*/
public $fields = null;
} }
...@@ -51,4 +51,11 @@ class PolymorphicOneToMany extends RelationBase ...@@ -51,4 +51,11 @@ class PolymorphicOneToMany extends RelationBase
* @var string * @var string
*/ */
public $order; public $order;
/**
* 为查询出来的模型指定字段.
*
* @var string[]|null
*/
public $fields = null;
} }
...@@ -42,4 +42,11 @@ class PolymorphicOneToOne extends RelationBase ...@@ -42,4 +42,11 @@ class PolymorphicOneToOne extends RelationBase
* @var mixed * @var mixed
*/ */
public $typeValue; public $typeValue;
/**
* 为查询出来的模型指定字段.
*
* @var string[]|null
*/
public $fields = null;
} }
...@@ -74,4 +74,11 @@ class PolymorphicToMany extends RelationBase ...@@ -74,4 +74,11 @@ class PolymorphicToMany extends RelationBase
* @var string * @var string
*/ */
public $order; public $order;
/**
* 为查询出来的模型指定字段.
*
* @var string[]|null
*/
public $fields = null;
} }
...@@ -56,4 +56,11 @@ class PolymorphicToOne extends RelationBase ...@@ -56,4 +56,11 @@ class PolymorphicToOne extends RelationBase
* @var mixed * @var mixed
*/ */
public $typeValue; public $typeValue;
/**
* 为查询出来的模型指定字段.
*
* @var string[]|null
*/
public $fields = null;
} }
...@@ -5,6 +5,7 @@ namespace Imi\Model\Relation; ...@@ -5,6 +5,7 @@ namespace Imi\Model\Relation;
use Imi\Bean\Annotation\AnnotationManager; use Imi\Bean\Annotation\AnnotationManager;
use Imi\Bean\BeanFactory; use Imi\Bean\BeanFactory;
use Imi\Db\Db; use Imi\Db\Db;
use Imi\Db\Query\Interfaces\IQuery;
use Imi\Event\Event; use Imi\Event\Event;
use Imi\Model\Annotation\Relation\AutoSelect; use Imi\Model\Annotation\Relation\AutoSelect;
use Imi\Model\Annotation\Relation\RelationBase; use Imi\Model\Annotation\Relation\RelationBase;
...@@ -116,7 +117,12 @@ abstract class Query ...@@ -116,7 +117,12 @@ abstract class Query
} }
else else
{ {
/** @var IQuery $query */
$query = $modelClass::query()->where($rightField, '=', $model->$leftField); $query = $modelClass::query()->where($rightField, '=', $model->$leftField);
if ($annotation->fields)
{
$query->field(...$annotation->fields);
}
Event::trigger($eventName . '.BEFORE', [ Event::trigger($eventName . '.BEFORE', [
'model' => $model, 'model' => $model,
'propertyName' => $propertyName, 'propertyName' => $propertyName,
...@@ -170,7 +176,12 @@ abstract class Query ...@@ -170,7 +176,12 @@ abstract class Query
$model->$propertyName = new ArrayList($modelClass); $model->$propertyName = new ArrayList($modelClass);
if (null !== $model->$leftField) if (null !== $model->$leftField)
{ {
/** @var IQuery $query */
$query = $modelClass::query()->where($rightField, '=', $model->$leftField); $query = $modelClass::query()->where($rightField, '=', $model->$leftField);
if ($annotation->fields)
{
$query->field(...$annotation->fields);
}
if ($annotation->order) if ($annotation->order)
{ {
$query->orderRaw($annotation->order); $query->orderRaw($annotation->order);
...@@ -292,7 +303,12 @@ abstract class Query ...@@ -292,7 +303,12 @@ abstract class Query
} }
else else
{ {
/** @var IQuery $query */
$query = $modelClass::query()->where($annotation->type, '=', $annotation->typeValue)->where($rightField, '=', $model->$leftField); $query = $modelClass::query()->where($annotation->type, '=', $annotation->typeValue)->where($rightField, '=', $model->$leftField);
if ($annotation->fields)
{
$query->field(...$annotation->fields);
}
Event::trigger($eventName . '.BEFORE', [ Event::trigger($eventName . '.BEFORE', [
'model' => $model, 'model' => $model,
'propertyName' => $propertyName, 'propertyName' => $propertyName,
...@@ -346,7 +362,12 @@ abstract class Query ...@@ -346,7 +362,12 @@ abstract class Query
$model->$propertyName = $modelPropery = new ArrayList($modelClass); $model->$propertyName = $modelPropery = new ArrayList($modelClass);
if (null !== $model->$leftField) if (null !== $model->$leftField)
{ {
/** @var IQuery $query */
$query = $modelClass::query()->where($annotation->type, '=', $annotation->typeValue)->where($rightField, '=', $model->$leftField); $query = $modelClass::query()->where($annotation->type, '=', $annotation->typeValue)->where($rightField, '=', $model->$leftField);
if ($annotation->fields)
{
$query->field(...$annotation->fields);
}
if ($annotation->order) if ($annotation->order)
{ {
$query->orderRaw($annotation->order); $query->orderRaw($annotation->order);
...@@ -405,7 +426,12 @@ abstract class Query ...@@ -405,7 +426,12 @@ abstract class Query
} }
else else
{ {
/** @var IQuery $query */
$query = $modelClass::query()->where($leftField, '=', $model->$rightField); $query = $modelClass::query()->where($leftField, '=', $model->$rightField);
if ($annotationItem->fields)
{
$query->field(...$annotationItem->fields);
}
Event::trigger($eventName . '.BEFORE', [ Event::trigger($eventName . '.BEFORE', [
'model' => $model, 'model' => $model,
'propertyName' => $propertyName, 'propertyName' => $propertyName,
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册