magento 2 collection gives empty data? - magento

below is my code:
$transObj = $this->_objectManager->create('Magento\Sales\Model\Order\Payment\Transaction');
$order_id = 9706;
$trans_result = $transObj->getCollection()
->addAttributeToSelect(
'order_id'
)
->addAttributeToSelect(
'payment_id'
)
->addAttributeToSelect(
'is_closed'
)
->addAttributeToSelect(
'txn_id'
)
->addAttributeToSelect(
'transaction_id'
)
->addFieldToFilter(
'order_id',
array("eq" => $order_id)
);
var_dump($trans_result->getData());
echo "\n Query:".$trans_result->getSelect();
below i am getting the out put as :
array(0) {
}
Query :SELECT main_table.order_id, main_table.payment_id, main_table.is_closed, main_table.txn_id, main_table.transaction_id FROM sales_payment_transaction AS main_table WHERE (order_id = 9706)
when i run the query in the Mysql , i am getting the data fetched properly.
But when i check the output of the result ($trans_result) using getData();
i am getting empty data.
Any help will be appreciated.?
Thanks in advance.

I'm pretty sure its within the attributes. Make it simpler.
$order_id = 9706;
$trans_result = $transObj->getCollection()
->addFieldToFilter('order_id', array('eq' => $order_id))
You should the same value because you're using all columns from the table, so there's no necesity to addfielters on SELECT clause. Although if you want to Select an scpecific column use
$trans_result = $trans_result->addFieldToSelect('payment_id')
Note I'm using addFieldToSelect instead of addAttributeToSelect

Related

CodeIgniter 3.1.7 unbuffered_row() returning NULL but row() works

I am attempting to output a CSV from a query result. dbutil->csv_from_result() should work, but it only returns the column headers. No data is included.
I tracked it down to system/database/DB_utility.php which uses unbuffered_row().
Any calls to unbuffered_row() return NULL. If I change it to row(), I get a result. It makes no difference if I use unbuffered_row('array') or unbuffered_row('object')
Why does row() work but unbuffered_row() does not?
Is this a bug in CI or am I missing something?
Edit: row('array') doesn't seem to work either.
Edit: It seems that calling $query->result() spoils dbutil->csv_from_result($query). You apparently cannot iterate through query results AND then save the results in a CSV file. This was possible in CI 2.
Is there any way to show query results AND save the CSV without running the query twice?
I have faced similar problem and found this:
I have this query:
$query = $this->db->query("SELECT * FROM tablename");
print_r($query);
Then I get this and unbuffered_row works fine:
CI_DB_oci8_result Object
(
[stmt_id] => Resource id #106
[curs_id] =>
[limit_used] =>
[commit_mode] => 32
[conn_id] => Resource id #91
[result_id] => 1
[result_array] => Array
(
)
[result_object] => Array
(
)
[custom_result_object] => Array
(
)
[current_row] => 0
[num_rows] =>
[row_data] =>
)
BUT if I call $query->num_rows() before I get a different CI_DB_oci8_result object and unbuffered_row didn't work returning null
$query = $this->db->query("SELECT * FROM tablename");
echo $query->num_rows();
print_r($query);
Hope this help somebody.

How to insert the array values in database using for each loop

I have array i want insert into the values in database using codeigniter, i don't know how to insert , i trying but i am not able to get the answer
My model
print_r($subjectHandling);
Array
(
[subjectId] => Array
(
[0] => 1
[1] => 2
)
)
now i want insert the values in database in this values.
I am trying like this
foreach($subjectHandling as $key=>$value) {
$reg_dat = array(
'statffId' => '1',
'subjectId' => $value,
);
$this->db->insert("subject_handling" , $reg_dat);
}
I m getting error ** Array to string conversion** , so how do this. i want to insert two roes in databse
This should work
$subjectHandling['subjectId'] = array(1, 2);
$reg_dat = array();
foreach($subjectHandling['subjectId'] as $key => $value) {
$reg_dat[] = array('staffId'=> 1, 'subjectId' => $value);
}
$this->db->insert_batch('subject_handling', $reg_dat);
https://www.codeigniter.com/userguide3/database/query_builder.html#inserting-data

result_array() data in where clause in OR condition : Active records Codeigniter

result_array() for a query gives the following :
Array ( [0] => Array ( [id] => 1 ) [1] => Array ( [id] => 4 ) )
I want to use id=1 and id=4 in the where clause in the OR condition like the following :
$this->db->where_in('id',$query->result_array());
But the above causes error. Is there any direct way of doing the same?
You can simply use array_column
array_column($query->result_array(), 'id')
here is reference to it.
$result = $roleIdQuery->result_array();
$column = array_map(function($sub)
{
return $sub['id'];
}, $result);
I used this. Sadly.
because it has arrays within that array.
$ids=array();
$resultArray=$query->result_array();
foreach($resultArray as $result){
$ids[]=$result['id'];
}
$ids // this is what you need to pass now..
If you change your original query to use GROUP_CONCAT. Not an option in Active Record so would need to write your own SQL. something like
$query= $this->db->query('SELECT GROUP_CONCACT(id) AS ids FROM table WHERE condition=true');
Then you should be able to do
$this->db->where_in('id',$query->result_array()[0]['ids']);
Just noticed your on 5.3 so unfortunately that means this won't work you will have to do.
$result = $query->result_array();
Then pass in $result[0]['ids']

laravel whereraw with where in statement

I'm trying to use a WHERE IN statement in the query builder using whereRaw but it doesn't seem to work. I'm not trying to select values from other tables though, just selecting from multiple values.
I've tried these 3 approaches:
return $this->object->whereRaw("`status` = 'active' AND `salesType` IN ( ? ) AND `region_id` = ?", array("'sale','buy'","1"))->paginate(10);
return $this->object->whereRaw("`status` = 'active' AND `salesType` IN ( ? ) AND `region_id` = ?", array("sale,buy","1"))->paginate(10);
return $this->object->whereRaw("`status` = 'active' AND `salesType` IN ( ? ) AND `region_id` = ?", array(array("sale,buy"),"1"))->paginate(10);
Why don't you use where and whereIn` methods?
return $this->object->where('status', '=', $active)->whereIn('salesType', $array);

jdatabase no numbers appearing in article

I am testing outputting a query in a article in Joomla 3.0.2 via Sorcerer.
When i output the array storing the query only values that are no numbers are appearing.
E.g. say i have 2 rows in a table called 'goofy' like this
id, description
1, test
2, test2
My code then looks like this
$query = "SELECT * FROM goofy";
$db->setQuery($query);
$results = $db->loadAssocList();
print_r($results);
The output I am getting in the article is like this;
Array ( [0] => Array ( [id] => [description] => test ) [1] => Array ( [id] => [description] => test2 ) )
Any ideas why the numbers wont output?
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*');
$query->from('goofy');
$db->setQuery($query);
$results = $db->loadAssocList();
print_r($results);
You haven't specified the table name correctly. When using database queries, you have to add the prefix onto the table name. You also need to call the database using $db = JFactory::getDBO();
So your query should be like this:
$db = JFactory::getDBO();
$query = "SELECT * FROM #__goofy";
$db->setQuery($query);
$results = $db->loadAssocList();
print_r($results);
If you database table isn't Joomla related, then you don't need to use the #__ prefix.

Resources