如何使用CodeIgniter DB Forge生成时间戳和日期时间的createdAt和updatedAt?

时间:2019-01-23 03:21:54

标签: php mysql codeigniter-3 data-migration

我想有2个时间戳/日期时间字段, createdAt ,其中仅包含创建记录的时间,但是如果记录上有更新,并且 updatedAt,则不会持续更新,它在创建记录和每次更新记录时生成。还有人说一条记录至少应该只包含一个时间戳字段(对于MySQL)?

这是我的迁移代码:

game <- function(n = 4){
  if (any(ceiling(6 * runif(n)) == 6L)){
    return("Win")
  } else {
    return("Lose")
  }
}

如何使用CodeIgniter DB Forge完成此操作?任何答案都很好。谢谢

1 个答案:

答案 0 :(得分:0)

好吧,这是我的解决方法:

import React, { Component, Fragment } from "react";
import { render } from "react-dom";
import axios from "axios";

class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      data: [],
      loading: false
    };
  }

  componentDidMount() {
    this.setState({
      data: [
        {
          provision_id: "1",
          provision: "Milk",
          category: [{ category_id: "1", category_price: "100 USD" }]
        }
      ]
    });
  }

  // Get New Price of Milk on button click

  handleNewPrice(pro_id) {
    alert(pro_id);
    const product = {
      pro_id: pro_id
    };
    const { data } = this.state;
    axios
      .post("http://localhost/apidb_react/price.json", { product })
      .then(response => {
        this.setState({
          data: data.map(store => {
            if (store.provision_id !== pro_id) {
              return store;
            } else {
              return {
                ...store,
                category_new: [
                  ...store.category_new, // that's fix for #1
                  {
                    /**
                     * problem #1 in line below, you're trying to spread array data 'store.category_new' to object
                     */
                    // ...store.category_new,
                    /**
                     * problem #2 I would recommend to make some validation 'response && response.data && response.data[0] && response.data[0].category_price'
                     */
                    category_price: response.data[0].category_price,
                    category_id: response.data[0].category_id
                  }
                ]
              }
            }
          })
        })
        console.log(response.data[0].category_price);
      })
      .catch(error => {
        console.log(error);
      });
  }

  render() {
    const { data } = this.state;
    return (
      <span>
        <label>
          <ul>
            {
              data.map(store => (
                <div key={store.provision_id}>
                  <div>
                    <h1>Provision Store</h1> <br />
                    <b> Product: </b>
                    {store.provision}
                  </div>

                  {
                    store.category_new &&
                    store.category_new.map(cat1 => {
                      return (
                        <div key={cat1.category_id}>
                          <div>
                            <b>New Prices:</b> {cat1.category_price} -----{" "}
                            {cat1.category_id}
                          </div>
                        </div>
                      );
                    })
                  }

                  {
                    store.category &&
                    store.category.map(cat => {
                      return (
                        <div key={cat.category_id}>
                          <div>
                            <b>Prices:</b> {cat.category_price}
                            <br />
                            <input
                              type="button"
                              value="Get New Prices"
                              onClick={() =>
                                this.handleNewPrice(cat.category_id)
                              }
                            />
                          </div>
                        </div>
                      );
                    })
                  }
                </div>
              ))
            }
          </ul>
        </label>
      </span>
    );
  }
}