Not able to retrieve value from an array when it exists - php

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;

Related

Codeigniter query returns record with null id. Which shouldn't be possible

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

Get the id field of an array

I have a request with Kohana which returns an array, just like this :
$query = DB::select()
->from('environnement')
->where('utilisateur_id','=', 'd83fa9a71cc1c414011cc1dbeb270026')
->where('region_id','=', $region_selectionnee);
$id_env = $query->execute();
return $id_env->as_array();
I call the request in my controller and var_dump the result. I get :
array(1) {
[0]=> array(4) {
["id"]=> string(32) "d83fa9a71cc1c414011cc1de74270027"
["courant"]=> string(1) "0"
["region_id"]=> string(1) "1"
["utilisateur_id"]=> string(32) "d83fa9a71cc1c414011cc1dbeb270026"
}
}
Now, I want to get the id field of this array, but I don't know how to do. I tried with $id_environnement->id but it says
Trying to get property of non-object
I also tried with $id_environnement["id"], but it says :
Undefined index: id
Can somebody help me to get this id please ? Thanks in advance !
As you can see, the result is an array, in which is the object you want. So you have to use $id_environment[0]["id"] to get it.
Arr::path($id_environment, '0.id')
This method don't raise exception if array key not exists
You can also use:
$id = DB::select('id')
->from('environnement')
->where('utilisateur_id','=', 'd83fa9a71cc1c414011cc1dbeb270026')
->where('region_id','=', $region_selectionnee)
->limit(1)
->execute()
->get('id', false);
Where the second argument in get() is what you want returned if 'id' is not found (if you don't include a second argument, it will return null if 'id' is not found).
edit
You don't have to specify 'id' within select(), but if you do specify a field (or fields), get() can only retrieve from the specified field(s).

Get the from and to object from p2p_id in WordPress - WP Posts to Posts Plugin

I'm now using WP Posts to Posts plugin in wordpress.
And now I made some links between posts and posts, users and users, or posts and users.
And in all three cases above, I want to do more with the p2p plugin.
For example, I may fetch the p2p_id first:
$users = get_users( array(
'connected_type' => 'multiple_authors',
'connected_items' => $post
) );
foreach($users as $user) {
$p2p_id = $user->p2p_id;
// ********** ATTENTION *********
// Here, I got the p2p_id of the p2p object
// In a general purpose, I want to get the
// from and to object from the p2p_id
}
So, how can I get the from and to object via the p2p_id? I've found over the documentation, but seemed no effective ways.
No other answers. So I finally found the answer myself from the source code:
File: /wp-content/plugins/posts-to-posts/vender/scribu/lib-posts-to-posts/api.php
Notice that there is a function: p2p_get_connection($p2p_id)
Then we call that function with a known p2p_id, an stdClass Object is returned, like the below:
object(stdClass)#2509 (4) {
["p2p_id"]=>
string(1) "8"
["p2p_from"]=>
string(2) "84"
["p2p_to"]=>
string(1) "2"
["p2p_type"]=>
string(10) "my_post_to_user"
}
Now that we can get the p2p_from id and p2p_to id, and we know it is a post or an user. We can construct the object.
So the finally solution may look like:
$conn = p2p_get_connection($this->p2p_id);
$from = get_post(intval($conn->p2p_from));
$to = new WP_User(intval($conn->p2p_to));
Still seemed not found in the documentation, hope it helps.

Is Possible Custom Select Field using ORM on Fuelphp?

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

How do I load a Magento product and find stores it's assigned to

I've been asked to reverse engineer some orders (from an Ebay Extension) and find out which store they really used.
To flesh out an example; we run several stores from one Magento backend selling various products. These are all sold on eBay using a single merchant account.
So what I need to do is load the order as it stands assigned against the eBay store, load the items attached to that order and then see what other stores that item is used on.
Once I get that far I can simply filter out the admin storeID and the eBay storeID which will leave with the store I'm looking for.
this is what I have so far:
foreach($collection->getItems() as $order):
// need to do this to load correct order information
$order = Mage::getModel('sales/order')->loadByIncrementID($order->getIncrementId());
$items = $order->getItemsCollection();
foreach($items as $item) {
// need to do this to get the actual item, not the item on the order
$item = Mage::getModel('catalog/product')->load($item->getItemId());
// do something to get the other store ids
}
endforeach;
You can use Mage_Catalog_Model_Product::getStoreIds() method to get list of these stores:
$item = Mage::getModel('catalog/product')->load($item->getItemId());
$storeIds = $item->getStoreIds();
Then $storeIds will contain array of store ids, and when you dump that:
var_dump($storeIds)
you should get following result:
array(3) {
[0]=>
string(1) "1"
[1]=>
string(1) "3"
[2]=>
string(1) "5"
}
try this
$item = Mage::getModel('catalog/product')->load($item->getItemId());
$storeId = $item->getStoreId();
this works just fine for me.

Categories