Dispaly records from database in magento - mysql

I am using this code in my Grid.php to display records from a single tabel 'paypal_payment_transaction':
protected function _prepareCollection()
{
$db = Mage::getSingleton('core/resource')->getConnection('core_write');
$query = "SELECT transaction_id,txn_id,additional_information,created_at,user_id,reference_txn
FROM `paypal_payment_transaction`
LIMIT 0 , 30";
$result = $db->query($query);
// Get count of affected rows
$affected_rows = $result->rowCount();
$orders = $result->fetchAll($sql);
foreach($orders as $order)
{
echo "<pre>"; print_r($order);
}
}
I need this query in magento way:
may be like this
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('sales/order_collection')
SOME QUERY TO SELECT RECORD FROM TABEL 'paypal_payment_transaction'
;
$this->setCollection($collection);
parent::_prepareCollection();
return $this;
}
So that I can display it in grid accordingly i.e. in :
protected function _prepareColumns()
{
-------------------
------------------
}

Try this -
protected function _prepareCollection()
{
$collection = Mage::getModel('paypal/payment_transaction')->getCollection();
$this->setCollection($collection);
parent::_prepareCollection();
return $this;
}

you can try the following code
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('payment/transaction')
$Collection->addFieldToSelect('transaction_id');
$this->setCollection($collection);
parent::_prepareCollection();
return $this;
}
Thanks

Related

Laravel saves child record, but sets foreign key to null

This has got to be a simple fix, as I have done this many times before. But as it stands I am completely stumped. I use the following code to save a parent object Unknown_Tag and its many children.
Method:
public function saveUnknown(Request $request)
{
$url = $request->url;
$tag = new Unknown_Tag();
$tag->url = $url;
$protocol =
substr($url, 0, strpos($url, ':'));
$tag->protocol = $protocol;
$domain =
parse_url($url, PHP_URL_HOST);
$tag->domain = $domain;
$tag->save();
//get the path
$Path = parse_url($url, PHP_URL_PATH);
if ($Path) {
$splitPath = substr($Path, 1);
$paths = explode('/', $splitPath);
foreach ($paths as $p) {
$path = new Path();
$path->path = $p;
$tag->Paths()->save($path);
}
}
//get Queries
$splitQuery = parse_url($url, PHP_URL_QUERY);
if ($splitQuery) {
$queries = explode('&', $splitQuery);
foreach ($queries as $q) {
$query = new Query();
$q = substr($q, 0, strpos($q, '='));
IF (SUBSTR($q, -1) != ' ') {
$q .= ' ';
}
$query->var = $q;
$value = $q = preg_replace('/^[^=]*:/', '', $q);
$query->value = $value;
$tag->Queries()->save($query);
}
}
}
The Parent Object
class Unknown_Tag extends Model
{
protected $table = 'unknown_tags';
public $timestamps = false;
public function Paths()
{
return $this->hasMany('App\Path', 'tag_id', 'ID');
}
public function Queries()
{
return $this->hasMany('App\Query', 'tag_id', 'ID');
}
}
The Child objects
class Query extends Model
{
protected $table = 'queries';
public $timestamps = false;
public function Tag()
{
return $this->belongsTo('App\Unknown_Tag', 'tag_id', 'ID');
}
}
class Path extends Model
{
protected $table = 'paths';
public $timestamps = false;
public function Tag()
{
return $this->belongsTo('App\Unknown_Tag', 'tag_id', 'ID');
}
}
When I run all this via a post request, The Parent and all the children are saved properly, but all the child objects have a foreign key that is set to null. If I manually change the foreign key to what it should be, everything works just fine, so I am fairly sure this is not a problem with my database. Can anyone see the obvious that I am missing here?
EDIT:Just to be clear, this returns no errors
If anyone ever sees this, laravel assumes the default primary key is 'id'. I had set mine to 'ID', so I had to let laravel know by using
protected $primaryKey = 'ID';
in my Unknown_tag definition

How to get URL ID into MySQL query for CodeIgniter

Basically, this is what I wanted to do, how does it work?
public function get_product_category(){
$id = $this->uri->segment(3);
$query = $this->db->query('SELECT * FROM products where category_id = $id ');
return $query->row();
}
You could use Query Builder Class
public function get_product_category(){
$this->db->where('category_id', $this->uri->segment(3));
$query = $this->db->get('products');
return $query->row();
// return $query->row_array();
}
Or
public function get_product_category(){
$this->db->select('*');
$this->db->from('products');
$this->db->where('category_id', $this->uri->segment(3));
$query = $this->db->get();
return $query->row();
// return $query->row_array();
}
I would autoload the database library.

Codeigniter Select JSON, Insert JSON

I have very simple users database: user_id, user_name, user_email
My model this:
class Users extends CI_Model {
private $table;
private $table_fields;
private $table_fields_join;
function __construct() {
parent::__construct();
$this->table = 'users';
$this->table_fields = array(
$this->table.'.user_id',
$this->table.'.user_name',
$this->table.'.user_email'
);
$this->table_fields_join = array();
}
function select(){
$this->db->select(implode(', ', array_merge($this->table_fields, $this->table_fields_join)));
$this->db->from($this->table);
$query = $this->db->get();
if($query->num_rows() > 0){
return $query->result();
} else {
return false;
}
}
function insert($data) {
$data = array(
'user_name' => $data['user_name'],
'user_email' => $data['user_email']
);
$this->db->insert($this->table, $data);
}
My controller this:
class Users extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('users');
}
public function select(){
$data['query'] = $this->users->select();
$data = json_encode($data['query']);
echo $data;
}
public function insert($json){
$data = json_decode($json);
$this->users->insert($data);
}
}
And this is my routing.php:
$route['default_controller'] = 'Welcome';
$route['users'] = 'users/select';
$route['users/insert/:(any)'] = 'users/insert';
I would like that 127.0.0.1/users/select give json.
Example: [{"user_name":"user1","user_email":"user#user.de"}]
This JSON insert my table: 127.0.0.1/users/insert/[{"user_name":"user1","user_email":"user#user.de"}]
But my code is not working. :-(
You want to return json object in response, so it's required to set json type in response header. As given here
public function select(){
$data['query'] = $this->users->select();
$this->output
->set_content_type('application/json')
->set_output(json_encode($data['query']));
}
It is required to encode part as below for insert part. so you can use this generated url to call your insert.
site_url('usres/insert/'.urlencode('[{"user_name":"user1","user_email":"user#user.de"}]'));
your insert route should be as
$route['users/insert/:(any)'] = 'users/insert/$1';
your insert method should be updated as
public function insert($json){
$json = urldecode($json);
$data = json_decode($json);
$this->users->insert($data);
}
}

Issue with a Table Model

I've been having a problem with a table model recently and I can't really explain why this happens. I want to select everything from a table and return it in a descending order. But when I try to display it nothing shows up:
Code:
<?php
//Will be used at a later date
namespace Blog\Model\Table;
use Zend\Db\TableGateway\TableGateway;
class Blog extends TableGateway
{
public function __construct($adapter)
{
parent::__construct('posts', $adapter);
}
public function displayPosts()
{
$adapter = $this->getAdapter();
$result = $adapter->query('SELECT * FROM `posts` ORDER BY `date_added` DESC');
return $result;
}
}
Result:
But when I add an $id argument it shows the data:
Code:
<?php
//Will be used at a later date
namespace Blog\Model\Table;
use Zend\Db\TableGateway\TableGateway;
class Blog extends TableGateway
{
public function __construct($adapter)
{
parent::__construct('posts', $adapter);
}
public function displayPosts($id = 6)
{
$adapter = $this->getAdapter();
$result = $adapter->query('SELECT * FROM `posts` WHERE `post_id` = ?', array($id));
return $result;
}
}
Result:
It's weird that it works with an argument and not without, any ideas?
You needed to order it by post_id
public function displayPosts()
{
$adapter = $this->getAdapter();
$result = $adapter->query('SELECT * FROM `posts`')->order(array('post_id' => 'DESC'));
return $result;
}

How to get proper collection in magento grid?

I have a custom grid tab in magento category with separate table to store data.
This is table with data:
When I filter by 'Yes' under first column. It does return associated rows with ID (284 & 285) but when I select 'No', it returns IDs (281-283), it's supposed to return 283 only which is the only stream not configured already.
And when I select ANY, it returns all associated and not associated with duplicate streams to select.
What I need is show each streams once only and work with first column filter.
I think I have incorrect joins with another table which holds all the streams (groups).
This is another table
protected function _prepareCollection()
{
if ($this->getCategory()->getId()) {
$this->setDefaultFilter(array('selected_tcategoryacl'=>1));
}
/* #var $collection Technooze_Tcategoryacl_Model_Mysql4_Tcategoryacl_Collection */
$collection = Mage::getModel('tcategoryacl/tcategoryacl')->getCollection();
$collection->getSelect()->joinRight('school_group', 'school_group_id=group_id');
$collection->addFieldToFilter('school_group_status', 1);
//$collection->getSelect()->group('school_group.school_group_id');
$collection = $this->insertFakeIdsToCollection($collection);
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _addColumnFilterToCollection($column)
{
$tcategoryaclIds = $this->_getSelectedTcategoryacl();
switch($column->getId()){
case 'selected_tcategoryacl':
if (empty($tcategoryaclIds)) {
$tcategoryaclIds = 0;
}
//$this->getCollection()->addFieldToFilter('category_id', $this->getCategory()->getId());
if ($column->getFilter()->getValue()) {
$this->removeRightJoin();
$this->getCollection()->clear()->addFieldToFilter('tcategoryacl_id', array('in'=>$tcategoryaclIds));
} elseif(!empty($tcategoryaclIds)) {
$this->getCollection()->clear();
$this->getCollection()->addFieldToFilter('tcategoryacl_id', array('nin'=>$tcategoryaclIds));
//$this->getCollection()->addFieldToFilter('school_group.school_group_id', array('nin'=>$this->_getSelectedSchoolGroupIds()));
$this->setCollection($this->insertFakeIdsToCollection($this->getCollection()));
}
break;
/*case 'allow_from':
case 'allow_to':
$this->getCollection()->addFieldToFilter($column->getId(), array('like'=>$column->getFilter()->getValue()));
break;*/
default: parent::_addColumnFilterToCollection($column);
}
return $this;
}
private function removeRightJoin(){
$this->getCollection()->clear()->getSelect()->reset('RIGHT JOIN');
}
protected function insertFakeIdsToCollection($collection)
{
$i = 0;
foreach($collection as $v){
// lets have fake ids for empty tcategoryacl_id, so checkbox can be selected
if(!$v->getData('tcategoryacl_id')){
$v->setData('tcategoryacl_id', '0' . $this->getCategory()->getId() . $i++);
$v->setId($v->getData('tcategoryacl_id'));
}
// lets auto select different groups in each row
if(!$v->getData('group_id')){
$v->setData('group_id', $v->getData('school_group_id'));
}
}
return $collection;
}
/**
* #return array
*/
protected function _getSelectedSchoolGroupIds()
{
$selected_tcategoryacl = array();
$category = $this->getCategory();
$collection = Mage::getModel('tcategoryacl/tcategoryacl')->getCollection();
$collection->addFieldToFilter('category_id', $category->getId());
foreach($collection as $v)
{
$selected_tcategoryacl[] = $v->getGroupId();
}
return $selected_tcategoryacl;
}
/**
* #return array
*/
protected function _getSelectedTcategoryacl()
{
$selected_tcategoryacl = array();
$category = $this->getCategory();
$collection = Mage::getModel('tcategoryacl/tcategoryacl')->getCollection();
$collection->addFieldToFilter('category_id', $category->getId());
foreach($collection as $v)
{
$selected_tcategoryacl[] = $v->getTcategoryaclId();
}
return $selected_tcategoryacl;
}
Update
added code on repo - https://github.com/dbashyal/Tcategory_ACL/blob/master/app/code/community/Technooze/Tcategoryacl/Block/Adminhtml/Catalog/Category/Tab/Tcategoryacl.php