我在使用ORM将数据保存到Kohana中的数据库时遇到问题。我不是专家,但我做的比这更复杂。我想缺乏基础知识导致了这一点。
我将在下面添加模型,表单和控制器操作代码。如果需要其他任何东西你可以问。 PS。这是我第一次发帖,所以我希望没有规则被打破。
模型:
class Model_Polloptionvote extends ORM {
protected $_table_name = "poll_option_votes";
protected $_primary_key = "id";
protected $_belongs_to = array(
'option' => array(
'model' => 'polloption'
)
);
protected $_table_columns = array(
'id' => array('type' => 'int'),
'option_id' => array('type' => 'int'),
'cookie' => array('type' => 'string')
);
}
表单调用操作:
<form action="<?php echo Route::url("home", array(
"controller" => "polls",
"action" => "vote"
)); ?>" method="POST">
<?php
if(isset($optionmodel)) {
foreach ($optionmodel as $option) { ?>
<label><?php
echo Form::radio('option_id', $option->id, FALSE);
echo $option->name; ?> </label> <?php
echo "</br>";
}
} ?>
</br>
<input type="submit" name="submit" id="submit" value="<?php echo __("Vote"); ?>" />
</form>
民意调查控制行动:
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Polls extends Controller_Base {
public function action_vote() {
$votemodel = ORM::factory("polloptionvote");
$id = $this->request->post("option_id");
if ($id != "") {
$optionmodel = ORM::factory("polloption", $id);
if ($votemodel->loaded()) {
$votemodel->option_id = $id;
$votemodel->cookie = "cookie";
$votemodel->save();
}
$this->request->redirect(Route::url("home", array(
"controller" => "index"
)));
}
视图加载正常,但似乎$votemodel->loaded()
始终为false。如果将该条件更改为1==1
,我会收到错误消息。可能是因为行动不知道在哪里保存。
查看后更新error.log:
错误日志: PHP致命错误:在第144行的/home/mywebsite/public_html/new/application/views/kohana/error.php中调用非对象上的成员函数route()
<?php if (Model_Korisnik::current() !== null) : ?>
<ul>
<li>Korisnik/ca: <span><?php echo Model_Korisnik::current()->username; ?></span></li>
<li class="profile"><a href="<?php echo Route::url("viewUser", array(
"ignore" => Model_Korisnik::current()->username,
"id" => Model_Korisnik::current()->id
)); ?>" title="<?php echo __("Profile"); ?>"><?php echo __("Profile"); ?></a></li>
<li class="settings"><a href="<?php echo Route::url("settingsURL"); ?>" title="<?php echo __("Settings"); ?>"><?php echo __("Settings"); ?></a></li>
<li class="logout"><a href="<?php echo Route::url("auth", array(
"action" => "logout"
)); ?>?redirect=<?php echo rawurlencode(Request::current()->route()->uri(Request::current()->param()) . URL::query()); ?>" title="<?php echo __("Log out"); ?>"><?php echo __("Log out"); ?></a></li>
</ul>
<?php else : ?>
最后一行是146