I am using Codeigniter as my PHP framework, and I keep getting this error when I submit my data to my database.
I am already using set in the controller. I am not sure why I have this error:
You must use the “set” method to update an entry
My model:
public function update_reserve($data,$book_id)
{
$this->db->where('book_id',$book_id);
$this->db->update('books',$data);
return TRUE;
}
My controller:
public function out()
{
$book_id = $this->input->post('book_id');
if(isset($_POST['btn_reserve'])) {
$data = array(
'pickup' =>$this->input->post('pickup'),
'return' =>$this->input->post('return'),);
if(!empty($data)) {
$this->user_model->update_reserve($book_id,$data);
}
}
}
In model, You can use "set" method:
public function update_reserve($data,$book_id)
{
$this->db->where('book_id',$book_id);
$this->db->set($data);
$this->db->update('books');
return TRUE;
}
I think you should call your method like this
$this->user_model->update_reserve($data,$book_id);
to see result
Related
Please, I need help, I know that this is basic oop but I'm still learning...
public function confirm()
{
$q = Input::get('track');
$confirm = Tracking::where(function ($query) use ($q) {
$query->where('tracking_no', 'like', $q );
})->first();
if ($confirm) {
return Redirect::to('progress');
} else if(!$confirm){
return Redirect::to('invalid-tracking-number');
}
}
public function invalid($q)
{
$data = $this->confirm($q);
return view('frontend.invalid');
}
I'm to pass the input field from confirm() to invalid() but is not working... I need the $q inside my invalid()
so that I pass it to view...
I have fixed it....I reference another method using
if($confirm->isNotEmpty())
{
return (a valid page)
} else {
return $this->invalid();
}
with is, I'm able to return all the input data to the view of invalid
I want to pass data from Controller to model.But I am unable to fetch it at the model side in CI.Kindly help me out.
Here is my controller function:
function show_chronicles($chronicle_num) {
$this->load->database();
//load the model
$this->load->model('Chronicles_model');
//load the method of model
$data['h'] = $this->Chronicles_model->show_seminar();
//return the data in view
$this->load->view('chronicles', $chronicle_num);
}
And here is my model:
public function show_seminar($chronicle_num) {
echo $chronicle_num;
//$this->db->select('*');
//$this->db->where('chronicles_no',$chronicle_num);
//$query1 = $this->db->get('chronicles');
//return $query1;
}
Its because your not passing any value to your model.
CONTROLLER
function show_chronicles($chronicle_num)
{
$this->load->database();
$this->load->model('Chronicles_model');
$data['h']=$this->Chronicles_model->show_seminar($chronicle_num);
$this->load->view('chronicles', $chronicle_num);
}
and you need to return the result() of your query
MODEL
public function show_seminar($chronicle_num = NULL)
{
return $this->db
->get_where('chronicles', array('chronicles_no' => $chronicle_num))
->result();
}
Controller:
function show_chronicles($chronicle_num) {
$this->load->database();
//load the model
$this->load->model('Chronicles_model');
//load the method of model
// pass it to model
$data['chronicle_num'] = $this->Chronicles_model->show_seminar($chronicle_num);
//return the data in view
$this->load->view('chronicles', $data);
}
And here is my model:
public function show_seminar($chronicle_num) {
return $chronicle_num;
//$this->db->select('*');
//$this->db->where('chronicles_no',$chronicle_num);
//$query1 = $this->db->get('chronicles');
//return $query1;
}
You just forgot to pass $chronicle_num as a parameter when you called $this->Chronicles_model->show_seminar();.
So the right way is $this->Chronicles_model->show_seminar($chronicle_num);
You can add as many parameters as you wish to the functions. Example:
public function show_seminar($chronicle_num, $param2, $param3) {
//Login here
}
Just remember to pass the parameters every time you call the function.
I have a function in a controller that when executed, check for existence of a record in a table and returns that record. This function is used as validation of a model inside the same controller. For example:
public function somethingValidate() {
$someVariable = SomeTable::find()->where(['some_id' => $someVariable])->one();
if ($model) {
return $model;
} else {
return false;
Here is the validation part of controller:
public function actionSave() {
$model = new TestModel() {
if ($this->somethingValidate()) {
try {
--- REST OF THE CODE ---
How can i now, pass $someVariable variable into a TestModel and manipulate the data either on save or beforeSave.
Any assistance is greatly appreciated.
For example, an Investment amount should be more or equal with the Proposal's min_investment value. Below is the beforeValidate() function in Investment model.
public function beforeValidate()
{
parent::beforeValidate();
$proposal = Proposal::findOne($this->proposal_id);
if ($this->amount < $proposal->min_investment)
{
$this->addError('amount', Yii::t('app', 'Minimum amount should be ' . $proposal->min_investment));
return FALSE;
}
return TRUE;
}
Is that what you're trying to achieve?
I have following functions in Controller.
public function UpdateCountry(\App\Http\Requests\CountryRequest $request) {
$this->SaveChanges($request);
}
private function SaveChanges($request) {
if($request['CountryID'] == 0) {
$Country = new \App\Models\CountryModel();
}
else {
$Country = \App\Models\CountryModel
::where('CountryID', $request['CountryID'])->first();
}
$Country->Country = $request['Country'];
$Country->CountryCode = $request['CountryCode'];
$Country->save();
return redirect()->route('AllCountries');
}
public function AllCountries() {
$Countries = \App\Models\CountryModel::all();
return view('Country.List', array('Countries' => $Countries));
}
Issue is in below line: When I call function SaveChanges, I am not able to see List of countries page and when I write the code directly in UpdateCountry function, it redirect route successfully.
return redirect()->route('AllCountries');
Anybody faced this issue before ?
Your route is being handled by the UpdateCountry function. Laravel will take action based on the returned value from this function. However, you're not returning anything from this function.
You call SaveChanges, which returns a Redirect object, but then you don't return anything from your UpdateCountry function. You need that Redirect object from the UpdateCountry function in order for Laravel to actually return the redirect to the client.
Update your UpdateCountry function to this:
// added the return statement, so this function will return the redirect
// to the route handler.
public function UpdateCountry(\App\Http\Requests\CountryRequest $request) {
return $this->SaveChanges($request);
}
Maybe you missed a return in $this->SaveChanges($request). It has to be:
public function UpdateCountry(\App\Http\Requests\CountryRequest $request) {
return $this->SaveChanges($request);
}
I hope it works fine for you.
i just try to save input data and in line of save() method show me error , this is my controller register method :
public function register() {
if ($this->Helloworlds->save($this->request->data)) {
$id = $this->Helloworld->id;
$this->request->data['Helloworld'] = array_merge($this->request->data['Helloworld'], array('id' => $id));
$this->Auth->login($this->request->data['Helloworld']);
return $this->redirect('/helloworld/index');
}
else
{
$this->Session->setFlash(__('Sorry no data has saved '));
}
}
and the error :
Error: Call to a member function isNew() on a non-object File
C:\wamp\www\vendor\cakephp\cakephp\src\ORM\Table.php Line: 1092
i would mention this i'm sure about my model name'helloworlds'
any idea?
at least i find out answer , the true code is :
public function register() {
if ($this->request->is('post')) {
$Helloworld = TableRegistry::get('helloworlds'); $Helloworlds = $Helloworld->newEntity($this->request->data); if ($Helloworld->save($Helloworlds)) {
$id = $this->Helloworld->id;
$this->request->data['Helloworld'] = array_merge($this->request->data['Helloworld'], array('id' => $id));
$this->Auth->login($this->request->data['Helloworld']);
return $this->redirect('/helloworld/index');
} else { $this->Session->setFlash(__('Sorry no data has saved ')); } } }