Having read Fixing Magento Flat Collections with Chaos by Alan Storm and finding a similar SO question here I am trying to return products that are in a category but without using Magento's "flat" data.
Here is the code that I originally used:
$category_model = Mage::getModel('catalog/category')->load($cid);
$collection = Mage::getModel('catalog/product_collection');
$collection->addCategoryFilter($category_model);
$collection->addAttributeToSort('entity_id','DESC');
$collection->addAttributeToSelect('*');
$collection->printLogQuery(true);
When this code gets fired through AJAX I get different results than when I run it from an observer and the reason is because of flat data. So I have written my own classes that are meant to use the EAV model:
app/code/local/Mynamespace/Mymodule/Model/Category.php:
class Mynamespace_Mymodule_Model_Category extends Mage_Catalog_Model_Category
{
protected function _construct()
{
$this->_init('catalog/category');
}
}
And:
app/code/local/Mynamespace/Mymodule/Model/Productcollection.php:
class Mynamespace_Mymodule_Model_Productcollection
extends Mage_Catalog_Model_Resource_Product_Collection
{
protected function _construct()
{
$this->_init('catalog/product');
$this->_initTables();
}
}
And then change my query code:
$category_model = Mage::getModel('mymodule/category')->load($cid);
$collection = Mage::getModel('mymodule/productcollection');
$collection->addCategoryFilter($category_model);
$collection->addAttributeToSort('entity_id','DESC');
$collection->addAttributeToSelect('*');
$collection->printLogQuery(true);
However the above code is still querying the flat data table. What is it I may be doing wrong?
since you already rewrote Mage_Catalog_Model_Resource_Product_Collection simple rewrite
public function isEnabledFlat()
{
if (Mage::app()->getStore()->isAdmin()) {
return false;
}
if (!isset($this->_flatEnabled[$this->getStoreId()])) {
$this->_flatEnabled[$this->getStoreId()] = $this->getFlatHelper()
->isEnabled($this->getStoreId());
}
return $this->_flatEnabled[$this->getStoreId()];
}
to
public function isEnabledFlat()
{
return false;
}
that should fix it :)
To disable flat index for the current request, you can also set the configuration temporarily without saving it as described here:
Mage::app()->getStore()->setConfig(
Mage_Catalog_Helper_Product_Flat::XML_PATH_USE_PRODUCT_FLAT, '0');
It is important that this is done before the product collection has been initialized because that's where the configuration is checked (via isEnabledFlat().
Related
I have a (relatively) basic need in Nova that I can't seem to figure out and I slowly start to feel that I'm approaching things the wrong way.
So, I've got a User, Company, Device and Transfer models and respectively resources, everything pretty default regarding the resource setup.
The schema is the following:
users: id, company_id
companies: id, type_id, name where type_id is pointing to one of three pre-populated types (manufacturer, dealer, client)
devices: id, imei
transfers: id, from_company_id, to_company_id, accepted_at
and Transfer is in a Many-to-Many with Device.
The idea behind the transfers being that Manufacturers transfer to Dealers, Dealers transfer to Clients, so it's really only a one-way thing.
Now the problem occurs at the following crucial point in the logic:
In my Transfer resource pages, I want to show different fields depending on the type of the company the currently authenticated user belongs to. Basically, if the company is:
Manufacturer, then display a DEALER column populated with the transfers' toCompany relation;
Dealer, then display a CONTRAGENT column populated with the transfers' fromCompany or toCompany relations (depending on which mathces the current auth() company)
Client, then display a DEALER column populated with the transfers' fromCompany
All of the described logic works fine with the following code (App\Nova\Transfer.php as is) UNTIL I wanted to finally display the transfer's devices on the details page:
<?php
namespace App\Nova;
use Illuminate\Http\Request;
use Laravel\Nova\Fields\ID;
use Laravel\Nova\Fields\BelongsTo;
use Laravel\Nova\Fields\BelongsToMany;
use Laravel\Nova\Http\Requests\NovaRequest;
class Transfer extends Resource
{
public static $model = \App\Models\Transfer::class;
public static $title = 'id';
public static $search = [
'id',
];
public static $with = [
'fromCompany',
'toCompany'
];
public function fields(Request $request)
{
$company = auth()->company();
if($company->hasType('manufacturer'))
{
$contragentTitle = 'Dealer';
$contragent = 'toCompany';
}
else if($company->hasType('dealer'))
{
//\Debugbar::info($this); //showing empty resource when populating the devices
$contragentTitle = 'Contragent';
$contragent = $this->fromCompany->is($company) ? 'toCompany' : 'fromCompany'; //exception here, since the resource is empty and fromCompany is null
}
else
{
$contragentTitle = 'Dealer';
$contragent = 'fromCompany';
}
$contragentCompanyField = BelongsTo::make("$contragentTitle company", $contragent, Company::class);
if($company->hasType('dealer'))
{
$contragentCompanyField->displayUsing(function ($contragentCompany) use ($contragent){
return $contragentCompany->title() . " (".($contragent == 'toCompany' ? 'Outgoing' : "Incoming").')';
});
}
return [
ID::make(__('ID'), 'id')->sortable(),
$contragentCompanyField,
BelongsToMany::make('Devices') //problematic field, when removed, everything is fine...
];
}
public static function indexQuery(NovaRequest $request, $query)
{
if(auth()->check())
{
return $query->where(function($subQuery){
return $subQuery->where('from_company_id', auth()->company()->id)->orWhere('to_company_id', auth()->company()->id);
});
}
}
public function cards(Request $request)
{
return [];
}
public function filters(Request $request)
{
return [];
}
public function lenses(Request $request)
{
return [];
}
//action is working fine (additional canRun added to avoid policy conflicts)
public function actions(Request $request)
{
return [
(new Actions\AcceptTransfer())->showOnTableRow()->canSee(function ($request) {
if ($request instanceof \Laravel\Nova\Http\Requests\ActionRequest) {
return true;
}
return $this->resource->exists
&& $this->resource->toCompany->is(auth()->company())
&& $this->resource->accepted_at === null;
})->canRun(function ($request) {
return true;
})
];
}
}
Now the strange thing that is happening is that the fields() method gets called multiple times on multiple ajax requests behind the scenes with Nova and when populating the devices relationship table, it gets called without a resource, although a call is never actually needed (as far as I can grasp the mechanics behind Nova) or at least when fetching relationships, you must still have the model information (at least the ID) somewhere to fetch by... So basically, if I'm a user of a dealer company, I can't see the devices that are being transferred (currently throwing a calling is() on null exception).
Now, this happens to be a big problem, since it hinders most of the stuff I need for my transfers, but also generally I don't like my approach so far, so... What would be the right way to achieve this multi-layer resource? Ideally I'd like to define three different transfer resource classes and somehow tell nova which one to use based on the user's company's type (since branching will most probably just grow more complex and therefore uglier as of the current aproach), but I can't figure out the way to do so.
I've also considered moving this entire logic to a separate Nova tool, but I really don't know much about them yet and whether that would be the right option... The only thing stopping me is that I still won't be able to elegantly solve the multi-layer problem and will have to write much of the otherwise useful Nova CRUD logic and views myself...
Any explanations (regarding the multiple calls of fields() and why resource is empty) or general structural recommendations to solve this case would be greatly appreciated! Many thanks in advance!
EDIT:
I was able to circumvent the error by taking advantage of viaResourceId, so instaed of $this I ended up using:
$transfer = $this->id ? $this->resource : \App\Models\Transfer::find($request->viaResourceId);
but the messy code and the unneeded calls still remain an open question. Thanks again in advance!
Here is an example of how I handled this:
public function fields(NovaRequest $request)
{
/** #var \App\Models\User $user */
$user = $this->id ? $this->resource : \App\Models\User::find($request->viaResourceId);
if ($user && $user->whatEver()) {
// display special fields in preview/detail view
return [...];
}
// display for index and if no model is found
return [...];
}
How to explicitly say to route model binding to fetch only related categories? I have my web.php file as follows:
Route::get('/catalog/{category}', [CategoryController::class, 'index'])->name('category.index');
Route::get('/catalog/{category}/{subcategory}', [SubcategoryController::class, 'index'])->name('subcategory.index');
Route::get('/catalog/{category}/{subcategory}/{subsubcategory}', [SubsubcategoryController::class, 'index'])->name('subsubcategory.index');
Subsubcategory controller:
public function index(Category $category, Subcategory $subcategory, Subsubcategory $subsubcategory)
{
$subsubcategory->load('product')->loadCount('product');
$products = Product::where('subsubcategory_id', $subsubcategory->id)->orderByRaw('product_order = 0, product_order')->get();
return view('subsubcategory.index', compact('subsubcategory', 'products'));
}
And model in question:
public function subcategory()
{
return $this->belongsTo(Subcategory::class);
}
public function category()
{
return $this->belongsTo(Category::class);
}
public function getRouteKeyName()
{
return 'slug';
}
It works partially ok. It loads all the slugs, but the problem is, let's say I have Samsung Subsubcategory with it's parent categories like:
catalog/mobile-phones/android/samsung
Whenever I modify url from catalog/mobile-phones/android/samsung to catalog/mobile-phones/ios/samsung it works, where in fact it should not. How to handle this second scenario?
PS: it also applies if I open subcategory and change category slug. But, obviously, if upper level category does not exists, it's going to throw 404.
You may want to explore the docs a bit in regard to explicit route model binding and customizing the resolution logic to get some ideas.
https://laravel.com/docs/8.x/routing#customizing-the-resolution-logic
The following is untested and I'm making some guesses about your table structures, but I think this should give you a basic concept of how you can alter route model binding to fit your needs. The same concept could also be applied to the {subcategory} binding, but with one less relationship check.
App/Providers/RouteServiceProvider.php
public function boot()
{
// ...default code...
// add custom resolution for binding 'subsubcategory'
Route::bind('subsubcategory', function($slug, $route) {
// check to see if category exists
if ($category = Category::where('slug',$route->parameter('category'))->first()) {
// check to see if subcategory exists under category
if ($subcategory = $category->subcategories()->where('slug',$route->parameter('subcategory'))->first()) {
// check to see if subsubcategory exists under subcategory
if ($subsubcategory = $subcategory->subsubcategories()->where('slug',$slug)->first()) {
// success, proper relationship exists
return $subsubcategory;
}
}
}
// fail (404) if we get here
throw new ModelNotFoundException();
});
}
I will note, however, that this makes a number of separate database calls. There may be more efficient ways to achieve the same goal through other methods if optimization is a concern.
I have a backend controller implements Backend\Behaviors\RelationController, I just want to set default values for the related model depending on the parent model values.
I have tried the following: model.beforeCreate, formExtendFields but no luck.
Thank you all.
After whole day of searching, I found the solution, it is documented no where on OctoberCMS website, I inspected the source file Backend\Behaviors\RelationController, after that I came with the below solution.
You should implement relationExtendViewWidget on your controller, then you can access the model from: $widget->model, something like below:
class Members extends Controller
{
public $implement = [
'Backend\Behaviors\RelationController',
];
public function relationExtendViewWidget($widget, $field)
{
$member = Member::findOrFail($this->params[0]);
$widget->model->course_id = $member->course_id;
$widget->model->member_id = $member->id;
}
public function relationExtendManageWidget($widget, $field)
{
$member = Member::findOrFail($this->params[0]);
$widget->model->course_id = $member->course_id;
$widget->model->member_id = $member->id;
}
}
I believe this should be documented somewhere on OctoberCMS documentation
I have looked every where in the administrator\components\com_k2 folder but am not able to find the code that saves a new item\article in K2. I checked the item.php file under models folder. No luck.
I need to override the K2 item save method.
I need a know the exact method that saves the Item's title and alias into the #__K2_content table.
I have to duplicate the K2 items in joomla articles on save and remove on trash/delete.
I have successfully been able to override the K2 core code. But am unable to find the right code to override. (override method is here)
The table that stores the K2 items (at least in the latest K2 version - 2.6.5) is #__k2_items, not #__k2_content.
I went through the code, it looks like K2 uses Joomla's methods: see administrator/components/com_k2/controllers/item.php - line 24: function save(). Everything is extended from Joomla classes.
class K2ControllerItem extends K2Controller
{
public function display($cachable = false, $urlparams = array())
{
JRequest::setVar('view', 'item');
parent::display();
}
function save()
{
JRequest::checkToken() or jexit('Invalid Token');
$model = $this->getModel('item');
$model->save();
}
.....
}
The K2 controller: /administrator/components/com_k2/controllers/controller.php
...
else if (version_compare(JVERSION, '2.5', 'ge'))
{
class K2Controller extends JController
{
public function display($cachable = false, $urlparams = false)
{
parent::display($cachable, $urlparams);
}
}
}
...
#Shaz, you gave me the right direction to look into.
in com_k2\controllers\item.php
this $model->save();saves the data.
The function save() is in the com_k2\models\item.php file, where there are two lines that capture the data.
$row = JTable::getInstance('K2Item', 'Table');
this initiates the $row, while
if (!$row->bind(JRequest::get('post')))
this populates $row.
So now $row contains all the variable values.
Now, this if (!$row->store()) saves the data.
I will use $row to save the same for the Joomla! articles in com_content.
Feels Good :)
i have a controller having following script
class get extends CI_Controller
{
function get_password()
{
$this->load->model('fetch_model');
$user_pass=$this->fetch_model->get_password();
$data['user_pass'] = json_encode($user_pass);
echo $data['user_pass'];
}
}
and a script on view page as this
function get_password(){
$.post("<?php echo base_url();?>admin.php/get/get_password",function(data)
{
for(i=0;i<data.length;i++)
{
$('#password').val(data[i].password);
$('#username').val(data[i].username);
}
},"json");
}
now if i use the following script in model then the ajax post is working perfectly..
class fetch_model extends CI_Model
{
function get_password()
{
return $this->db->query("SELECT * FROM td_admin_user")->result_array();
}
}
but when i change the model script into this, then the ajax script isnt working
class fetch_model extends CI_Model
{
function get_password()
{
foreach($this->db->query("SELECT * FROM td_admin_user")->result() as $r_pass)
{
$pass=$r_pass->password;
$user=$r_pass->username;
}
$user_pass=array('username'=>$user,
'password'=>$pass);
return $user_pass;
}
}
but to be very frankly, i need to send data in the following way
$user_pass = array('username'=>$user, 'password'=>$pass);
and not as result_array()
so please help me in this context, thanks in advance
This is not a bug! Did you even look at what type of data is returned in each call?
In your JS, you are using:
$('#password').val(data[i].password);
Where data is an indexed array - exactly what is returned by ->result_array()
But you want it to be a key based array:
$user_pass=array('username'=>$user, 'password'=>$pass);
If that's the case, you should forget using the for loop on JS (specially if your query only results in one row) and use
$('#password').val(data.password);
Instead.
Also look at console.log (if you are using firebug/developer tools) and try print_r or var_dump on the PHP side to understand the data formats you are returning.