通过handle_in在通道中将数据插入数据库

时间:2016-12-13 10:04:23

标签: phoenix-framework channel ecto

我有一张表和订单表。订单和表之间的关系是Order属于表,而表有许多订单。因此,订单表的一列中有table_id。此外,表格还有current_order列,以保持当前表格中的当前order_id

我有一个table:show频道,显示所有表格列表。当用户点击某个表格时,它会推送table:busy并发送已点击表格的table_id

因此,在表格频道中,table:busy有handle_in,我想创建从table_id频道发送table:show的订单。

def handle_in("table:busy", %{"table_id" => table_id}, socket) do
    table = Repo.get(Pos1.Table, table_id)
    changeset = table
        |> assoc(:orders)
        |> Pos1.Order.changeset(:table_id)

    case Repo.insert(changeset) do
        {:ok, order}->
    changesettable = Table.changeset(table, %{current_order: order.id})
    table = Repo.update!(changesettable)
    {:noreply, socket}

    {:error, changeset} ->
        {:reply, {:error, %{error: "Error creating order"}}, socket}

    end

end

这是错误消息:

---[error] GenServer #PID<0.1327.0> terminating
** (UndefinedFunctionError) function Ecto.Query.__changeset__/0 is undefined or private
    (ecto) Ecto.Query.__changeset__()
    (ecto) Ecto.Changeset.do_cast/4
    (pos1) web/channels/table_channel.ex:18: Pos1.TableChannel.handle_in/3
    (phoenix) lib/phoenix/channel/server.ex:225: anonymous fn/4 in Phoenix.Channel.Server.handle_info/2

以上错误显示未定义变更集...如何定义变更集以将数据插入Orders表?

提前致谢..

---编辑1

defmodule Pos1.Order do
 use Pos1.Web, :model

schema "orders" do

field :number_of_customers, :integer
field :completed, :boolean, default: false
field :confirmed, :boolean, default: false
field :service_charge, :boolean, default: true
field :total, :integer, default: 0
field :discount, :float, default: 0.0
field :current_select, :integer, default: 1
field :payed, :float
field :finalized, :boolean, default: false
field :served, :boolean, default: false
field :current_round, :integer, default: 0
field :rounds, :integer, default: 1
field :created_date, Ecto.DateTime
belongs_to :table, Pos1.Table
belongs_to :payment, Pos1.Payment
has_many :order_items, Pos1.OrderItem
belongs_to :staff, Pos1.Staff

timestamps
end

  @required_fields ~w(number_of_customers table_id completed confirmed 
                  total service_charge current_select finalized served
                  rounds current_round)
  @optional_fields ~w(payment_id discount staff_id payed created_date)


 def changeset(model, params \\ :empty) do
   model
   |> cast(params, @required_fields, @optional_fields)
  end
end

1 个答案:

答案 0 :(得分:0)

此错误是由package com.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.model.Sekolah; import com.controller.SekolahServlet; import com.util.DBConnection; public class SekolahDao { private Connection connection; public SekolahDao() throws ClassNotFoundException{ connection = DBConnection.getConnection(); } public void addSekolah(Sekolah sekolahs) { try { PreparedStatement preparedStatement = connection .prepareStatement("insert into sekolah(sekolahid, snama, salamat, stel, semel) values (?, ?, ?, ?, ?)"); //Parameter start with 1 preparedStatement.setString(1, sekolahs.getSekolahid()); preparedStatement.setString(2, sekolahs.getSnama()); preparedStatement.setString(3, sekolahs.getSalamat()); preparedStatement.setInt(4, sekolahs.getStel()); preparedStatement.setString(5, sekolahs.getSemel()); preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } public void deleteSekolah(String sekolahid) { try { PreparedStatement preparedStatement = connection .prepareStatement("delete from sekolah where sekolahid=?"); //Parameters start with 1 preparedStatement.setString(1, sekolahid); preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } public void updateSekolah(Sekolah sekolahs) { try { PreparedStatement preparedStatement = connection .prepareStatement("update sekolah set snama=?, salamat=?, stel=?, semel=? " + "where sekolahid=?"); //Parameter start with 1 preparedStatement.setString(1, sekolahs.getSnama()); preparedStatement.setString(2, sekolahs.getSalamat()); preparedStatement.setInt(3, sekolahs.getStel()); preparedStatement.setString(4, sekolahs.getSemel()); preparedStatement.setString(5, sekolahs.getSekolahid()); preparedStatement.executeUpdate(); } catch (SQLException e) { e.printStackTrace(); } } public List<Sekolah> getAllUsers() { List<Sekolah> sekolah = new ArrayList<Sekolah>(); try { Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery("select * from sekolah"); while (rs.next()) { Sekolah sekolahs = new Sekolah(); sekolahs.setSekolahid(rs.getString("sekolahid")); sekolahs.setSnama(rs.getString("snama")); sekolahs.setSalamat(rs.getString("salamat")); sekolahs.setStel(rs.getInt("stel")); sekolahs.setSemel(rs.getString("semel")); sekolah.add(sekolahs); } } catch (SQLException e) { e.printStackTrace(); } return sekolah; } public Sekolah getUserById (String sekolahid) { Sekolah sekolahs = new Sekolah(); try { PreparedStatement preparedStatement = connection.prepareStatement("select * from sekolah where sekolahid=?"); preparedStatement.setString(1, sekolahid); ResultSet rs = preparedStatement.executeQuery(); while (rs.next()){ sekolahs.setSekolahid(rs.getString("sekolahid")); sekolahs.setSnama(rs.getString("snama")); sekolahs.setSalamat(rs.getString("salamat")); sekolahs.setStel(rs.getInt("stel")); sekolahs.setSemel(rs.getString("semel")); } }catch (SQLException e) { e.printStackTrace(); } return sekolahs; }} @required_fields模型引起的。

我没有通过每个Orders所以它没有创建订单。

我将一些@required_fields移到了@required_fields并且有效。