public function actionGenerate() { $names = file(getcwd() . "/protected/tests/names.txt"); $streets = file(getcwd() . "/protected/tests/streets.txt"); $name_arr = array(); $street_arr = array(); foreach ($names as $line) { $name = explode(" ", $line); $name_arr[] = trim($name[0]); } foreach ($streets as $line) { $street = explode(" ", $line); if (count($street) == 3) { $name1 = trim($street[0]); $name2 = trim($street[1]); $name3 = trim($street[2]); $street_arr[] = $name1 . " " . $name2 . " " . $name3 . ", Toronto"; } } $limit = 1000000; for ($i = 0; $i < $limit; $i++) { set_time_limit(0); // resetting the MySQL timeout echo "Creating an invoice #{$i} <br/>"; $invoice = new Invoice(); $invoice->name = $name_arr[rand(0, count($name_arr) - 1)]; $invoice->address = rand(1, 5000) . " " . $street_arr[rand(0, count($street_arr) - 1)]; $y = 2012; //rand(2006,2012); if ($y < 2012) { $m = rand(1, 12); } else { $m = rand(1, 11); } $m = 11; $d = rand(1, 2); $invoice->date = date($y . '-' . $m . '-' . $d); $invoice->delivery_date = date($y . '-' . $m . '-' . $d); $invoice->phone1 = "647" . rand(1, 9) . rand(1, 9) . rand(1, 9) . rand(1, 9) . rand(1, 9) . rand(1, 9) . rand(1, 9); $invoice->delivery_date = date($y . '-' . $m . '-' . $d); $invoice->delivery_cost = rand(10, 50); if ($invoice->save()) { $number_of_items = rand(3, 9); for ($j = 0; $j < $number_of_items; $j++) { $entry = new Entry(); $entry->invoice = $invoice->id; $entry->inventory = rand(3, 2709); $inventory = Inventory::model()->findByPk($entry->inventory); if ($inventory) { $entry->item = $inventory->name . " #" . $inventory->model; $entry->quantity = rand(1, $inventory->quantity); $entry->price = round($inventory->in * 1.3); $entry->amount = round($entry->price * 0.9 * $entry->quantity); if ($entry->save()) { echo "--Entry Created! <br/>"; $inventory->quantity = $inventory->quantity - $entry->quantity; if ($inventory->save()) { echo "---Inventory Updated! <br/>"; } else { $entry->delete(); echo "***Inventory Error! Failed to update <br/>"; } } else { echo "***Entry Error! <br/>"; print_r($entry->getErrors()); echo "<br/>"; } } else { echo "*Inventory Error! (invalid randomed inventory id) <br/>"; } } if ($invoice->Amount > 0) { echo "Invoice Created! id=>" . $invoice->id . "<br/>"; } else { echo "Invoice has no entries! Deleting id=>" . $invoice->id . "<br/>"; $invoice->delete(); } } else { echo "Invoice Error! <br/>"; print_r($invoice->getError()); echo "<br/>"; } echo "<hr/>"; } }
public function actionEntry() { //关闭页面布局 $this->layout = false; //创建表单模型 $entry = new Entry(); //如果是Post请求提交 if (Yii::$app->request->getIsPost()) { //表单模型设置属性为post值 $entry->setAttributes(Yii::$app->request->post()); //表单模型数据验证 if ($entry->validate()) { //正确即保存数据 $result = $entry->save(); //返回保存后的信息 if ($result) { var_dump($entry->attributes); } else { var_dump($entry->getErrors()); } } else { //返回错误提示 var_dump($entry->getErrors()); } } else { //如果不是Post请求,正常显示模板 return $this->render('entry', ['model' => $entry]); } }