I have been trying to solve this problem for a long time already. I tried searching for an answer in stackoverflow, google and other sources but I didn't manage to find something that could lead me to the solution of the problem. I really hope you can help me. Thanks in advance.
Im using Codeigniter 3
My query in codeigniter is as shown here bellow:
$this->db->select($fields)
->from("usuario as u")
->join("usuario_info as ui ", "ui.idusuario = u.idusuario","left");
$this->db->where("usua_url",$this->url_usuario);
return $this->db->get(); //last line
I do get results from the query with data from a record which has an id (in the database is a PK and can't be NULL
My compiled query from CI is:
SELECT * FROM `usuario` as `u` LEFT JOIN `usuario_info` as `ui` ON `ui`.`idusuario` = `u`.`idusuario` WHERE `usua_url` = 'heri'
And my result is the following after applying result_array method (I censored some field names and its values, you know..):
array(9) {
["idusuario"]=> NULL
["other_field1"]=> string(9) "censored"
["other_field2"]=> string(60) "censored"
["other_field3"]=> string(26) "censored"
["other_field4"]=> string(19) "censored"
`["other_field5"]=> string(7) "censored"
["other_field6"]=> string(4) "censored"
["other_field7"]=> NULL
["other_field"]=> NULL
}`
When I copy and paste exactly the same query into DataGrip or Workbench I get the same results except for idusuario, the results look like:
array(9) {
["idusuario"]=> 1
["other_field1"]=> string(9) "censored"
["other_field2"]=> string(60) "censored"
["other_field3"]=> string(26) "censored"
["other_field4"]=> string(19) "censored"
`["other_field5"]=> string(7) "censored"
["other_field6"]=> string(4) "censored"
["other_field7"]=> NULL
["other_field"]=> NULL
}`
Well after trying something I thought wouldn't make any difference I got to the solution.
I had a method like for the query like this:
public function selectPerfilInfo($fields = "*"){
$this->db->select($fields);
$this->db->from("usuario as u");
$this->db->join("usuario_info as ui ", "ui.idusuario = u.idusuario","left");
$this->db->where("usua_url",$this->url_usuario);
return $this->db->get();
}
I was calling it like this in my controller:
//before calling the method I already set the `url_usuario` attribute of my class
$this->my_controller->selectPerfilInfo();
I changed the line above to this:
$this->my_controller->selectPerfilInfo("u.idusuario, u.other_field1");
After passing the specific fields to the function it returns the right results.
Im not sure why it happens and this leaves me a lot of doubts because I have used the same structure for getting records from other tables with no problems.
I'd appreciate if any of you could give me any information about it. Thanks
I have an array which I return from a class. It contains data for me which I need in the controller of my application. However something strange is happening. Some values are when I get them by key not defined. But when I print the whole array they are defined. But that's not all. Some keys return their value, some don't.
To test if the key can be found I used array_key_exists(), which returned true. Let's get to the example. When I print $this->case->data, it will return the following;
[id] => 1
[ticket_number] => 2334
[user_id] => 2
[ticket_state] => 1
Which is okay. To test this out I tried returning the ticket_number value. ($this->case->data['ticket_number']). It worked perfectly and returned it's value. However when I try to return ticket_state ($this->case->data['ticket_state']) I get an undefined as value.
It might be that I'm missing something here. What am I doing wrong? And if I'm doing it wrong how can it be that ticket_number works, but ticket_state does not?
Edit:
I do get undefined as value returned. Not an undefined index error. I did a var_dump() on the variable.
array(4) { ["id"]=> string(1) "1" ["ticket_number"]=> string(14) "2334" ["user_id"]=> string(1) "2" ["ticket_state"]=> string(1) "1" }
Edit II:
My code where I try to receive the value is the following simple if statement;
if($this->case->data['ticket_state'] == 2){
$this->case->reopen();
}
I've tried adding the 2 in quotes, but since $this->case->data['ticket_state'] is undefined it the statement will be executed regardless. (Which I found strange too. Why should it still be executed when undefined is not the same as 2 or '2'?).
The current value of my column ticket_state is 1.
I am creating a download site, and I am trying to set access to specific downloads depending on the company the user works for. When setting the company permissions, I am trying to retrieve information from the database to say whether it has access or not. I am using a method from a class to look for the company and software correlation and placing it in an array. I am then trying to access a specific value within that array, but all I get is this warning 'Trying to get property of non-object'.
I am unsure where I am going wrong.
Class Method
public static function locateAccess($company, $software)
//find the access a company has to a particular software
{
$sql = "SELECT * FROM software_access WHERE software_company_id = '$company'";
$sql .= " AND software_software_id = '$software'";
return self::findQuery($sql);
}
Code on the Page
$access = SoftwareAccess::locateAccess($companyId,$accessName);
$allowed = $access->software_access;
Output with a Var Dump
array(1) {
[0]=> object(SoftwareAccess)#34 (4) {
["software_access_id"]=> string(2) "22"
["software_company_id"]=> string(1) "3"
["software_software_id"]=> string(1) "4"
["software_access"]=> string(1) "1"
}
}
Notice: Trying to get property of non-object in C:\xampp\htdocs\RMS\public\companyAdmin.php on line 136
As you can see the object does exist within the array, and I have used a similar functionality in other parts of my website.
Any assistance is much appreciated
You need to do like
$access[0]->software_access;
I have following code in my controller:
$model=ChForms::model()->findByPk($id);
$decode= $model->json;
$res=CJSON::decode($decode);
var_dump($res);
return $this->render('index', array('model'=>$model, 'res'=>$res));
the result of var_dump($res) is:
["p_2"]=> string(4) "test" ["p_3"]=> string(1) "0"
How can I call values of ["p_2"] and ["p_3"] in my view(e.g test, 0)
CJSON::decode() returns an array by default. Since you are passing $res into your view you can access the required fields via $res["p_2"] and $res["p_3"].
I have a problem when I want to query table using ORM ,example I have article table with field id,author,text.
My code like this :
// Single where
$article = Model_Article::find()->where('id', 4);
print_r($article);
that't code will be fetch all field on table article, it's like select * from article where id = 4
Try Possibility
$article = Model_Article::find(null, array('id','title'))->where('id', 3);
the response is
object(Orm\Query)#89 (14) {
["model":protected]=>
string(10) "Model_Article"
["connection":protected]=>
NULL
["view":protected]=>
NULL
["alias":protected]=>
string(2) "t0"
["relations":protected]=>
array(0) {
}
["joins":protected]=>
array(0) {
}
["select":protected]=>
array(1) {
["t0_c0"]=>
string(5) "t0.id"
}
["limit":protected]=>
NULL
["offset":protected]=>
NULL
["rows_limit":protected]=>
NULL
["rows_offset":protected]=>
NULL
["where":protected]=>
array(1) {
[0]=>
array(2) {
[0]=>
string(9) "and_where"
[1]=>
array(3) {
[0]=>
string(5) "t0.id"
[1]=>
string(1) "="
[2]=>
int(3)
}
}
}
["order_by":protected]=>
array(0) {
}
["values":protected]=>
array(0) {
}
}
that's is not return id or title field.
but when i'm try by adding get_one() method
$article = Model_Article::find(null, array('id','title'))->where('id', 3)->get_one();
id is return , but title is not and another field, i don't know why ?
Reference
ORM Discussion FuelPHP it's say ORM currently will be select all column, no plans to change that at the moment.
My Problem
Select Custom Field using ORM like this select id,owner from article where id = 4 it's will be return only id & owner, Is Possible to get that using ORM on FUELPHP ?
Do not use
Model_Article::find()->
but use
Model_Article::query()->
The first one works but is considered an error situation which might change in future versions.
As of version 1.4 the ORM supports partial selects, using
Model::query()->select('id', 'value')->
The second parameter of find() is an array of conditions for the find, such as 'where' or 'order_by' clauses. There is no support for selecting column names in this array.
The Orm\Model fetches all column because it can't deal with incomplete / partials objects.
If you want a custom query, don't use the ORM, use the query builder for that.
DB::select('id','title')->from(Model_Article::table())->where('id', 4);
if you are trying to find out result like query "select * from article where id = 4"
$article = Model_Article::find()->where('id', 4)->get_one();
print_r($article);
and one more concept you should understand
get_one() returns only one record (as a object ).
get() returns multiple record ( in the form of array of objects ).
and if your field is not displaying then check the model Model_Article .this problem may be occur when you have not declared fields in properties list.
Fuelphp has been release new version , you can see this documentation , in my case fuelphp version is 1.2 and cannot select custom field using orm.
select custom field using orm is available since version 1.4
http://fuelphp.com/docs/packages/orm/crud.html
// Find only some columns
Model_Article::query()->select('id', 'name');
Model_Article::find('all', array('select' => array('id', 'name')));