Error: Call to undefined method CodeIgniter\Database\Postgre\Connection::select() - codeigniter-4

public function getLoad() {
$q = $this->conn->select('load.level_id, load.section_id, sub.subject_title, sub.subject_name')
->from('academics.tbl_teaching_load load')
->join('academics.tbl_subjects sub', 'sub.subject_id = load.subject_id')
->where('user_id', $this->request->getVar('user_id'))
->get()->getResult();
echo json_encode($q);
}
As the title said, it returns an error: Call to undefined method CodeIgniter\Database\Postgre\Connection::select()
I am new too CI4 and trying to join a table. The only problem I encounter so far is this. I can't seem to find the solution for the problem. Any help or tips will do. TYIA

Error:
Call to undefined method CodeIgniter\\Database\\Postgre\\Connection::select()
Explanation:
The error above is pretty clear. You're trying to call the select(...) method which doesn't exist in the CodeIgniter\Database\Postgre\Connection class.
However, the select(...) method exists in the CodeIgniter\Database\BaseBuilder class. You can receive the BaseBuilder instance from the Connection instance through the inherited CodeIgniter\Database\BaseConnection::table(...) method.
In addition, I would recommend you to first join your table(s) before selecting columns using the ->select(...) method. That would be easier to read and interpret.
Hence, reorder and change your query to...
Solution:
public function getLoad()
{
$q = $this->conn->table('academics.tbl_teaching_load load')
->join('academics.tbl_subjects sub', 'sub.subject_id = load.subject_id')
->where('user_id', $this->request->getVar('user_id'))
->select(['load.level_id', 'load.section_id', 'sub.subject_title', 'sub.subject_name'])
->get()->getResult();
echo json_encode($q);
}

Related

console.log is omitting some key pairs? [duplicate]

This question already has answers here:
mongoose .find() method returns object with unwanted properties
(5 answers)
Closed 5 years ago.
Working with a strange problem here. This is an array of objects which is pulled from mongodb and passed into the following function.
I tried the following 3 logs sequentially within the forEach on the array pulled from the database:
e (the object element within the array) which returns correctly. as you can see all the properties (keys) exist:
{ paid: false,
hotelWebsite: 'www.testing.com',
_id:5951848a24bb261eed09d638,
hotelAddress: '123 easy street',
...etc }
console.log(Object.keys(e)) is returning things that are not the keys...
[ '__parentArray',
'__parent',
'__index',
'$__',
'isNew',
'errors',
'_doc',
'$init' ]
and finally:
for(key in e){
console.log(key);
}
which returns an absolute mess of data, part of which DOES contain the actual keys of the object:
__parentArray
__parent
__index
$__
isNew
errors
_doc
$init
id
_id
hotelWebsite
hotelAddress
hotelNumber
hotelName
courseCost
courseDate
courseState
courseCity
courseName
paid
studentComments
studentEmail
studentPhone
studentCountry
studentZip
studentState
studentCity
studentAddress
studentCompany
studentName
schema
constructor
$__original_remove
remove
_pres
_posts
$__original_validate
validate
toBSON
markModified
populate
save
update
inspect
invalidate
$markValid
$isValid
ownerDocument
$__fullPath
parent
parentArray
on
once
emit
listeners
removeListener
setMaxListeners
removeAllListeners
addListener
$__buildDoc
init
$hook
$pre
$post
removePre
removePost
_lazySetupHooks
set
$__shouldModify
$__set
getValue
setValue
get
$__path
unmarkModified
$ignore
modifiedPaths
isModified
$isDefault
isDirectModified
isInit
isSelected
isDirectSelected
$__validate
validateSync
$__reset
$__dirty
$__setSchema
$__getArrayPathsToValidate
$__getAllSubdocs
$__handleReject
$toObject
toObject
toJSON
toString
equals
execPopulate
populated
depopulate
And a relevant sample of the code if needed:
studentsArray.forEach( (e, i) => {
if(task === 'nameTag'){
console.log(e);
console.log(Object.keys(e));
for(k in e){
console.log(k);
}
}
....
I need access to the properties (keys) for further processing within the forEach function. I am very confused on what is causing this and have never run into this sort of issue before. For the record the objects exist, using a console.log(typeof e) it IS an object (not a data "string"). I can access the properties using the dot or bracket notation but NOT using Object.keys() or for (keys in obj).
Can anyone help me sort this out please?
for ... in iterates all enumerable properties, both own and inherited. This is not "a strange bug," this is in fact the intended behavior.
As for the Object.keys(), unless it was overwritten by a non-compliant implementation, those are in fact enumerable keys of the object itself, so you are most likely mistaken. The e object has a .toJSON() method in its prototype that is implicitly called when you do console.log(e), so that is probably the output you are seeing there, and is not likely going to reflect exactly the same property keys as the original object. Try calling console.log(e.toJSON()) and I'm guessing it will be the same output as in the first one.
If you want only the object's own properties, use Object.getOwnPropertyNames(e).
If you want the keys printed in the first output, then use Object.keys(e.toJSON()).

Acumatica - An object Reference is Required or the non-static field, method, or property

hi does anyone encountered this error? everytime I use PXSelect on a foreach loop in which on the other source code does but on my code does not, could anyone identify the cause? the code below is also the the original source code from Acumatica but I only changed the Datamember from PaymentCharges to OtherCharges
[PXOverride]
public void VoidCheckProc(ARPayment doc)
{
foreach (PXResult<ARPaymentChargeTran> paycharge in PXSelect<ARPaymentChargeTran, Where<ARPaymentChargeTran.docType, Equal<Required<ARPayment.docType>>, And<ARPaymentChargeTran.refNbr, Equal<Required<ARPayment.refNbr>>>>>.
Select(this, doc.DocType, doc.RefNbr))
{
ARPaymentChargeTran charge = PXCache<ARPaymentChargeTran>.CreateCopy((ARPaymentChargeTran)paycharge);
charge.DocType = Document.Current.DocType;
charge.CuryTranAmt = -1 * charge.CuryTranAmt;
charge.Released = false;
charge.CuryInfoID = Document.Current.CuryInfoID;
charge.CashTranID = null;
//PaymentCharges.Insert(charge);
OtherCharges.Insert(charge);
}
}
I believe, you are writing this method in an extension for the base BLC
So instead of using 'this', use 'this.Base'
The Select method is non-static, as the error message says, but you call it on the PXSelect<...>-type. You need to have an instance of that type.
Based on Hybridzz answer, I assume you used the wrong overload of the Select-method. Probably your arguments do not have the correct type, so the compiler selects the best fitting overload of the method. In this case, it selects the one accepting only the argument params object[] o, which is non-static. A bit misleasing design of the API you use.

Can javascript function name contain a space?

I copied the code from kraken. I don't understand why there is a space between get and app(). Can someone please explain what's going on here?
var kraken = {
get app() {
return this._app;
},
use: function (route, delegate) {
//.....
}
}
No, in javascript a function cannot contain spaces. The code you are showing is using the get keyword to bind a property to a object.
get
Binds an object property to a function that will be called when that property is looked up.
Have a look to getters and setters in javascript.
It's a getter.
Check out this link.
The function is get and it's exposing a property called app.

C# create dynamic class and use it in Threadpool

I am trying to create a dynamic instance of a class at the runtime by using the system.dynamic. and pass it to the threadpool to execute it. But, I am getting an error saying the method in the class is used as a property. Below is the code.
Type t = Type.GetType("clsCountrySelectInsertUpdate_TEST");
dynamic dd = Activator.CreateInstance(t, null);
dd.intMode = 203;
ThreadPool.QueueUserWorkItem(new WaitCallback(dd.CountrySelectInsertUpdate));
I tried to call the method in the threadpool as below as well, but got an error saying that the return type void cannot be converted from the method CountrySelectInsertUpdate.
ThreadPool.QueueUserWorkItem(new WaitCallback(dd.CountrySelectInsertUpdate()),null);
The method CountrySelectInsertUpdate() does not take a parameter and returns a void.
Any help would be much appericated
WaitCallback requires state. You could easily use a lambda to write this:
ThreadPool.QueueUserWorkItem(new WaitCallback(state => dd.CountrySelectInsertUpdate()), null);
That being said, as you're already using .NET 4, I'd recommend using the TPL instead. It provides a nicer threading model in general than ThreadPool.QueueUserWorkItem, especially if you later need to get the results (or handle exceptions):
Task.Factory.StartNew( () => dd.CountrySelectInsertUpdate());

Groovy: how to test if a property access will be successful?

I have a variable Object foo, which is not null. I want to use foo.bar, but only if it won't bomb me with 'No such property: bar for class: Whatever'.
How should I do the following test:
if (/*test-here*/) {
use(foo.bar)
}
Use object.hasProperty(propertyName). This will return a truthy value (the property reference) if the property exists. Also object.metaClass.hasProperty(instance, propertyName) is possible. Use object.respondsTo(methodName) to test for method existence.
I do this in my Gradle scripts:
if(project.hasProperty("propertyThatMightExist")){
use(propertyThatMightExist)
}
If you're doing it on lots of foos and bars you could write (once, but before foo is created):
Object.metaClass.getPropertySafe =
{ delegate.hasProperty(it)?.getProperty(delegate) }
Then you can write:
foo.getPropertySafe('bar')
This worked for me :
Customer.metaClass.properties.find{it.name == 'propertyName'}.
Customer in this example is a domain class. Not sure if it will work for a plain Groovy class
boolean exist = Person.metaClass.properties.any{it.name == 'propName'}
if propName is an attribute ,exist=true // vice versa
I can't speak for Groovy specifically, but in just about every dynamic language I've ever used the idiomatic way of doing this is to just do it, and catch the exception if it gets thrown, and in the exception handler do whatever you need to do to handle the situation sensibly.

Resources