Yii2 ActiveRecord Setting unknown property: app\models\ - php

This code throw Exception:
public function actionSetdubl() {
$dubls = Yii::$app->request->post('dubl');
$parent = Yii::$app->request->post('parent');
$parentInfo = JurForm::find()->where(['PKJUR' => $parent])->asArray()->all()[0];
for ($i = 0; $i < sizeof($dubls); ++$i) {
$val = $dubls[$i];
$jur = JurForm::findOne($val);
$jur->CFLDUBL = 'Yes';
$jur->DUBLMDM_ID = $parentInfo['MDM_ID'];
$jur->DCHANGEDATE = date('Y-m-d H:i:s');
$jur->save();
}
return Yii::$app->getResponse()->redirect('/index.php?r=jur/analysis');
}
on the line with code $jur = JurForm::findOne($val);.
Exception:
Setting unknown property: app\models\JurForm::PKJUR.
DB: Oracle.

ActiveRecord2 has a hard time automatically mapping table names that start with a capital letter.
So for these columns you have to go into your model class and formally declare them:
public $PKJUR;

maybe better?
$parentInfo = JurForm::find()->where(['PKJUR' => $parent])->asArray()->one()
also i think PKJUR is not defined in DB.

Related

Database Syntax error Codeigniter Grocery Crud with SQLite

I have created one application with Codeigniter Grocery Crud with SQLite. It is working fine at localhost. But when I hosted online by CPanel, it shows syntax error following,
A Database Error Occurred
Error Number: HY000/1
near "SHOW": syntax error
SHOW COLUMNS FROM 'epaper'
Filename: models/Grocery_crud_model.php
Line Number: 436
At File: models/Grocery_crud_model.php 436 line is following
foreach($this->db->query("SHOW COLUMNS FROM '$this->table_name' ")->result() as $db_field_type)
And Full Model is
function get_field_types_basic_table()
{
$db_field_types = array();
foreach($this->db->query("SHOW COLUMNS FROM '$this->table_name' ")->result() as $db_field_type)
{
$type = explode("(",$db_field_type->Type);
$db_type = $type[0];
if(isset($type[1]))
{
if(substr($type[1],-1) == ')')
{
$length = substr($type[1],0,-1);
}
else
{
list($length) = explode(" ",$type[1]);
$length = substr($length,0,-1);
}
}
else
{
$length = '';
}
$db_field_types[$db_field_type->Field]['db_max_length'] = $length;
$db_field_types[$db_field_type->Field]['db_type'] = $db_type;
$db_field_types[$db_field_type->Field]['db_null'] = $db_field_type->Null == 'YES' ? true : false;
$db_field_types[$db_field_type->Field]['db_extra'] = $db_field_type->Extra;
}
$results = $this->db->field_data($this->table_name);
foreach($results as $num => $row)
{
$row = (array)$row;
$results[$num] = (object)( array_merge($row, $db_field_types[$row['name']]) );
}
return $results;
}
For More Reference
https://github.com/scoumbourdis/grocery-crud/blob/master/application/models/Grocery_crud_model.php
Please help me to solve syntax error.

Laravel 5.4 - Update an existing record

I'm struggling to update an existing record through an Eloquent Model in Laravel 5.4
I have for creating a record that works perfectly fine, I took it and modified it to try and update the record:
public function commitEdit ($char_edit_id)
{
$edited_character = \DB::table('characters')->where('char_id', $char_edit_id)->first();
$edited_character->campaign_id = 1;
$edited_character->character_name = request('characterName');
$edited_character->Race = request('race');
$edited_character->Sub_Race = request('subRaceField');
$edited_character->Class = request('class');
$edited_character->Level = request('level');
$edited_character->Strength = request('strength');
$edited_character->Dexterity = request('dexterity');
$edited_character->Constitution = request('constitution');
$edited_character->Intelligence = request('intelligence');
$edited_character->Wisdom = request('wisdom');
$edited_character->Charisma = request('charisma');
$levelVar = request('level');
if ($levelVar >= 4) {
$edited_character->Proficiency = 2;
} else if ($levelVar >= 8) {
$edited_character->Proficiency = 3;
}
$edited_character->Trained_Skills = request('skillsField');
$edited_character->Languages = request('languagesField');
$edited_character->Hit_Die = 1;
$edited_character->max_HP = request('max-hp');
$edited_character->Alignment = request('alignment');
$edited_character->Armor_Class = request('armor-class');
$edited_character->Initiative = request('initiative');
$edited_character->Speed = request('speed');
$edited_character->Background = request('background');
$edited_character->update();
return redirect('./characters');
That gives this error:
Call to undefined method stdClass::update()
I have tried using save() but I get the same error with save() instead of update()
Thanks in advance c:
Documentation
If you just need to retrieve a single row from the database table, you may use the first method. This method will return a single StdClass object:
$edited_character is a stdClass, no Eloquent model.
You can try this code:
public function commitEdit ($char_edit_id)
{
$edited_character = \DB::table('characters')->where('char_id', $char_edit_id)->update([
'campaign_id' => 1,
'character_name' => request('characterName'),
'Race' => request('race'),
//others property
]);
}
Or create Characters model which will be extends from Illuminate\Database\Eloquent\Model and use save method:
public function commitEdit ($char_edit_id)
{
$edited_character = Characters::where('char_id', $char_edit_id)-first();
//your code with properties
$edited_character->save();
}
You can try this way:
public function commitEdit ($char_edit_id)
{
$edited_character = Characters::find($char_edit_id);
$edited_character->character_name = request('characterName');
$edited_character->Race = request('race');
$edited_character->Sub_Race = request('subRaceField');
$edited_character->Class = request('class');
$edited_character->Level = request('level');
$edited_character->Strength = request('strength');
$edited_character->Dexterity = request('dexterity');
$edited_character->Constitution = request('constitution');
$edited_character->Intelligence = request('intelligence');
$edited_character->Wisdom = request('wisdom');
$edited_character->Charisma = request('charisma');
$levelVar = request('level');
if ($levelVar >= 4) {
$edited_character->Proficiency = 2;
} else if ($levelVar >= 8) {
$edited_character->Proficiency = 3;
}
$edited_character->Trained_Skills = request('skillsField');
$edited_character->Languages = request('languagesField');
$edited_character->Hit_Die = 1;
$edited_character->max_HP = request('max-hp');
$edited_character->Alignment = request('alignment');
$edited_character->Armor_Class = request('armor-class');
$edited_character->Initiative = request('initiative');
$edited_character->Speed = request('speed');
$edited_character->Background = request('background');
if($edited_character->save()){
return redirect('./characters');
}else{
// show error message
}
}

Getting Undefined offset: 1 in laravel

I am new to Laravel and was developing small application for my practise. I am doing job search functionality. This error giving me alot trouble and confuses me alot.
public function job_search(Request $request) {
$search_skill_set = $request->job_skills;
$search_results = JobPost::whereRaw('FIND_IN_SET(?, job_skills)', $search_skill_set)
->get()
->toArray();
for ($i = 0; $i < count($search_results); $i++) {
$department_id = (int)$search_results[$i]['department_name'];
$department_name = Department::select('department_name')
->where('id', '=', $department_id)
->get()
->toArray();
// the next statement raises an Undefined:offset 1 error
$search_results[$i]['department_name_info'] = $department_name[$i]['department_name'];
}
var_dump($search_results);
}
I am not getting where am i doing wrong, so any suggestion from given snippet and any modification in the code
change this line:
$search_results[$i]['department_name_info'] = $department_name[$i]['department_name'];
to
$search_results[$i]['department_name_info'] = $department_name[0]['department_name'];
for ($i=0; $i < count($search_results) ; $i++) {
$department_id = (int)$search_results[$i]['department_name'];
//I am getting department id correct here
$department_name = Department::select('department_name')->where('id','=',$department_id)->get()->toArray();
//$depratment_name is also going okay and working
$search_results[$i]['department_name_info'] = $department_name[0]['department_name'];
// This line should have a static index.
}

is there any possible chance when i input data into loop and doesnt entered into database?

sorry for my bad english . i got problem with looping . my friend said when he used my program and he insert 20 data or more into database in one time , sometimes a data doesnt match with data previously entered .
an example : he insert 20 data and he just got 19 but the problem is that happening sometimes.
so im confused now , how can the program error just sometimes ?
here my code :
else if ($mod == 'suratkeluar'){
$batas = $_GET['batas'];
if (isset($_POST['submit'])) {
for ($i = 0; $i < $batas; $i++) {
$idlot = $_POST['hiddenlot'][$i];
$idbenang = $_POST['hiddenbenang'][$i];
$benang = $_POST['jenisbenang'][$i];
$warna = $_POST['warna'][$i];
$lot = $_POST['lot'][$i];
$harga = $_POST['hiddenprice'][$i];
$netto = $_POST['netto'][$i];
$box = $_POST['box'][$i];
$cones = $_POST['cones'][$i];
$ket = $_POST['keterangan'][$i];
$cussplit = explode('_',$_POST['customer']);
$idcus = $cussplit[0];
$cus = $cussplit[1];
$alamatcus = $cussplit[2];
$kota = $cussplit[3];
$POsplit = explode('_',$_POST['nopo']);
$idpo=$POsplit[0];
$nopo=$POsplit[1];
$mobilsplit = explode('_',$_POST['kendaraan']);
$kendaraan = $mobilsplit[0];
$plat = $mobilsplit[1];
$identitas = $_POST['identitas'][$i];
$month = date('n');
$years=date('Y');
if($idlot != 0){
$a=mysql_query("INSERT INTO surat_jalan VALUES ('',now(),'$_POST[nosurat]','$idlot','$benang','$warna','$lot','$harga','$netto','$box','$cones','$idcus','$cus','$alamatcus','$kota','$idpo','$nopo','$ket',1,'$month$years','$identitas','$kendaraan','$plat','$idbenang')");
$aax=mysql_query("SELECT * FROM lot WHERE id_lot='$idlot'");
$xx=mysql_fetch_array($aax);
$net=$xx['netto'] - $netto;
$bbox=$xx['box'] - $box;
$ccones=$xx['cones'] - $cones;
if($net == 0)
{
$a= mysql_query("UPDATE lot SET netto = '$net',
box = '$bbox',
cones = '$ccones',
warning = 1
WHERE id_lot = '$idlot'");
}
else
{
$a=mysql_query("UPDATE lot SET netto = '$net',
box = '$bbox',
cones = '$ccones'
WHERE id_lot = '$idlot'");
}
}
$b = $i + 1;
for ($c = 0; $c < $box; $c++) {
$packinglist = $_POST["packinglist$b"][$c];
$a= mysql_query("INSERT INTO packing_list VALUES('','$packinglist','$surat[id_surat]','$surat[no_surat_jalan]')");
}
}
}
}
How about this? I'll give an example but this could go with (probably) all queries. Let's say, this one (second from the last):
$a = mysql_query("UPDATE lot SET netto = '$net', box = '$bbox', cones = '$ccones' WHERE id_lot = '$idlot'");
To check if it was successful, you can add additional lines:
if ($a) // Success?
print("Row has been updated.<br>");
else // Error
print("An error has occured; row has not been updated. Reason: ".mysql_error()."<br>");
If there are no redirections at the end, etc. and there is at least one error (the last one?), then your friend will see something like this:
An error has occured; row has not been updated. Reason: #1054 - Unknown column 'cones' in 'where clause'
An error has occured; row has not been updated. Reason: #1054 - Unknown column 'box' in 'field list'
With that help, you can see if there is an error in your queries or in application itself (timeout?).
There is also another problem: you're using PHP's mysql_API, which is deprecated. Instead, you should upgrade and use mysqli_API.

Switching databases in code igniter

I have two databases I need to connect to, which I can do in the controllers and libraries I have written. For some odd reason (i'm assuming I'm just missing something simple here) I can't get it to work in the model all of the sudden. I have read the database class in the CI user guide.
I tried making a reference to $pew when loading pew ($this->pew =& $this->load->database('pew', TRUE)) to no avail.
Any thoughts, suggestions? Thanks!
Error
PHP Fatal error: Call to a member function query() on a non-object in
/Sites/CI/nyan/application/models/pewpewmodel.php on line 15
Line 15
$this->pew->query('SELECT * FROM ExtractEvent'); //simplified for testing
database.php:
$active_group = 'nyan';
$active_record = TRUE;
$db['nyan']['hostname'] = 'catcatcat';
$db['nyan']['username'] = 'mew';
$db['nyan']['password'] = 'meow';
$db['nyan']['database'] = 'meow';
$db['nyan']['dbdriver'] = 'mysql';
$db['pew']['hostname'] = 'jujubees';
$db['pew']['username'] = 'qwop';
$db['pew']['password'] = 'qwop';
$db['pew']['database'] = 'nom';
$db['pew']['dbdriver'] = 'mssql';
Model pewpewmodel.php
private $pew;
function __construct()
{
parent::__construct();
$this->pew = $this->load->database('pew', TRUE);
}
function get_forms_by_date($id = NULL, $Year = NULL, $Month = NULL, $Day = NULL)
{
$this->pew->query('SELECT * FROM ExtractEvent'); //simplified for testing
}
Controller nomnom.php
public function index()
{
$this->load->model('pewmodel');
$data['Forms'] = $this->pewmodel->get_forms_by_date($this->session->userdata('Username'), date('Y'), date('n'), date('j'));
$this->load->view('common/header', $data['Forms']);
$this->load->view('home/index');
$this->load->view('common/footer');
}
View index.php
<pre>
<?php print_r($Forms); ?>
</pre>
This worked for me.
[from within the model constructor]
$db['hostname'] = 'localhost';
$db['username'] = 'root';
$db['password'] = '';
$db['database'] = 'my_database';
$db['dbdriver'] = 'mysql';
$db['dbprefix'] = '';
$db['pconnect'] = TRUE;
$db['db_debug'] = TRUE;
$db['cache_on'] = FALSE;
$db['cachedir'] = '';
$db['char_set'] = 'latin1';
$db['dbcollat'] = 'latin1_bin';
$db['swap_pre'] = '';
$db['autoinit'] = TRUE;
$db['stricton'] = FALSE;
$this->load->database($db, FALSE, TRUE);
// False=don't return db object
// True =use as active record, so it replaces default $this->db
Hope this helps coders searching through Google like I did.
$this->pew = $this->load->database('pew', TRUE);
You load a database which doesn't exist in your database-configuration.
I was getting a 500 error when I passed TRUE to connect like below. I assumed (yeah I know) that I was just having the same problem with connecting. truth be told it was an ID10T error from the beginning. I was missing the mssql.so library on my local machine and another I was testing from.
$this->pew =& $this->load->database('pew', TRUE)
My apologies for the waste of time gents.

Categories