Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Elixir 在map/2中使用动态值_Elixir_Phoenix Framework_Ecto - Fatal编程技术网

Elixir 在map/2中使用动态值

Elixir 在map/2中使用动态值,elixir,phoenix-framework,ecto,Elixir,Phoenix Framework,Ecto,所以我读了关于这个函数的书,我有一个场景,我必须使用它 查询如下: from p in model, where: p.id == 1, select: map(p, [:id, :inserted_by, customer: [:id, :first_name]]) 因此,不是由插入的id和而是硬编码的id和名字。我想在这样的列表中使用动态值 [:id, :inserted_by, :first_name] select: map(p, [^dynamic_va

所以我读了关于这个函数的书,我有一个场景,我必须使用它

查询如下:

  from p in model,
    where: p.id == 1,
    select: map(p, [:id, :inserted_by, customer: [:id, :first_name]])
因此,不是由插入的
id
而是硬编码的
id
名字
。我想在这样的列表中使用动态值

  [:id, :inserted_by, :first_name]
  select: map(p, [^dynamic_value, customer: ^dynamic_value])
我试图通过在变量中保存列表来使用
^
操作符。但它给出了错误
不能使用外部匹配子句

如何更改动态值的查询?
像这样

  [:id, :inserted_by, :first_name]
  select: map(p, [^dynamic_value, customer: ^dynamic_value])

谢谢。

官方
Ecto
文档通过第二句话逐字回答了这个问题:

也可用于动态选择字段:

fields = [:title, :body]
from p in Post, select: map(p, ^fields)
在你的情况下,这将是:

dynamic_value = [:id, :inserted_by, customer: [:id, :first_name]]

from p in model,
where p.id == 1,
select: map(p, ^dynamic_value)
预先定义
dynamic_value
的问题是:
from
简而言之是一个宏,它内部有一个复杂的逻辑,并且它接受准备好的AST