Magento how to join another table with customer grid - magento

I want to join another table with customer grid condition will be custom attribute value and table column value.
I have added one custom attributes for customers, attribute name is affiliate_id, and my other table name is aff_accounts, I want to show the aff_accounts table's column in the customer grid.
Please help me.
Thanks

If you are facing any issue you can ask me.Try the below code:
protected function _prepareCollection() {
$collection = Mage::getResourceModel('customer/customer_collection')->addNameToSelect();
$collection->getSelect()->join(
array('e' => 'event'), 'e.customer_id=main_table.entity_id', array('e.status') // added 'e.status' in stead of 'status'
);
$this->setCollection($collection);
return parent::_prepareCollection();
}
Hope it will help you.

Related

Laravel: Eager loading specific columns

Why does
Order::with(['products'=>function($q){
$q->select('name', 'price', 'quantity')->orderBy('name','asc');
}])->paginate($length);
returns all orders with their respective product data, but
Order::with(['products'=>function($q){
$q->select('name', 'price', 'quantity')->orderBy('name','asc');
}])->select('pickup_date', 'pickup_time', 'remark')->paginate($length);
gives me all order data I want, but an empty products array?
I want to select some specific columns from the order table with some specific columns from the products table. How can I do this?
FYI:
Orders-products have a many-to-many relationship with models:
Order Model:
public function products()
{
return $this->belongsToMany('App\Models\Product')->withTimestamps();
}
Product Model:
public function orders()
{
return $this->belongsToMany('App\Models\Order')->withTimestamps();
}
You need to select like this way :
Order::with(['products'=>function($q){
$q->orderBy('name','asc');
}])->select('products.name as name','products.price as price','products.quantity as quantity','orders.pickup_date as pickup_date', 'orders.pickup_time as pickup_time', 'orders.remark as remark')->paginate($length);
Or without sub query :
Order::with('products')
->select('products.name as name','products.price as price','products.quantity as quantity','orders.pickup_date as pickup_date', 'orders.pickup_time as pickup_time', 'orders.remark as remark')
->orderBy('name','asc');
->paginate($length);
You are missing one thing,you should add product id in outer select.
Order::with(['products'=>function($q){
$q->select('name', 'price', 'quantity')->orderBy('name','asc');
}])->select('product_id','pickup_date', 'pickup_time', 'remark')->paginate($length);
I hope it would be helpful
Try:
Order::query()
->select(['pickup_date', 'pickup_time', 'remark'])
->with(['products:name,price,quantity'])
->paginate($length);

How to order by eager loaded relationship in Laravel 5.5?

The code below works perfectly and displays all products with discounts in JSON Format for my API. But I want the result ordered by id in the discounts table. Something like orderBy('discount.id', 'desc'). Can anyone provide a solution for this? How it is possible to use orderBy with id column in discount table using has()?
public function promotions()
{
return $this->prepareResult(true, Product::has('discount')->where([
'company_id' => 1, 'is_active' => 1
])->with('category')->with('discount')->get(), [], "All Promotions");
}
You can use a function within your with statement:
public function promotions()
{
return $this->prepareResult(true, Product::has('discount')
->where(['company_id'=> 1, 'is_active'=>1])
->with('category')
->with(['discount' => function ($query) {
return $query->orderBy('id','DESC');
}])->get(), [],"All Promotions");
}
You can read about this here in the documentation.
If you want to go the collection method route instead of Alex's answer (which is also valid), you can just continue to chain collection methods after get. Since you included the with(), you can do
->get()->sortBy('discount.id')
https://laravel.com/docs/5.6/collections#method-sortby
Not related to your question, but wanted to point out that you can pass multiple arguments to with() so that you don't call it twice.
Product::has('discount')->where(['company_id'=> 1, 'is_active'=>1])->with('discount', 'category')->get()
As DevK mentioned, you need to do a join and then you can sort your products by that. The trick here is that you join the discounts table to your products table, but only select the id column (named as discount_id) from your discounts table so you can sort by those records.
In the below example, I assumed that your
Category model's table is categories
Product model's table is products
Discount model's table is discounts
products table references a discount.id as discount_id
This code will return every column from the products table plus a discount_id column, which you can ignore, it's only there for the sorting. It will also keep it as a collection with the relationships that you stated above.
public function promotions()
{
return $this->prepareResult(
true,
Product::has('discount')
->where(['company_id' => 1, 'is_active' => 1])
->with('category')
->with('discount')
->selectRaw('products.*, discounts.id as discount_id')
->join('discount', 'products.discount_id', '=', 'discounts.id')
->orderBy('discount_id', 'DESC')
->get(),
[],
"All Promotions"
);
}

magento - custom table join with catalog/product

I am having seller_id field in my custom table sellerrequest as foreign key. Primary reference is in customer/customer_collection collection. I want to show the seller name in admin grid from the seller_id. I am not sure how to join both collections but I tried with -
$collection = Mage::getModel("wallets/sellerrequest")
->join(
'customer/customer_collection',
'seller_id=main_table.seller_id'
)
->getCollection();
but, it doesn't work. Is this wrong way? Any Help appreciated.
Thanks.
Try this
$collection = Mage::getModel("wallets/sellerrequest")->getCollection();
$collection->getSelect()->joinLeft(
array('cust' => $collection->getTable('customer/customer_collection')),
'cust.seller_id = main_table.seller_id');
Hope this may help.By the way I haven't tried it. But the same worked for me.See the data in the collection to check whether you get the correct data or not.
Here is another example that i have tried.
protected function _prepareCollection(){
$collection = Mage::getModel('children/children')->getCollection();
$collection->getSelect()->joinLeft('schools', 'schools.school_id = main_table.school_id', array('school_name'));
$collection->addFieldToFilter('main_table.customer_id', array('in' => $this->_getCustomer()->getId()));
$this->setCollection($collection);
return parent::_prepareCollection();
}
Here i have joined "schools" table to my children model.In my case the common key between tables is school_id. This worked for me,check this out and make some ammendments to meet your requirement.

Magento add company name to order and customer grid

What I need is to show Company name instead of first and lastname whenever customer address is shown.
Besides that, I need to add company name to registration form, customer and order grid.
What is the best way(s) to achieve this?
I was adding this today into some Magento store and saw this old question, maybe my answer will help somebody else. This will answer you only how to add company name into order grid. To do that you need to extend Mage_Adminhtml_Block_Sales_Order_Grid class. Then you override _prepareCollection and _prepareColumns functions. This code is based on this free extension
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$tableName = Mage::getSingleton("core/resource")->getTableName('sales_flat_order_address');
$collection->getSelect()->join($tableName, "main_table.entity_id = $tableName.parent_id AND $tableName.address_type='billing'",array('company'));
$this->setCollection($collection);
return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumnAfter('company', array(
'header' => Mage::helper('customer')->__('Company'),
'index' => 'company'
),'billing_name');
return parent::_prepareColumns();
}

use multiple _prepareCollection()'s

Got this code (in: app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php):
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->join('sales_flat_order_item', '`sales_flat_order_item`.order_id=`main_table`.entity_id', array('skus' => new Zend_Db_Expr('group_concat(`sales_flat_order_item`.sku SEPARATOR "<br>")')))->group('sales_flat_order_item.entity_id');
$collection->getSelect()->join('sales_flat_order_payment', 'main_table.entity_id = sales_flat_order_payment.parent_id',array('method'))->group('sales_flat_order_payment.parent_id');
$this->setCollection($collection);
return parent::_prepareCollection();
}
When I comment the line below COL 1 or COL 2 this works fine, but I want them to work both, how is this done? (if I use them at the same time I get an error: Integrity constraint violation: 1052 Column 'entity_id' in group statement is ambiguous)
UPDATE WITH ANSWER:
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->join('sales_flat_order_item', '`sales_flat_order_item`.order_id=`main_table`.entity_id', array('skus' => new Zend_Db_Expr('group_concat(`sales_flat_order_item`.sku SEPARATOR "<br>")')));
$collection->getSelect()->join('sales_flat_order_payment', 'main_table.entity_id = sales_flat_order_payment.parent_id',array('method'))->group('main_table.entity_id');
$this->setCollection($collection);
return parent::_prepareCollection();
}
The answer is in the error, the table to apply the group to cannot be determined. You need to explicitly define the table:
...->group('sales_flat_order_item.entity_id');
echo your query by using echo (string) $collection->getSelect(); You will get a plain sql query here.
Fire that query in database and check, are you getting results the way you want.
Also last but not the least Group also create problem.
Have a look at below link
using group() breaks getSelectCountSql in magento

Resources