CakePHP 3.0 dev. Model Save() error? - php

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 ')); } } }

Related

Attempt to Read Property on String Error Codeigniter

I am trying to display data through a query on Codeigniter, but return an error
"ATTEMPT TO READ PROPERTY "JENIS_PRODUK" ON STRING"
Here's my code on controller :
public function minimalist($id = 'minimalist')
{
$data["minimalis"] = $this->welcome_model->getMinimalis($id);
$this->load->view('frontend/minimalis.php', $data);
}
My code on Model :
public function getMinimalis($id)
{
return $this->db->get_where($this->_table, ["jenis_produk" => $id])->row();
}
On view I added foreach to display those data
Can you tell me what's wrong?
Please change this
public function getMinimalis($id)
{
return $this->db->get_where($this->_table, ["jenis_produk" => $id])->row();
}
To
public function getMinimalis($id)
{
return $this->db->get_where($this->_table, ["jenis_produk" => $id])->result();
}

Call to undefined method in Codeigniter 3

So i just want to make simple form to add, update, and delete some data(Name, id, and address). Add and delete works fine for me, but the update doesn't, codeigniter give me a error message :
A PHP Error was encountered
Severity: Error
Message: Call to undefined method Mahasiswa_model::getMahasiswa()
Filename: controllers/Mahasiswa.php
Line Number: 35
Backtrace:
And these is my code which i thought its related with the error.
This one file name is Mahasiswa.php
<?php
if (!defined('BASEPATH')) exit('no direcet script access allowed');
class Mahasiswa extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->database();
$this->load->helper('url');
$this->load->helper('form');
$this->load->model('Mahasiswa_model');
}
public function index() {
$data['mhs'] = $this->Mahasiswa_model->retrieve();
$this->load->view('Mahasiswa_view', $data);
}
public function form_tambah() {
$this->load->view('Tambah_view');
}
public function submit() {
$this->Mahasiswa_model->add($this->input->post('var'));
$data['submitted'] = TRUE;
$this->load->view('Tambah_view', $data);
}
function delete() {
$this->Mahasiswa_model->delete($this->uri->rsegment(3));
$this->index();
}
function form_update() {
$data['mhs'] = $this->Mahasiswa_model->getMahasiswa($this->uri->rsegment(3));
$this->load->view('update_view', $data);
}
function update() {
$this->Mahasiswa_model->update($this->input->post('old_nim'),
$this->input->post('var'));
$this->index();
}
}
?>
And this one is Mahasiswa_model.php
<?php
class Mahasiswa_model extends CI_Model {
function retrieve() {
$query = $this->db->get('mhs');
if($query->result()) {
foreach ($query->result() as $content) {
$data[] = array(
$content->nim,
$content->nama,
$content->alamat
);
}
return $data;
} else {
return FALSE;
}
}
function add($arg) {
$data = array (
'nim' => $arg[0],
'nama'=> $arg[1],
'alamat' => $arg[2],
);
$this->db->insert('mhs', $data);
}
function delete($id) {
$this->db->where('nim', $id);
$this->db->delete('mhs');
}
function update($id, $form) {
$data = array(
'nim' => $form[0],
'nama' =>$form[1],
'alamat' => $form[2],
);
}
}
?>
Maybe someone can help me find where mistake i made? Line 35 not clear enough for me
This line in your controller is the problem:
$this->Mahasiswa_model->getMahasiswa($this->uri->rsegment(3));
You're calling the getMahasiswa method that should be available in your model, but (assuming the code you posted for your model is complete) it's not. Your model only has the retrieve, add, delete and update methods declared.
you either need to create the getMahasiswa method in your model, or you need to call a different method from your controller.
if you want to call a function from same controller then simply write.
$data = $this->getMahasiswa($parameter1);
IMP : function must be present there in the same controller.

Error:You must use the "set" method to update an entry

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

Yii: ClassName and its behaviors do not have a method or closure named "getRandomPlayers"

i am facing a strange problem. i have a function defined in model which i have been using from weeks now, is suddenly giving me error
ClassName and its behaviors do not have a method or closure named "getRandomPlayers"
following is my code:
Model
public function getRandomPlayers($params)
{
$criteria_obj= new CDbCriteria;
$criteria_obj->order="random()";
$criteria_obj->condition="user_id!=".$params['user_id'];
$criteria_obj->limit=7;
$random_users= Users::model()->findAll($criteria_obj);
if(!empty($random_users))
return $random_users;
else
return false;
}
Controller
public function actionInviteRandom()
{
$body_data = $this->getRequest()->getRawBody();
$data_posted = json_decode($body_data);
if(!empty($data_posted->user_id))
{
$check_valid_user=Users::model()->findByPk($data_posted->user_id);
if(empty($check_valid_user))
{
$this->sendResponse(200, array("status_code" => "002",
"status_message" => Yii::t('strings', 'User does not exist'),
)
);
}
else
{
$params=array(
"user_id"=>$data_posted->user_id,
);
$get_users= Users::model()->getRandomPlayers($params);
$count=0;
if(!empty($get_users))
{
die("here");
}
}
}
}
please check. Thanks in advance
add static to functions name :
public static function getRandomPlayers($params){ ...
UPDATE:
then you can use it like :
$get_users= Users::getRandomPlayers($params);

Redirect to the same form with data loaded - Joomla 2.5

I have overridden the SAVE() function in Joomla 2.5
class footballModelPlayer extends JModelAdmin {
public function getTable($type = 'Player', $prefix = 'footballTable', $config = array()) {
return JTable::getInstance($type, $prefix, $config);
}
public function save($data) {
// some code here
$table_one->bind($data);
$table_one->save($data);
return true;
}
Problem is the when I save the new data or edit the existing one, it redirects me to the New Empty From.
If I replace last line by return $data->player_id it redirects me to the same form loaded with data but show error
Save failed with the following error:
Hoe can I save/update the records with displaying Successful Message and staying on the same form with data loaded into it???
You are missing 2 functions in your model:
public function getForm($data = array(), $loadData = true){
// Get the form.
$form = $this->loadForm('com_football.Player', 'Player', array('control' => 'jform', 'load_data' => $loadData));
if (empty($form))
{
return false;
}
return $form;
}
and
protected function loadFormData(){
$data = JFactory::getApplication()->getUserState('com_football.edit.Player.data', array());
if (empty($data))
{
$data = $this->getItem();
}
return $data;
}
Implement them and your data will magically appear. You don't need to override the save method by the way, since you are doing what will be done by default.

Categories