Join model between customer entity and custom module - magento

In the Grid.php, I need to join customer/customer model with my custom model.
protected function _prepareCollection() {
$collection = Mage::getResourceModel('customer/customer_collection')->addNameToSelect();
$collection->getSelect()->join(
array('e' => 'event'), 'e.customer_id=main_table.entity_id', array('status')
);
$this->setCollection($collection);
return parent::_prepareCollection();
}
Basically I need to add some more information (in this example is status) to the collection. The event table contains the customer_id as reference to customer_entity.entity_id. How can I do this?

If there is any error you are getting you can share. Meanwhile try below updated 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 will help!

Related

Problem in inserting data into many to many relationships using attach() function laravel

i have two tables (orders & products) and one pivot table (order_product).
i have made many to many relationship b\w them using following code.
class Product extends Model{
protected $fillable = ['name', 'price', 'is_iframe_product'];
public function orders() {
return $this->belongsToMany(Order::class);
}
}
class Order extends Model{
public $guaded = ['id'];
protected $fillable = ['is_iframe_order','price', 'status', 'address_id','user_id'];
public function products () {
return $this->belongsToMany(Product::class);
}
}
i am using following code to insert records in CheckoutController.php
$Product = array('name' => $item['item_name'], "price" => $item['price'], "is_iframe_product" => "1");
$saved = order()->products()->attach([$Product]);
but getting this error:
exception: "Symfony\Component\Debug\Exception\FatalThrowableError"
file: "C:\wamp3\www\jewellery\jewellery\app\Http\Controllers\CheckoutController.php"
line: 63
message: "Call to undefined function App\Http\Controllers\order()"
Here's what to do:
First save the product into the database
$product = Product::create(array('name' => $item['item_name'], "price" =>$item['price'], "is_iframe_product" => "1"));
Then attach the saved product with the product's id
$saved = $order->products()->attach($product->id);
What you need to do is create the order first:
$order = new Order;
// set some attributes
$order->save();
Then you can attach the specified product:
$product = Product::find(1);
$order->products()->attach($product->getKey());
If you are creating the product on the fly:
$product = Product::create(array('name' => $item['item_name'], "price" =>$item['price'], "is_iframe_product" => "1"));
$order->products()->attach($product->getKey());
You're using a function: order()->...
And the error says the function doesn't exist: Call to undefined function ... order()
Did you mean to reference a variable, like this? $order->...
It would be helpful if you included the rest of the controller, since I don't know what variables you're using.
Also, your Order model has a typo: $guaded should be $guarded.

Magento Join and Left Join function

is there anyway that i can join groupdeals/coupon table to my grid collection?
this is my prepare collection function
protected function _prepareCollection()
{
$orderIds = Mage::getModel('sales/order_item')->getCollection()->addFieldToFilter('product_id', $this->getRequest()->getParam('id'))->getColumnValues('order_id');
$collection = Mage::getResourceModel('sales/order_grid_collection')->addAttributeToFilter('entity_id', array('in' => $orderIds));
$this->setCollection($collection);
return parent::_prepareCollection();
}
now i want it to join with groupon_coupons table
i want to join the tables so that i can filter Coupon code and security code from my order Grid.
add this line to collection ->join(array('tblalias' => 'tablename'),'main_table.fildname = tblalias.fildname',array('fildfromsecondtable' => 'tblalias.filedname'))

Display category name in product grid

I am trying to show category name for each product in admin grid. I did it like this and it works, but I am not sure is it correct way:
protected function _prepareCollection()
{
/**
* Tell Magento which collection to use to display in the grid.
*/
$collection = Mage::getResourceModel(
'catalog/product_collection'
);
$collection ->addAttributeToSelect('sku')
->addAttributeToSelect('name')
->addAttributeToSelect('type_id');
$collection->joinTable(
'catalog/category_product',
'product_id=entity_id',
array('my_cat'=>'category_id'),
null,
'left'
);
$collection->joinTable(
'catalog_category_entity_varchar',
'entity_id=my_cat',
array('mv'=>'value'),
null,
'left'
);
$collection->groupByAttribute('entity_id');
$this->setCollection($collection);
return parent::_prepareCollection();
}
I would appreciate if somebody could tell me is it correct, and is there a better way to do this?

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

Better way to add attribute to collection

I am overriding the Mage/Adminhtml/Sales/Order/Grid.php and adding some data to the prepareCollection. This is how I got the customer EAV Attribute campaign_id to be included in the collection, but it is kind of hacky. I was wondering if there was a better way.
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
foreach ($collection as &$object){
$customer = Mage::getModel('customer/customer')
->setId($object->getCustomerId())
->load();
$object->setCampaignId($customer->getCampaignId());
}
$this->setCollection($collection);
return parent::_prepareCollection();
}
You'll need to join the data from customer records onto the order collection before its loaded.
You can observe the collection before & after load events. For sales/order_grid_collection collection these events are sales_order_grid_collection_load_before and sales_order_grid_collection_load_after - you'll want to use the former. The collection can be accessed in your _before_load event observer via $observer->getOrderGridCollection().
protected function _prepareCollection() {
$collection = Mage::getResourceModel($this->_getCollectionClass());
$class = get_class($collection);
$attribute = Mage::getModel('eav/config')
->getAttribute('customer', 'campaign_id');
$attributeId = $attribute->getAttributeId();
$backendType = $attribute->getBackendType(); //probably varchar
$tableName = Mage::getSingleton('core/resource')
->getTableName('customer_entity_' . $backendType);
$collection->getSelect()
->joinLeft(array('v' => $tableName),
'main_table.customer_id = v.entity_id AND attribute_id = 153',
array('v.value', 'v.attribute_id'));
$this->setCollection($collection);
return parent::_prepareCollection();
}

Resources