I have created a collection ($myCollection) in Magento and I get its data using this code:
$data = $myCollection->getData();
That works, but when I look at the $data array, I see that some columns are just missing and the strange thing is, only sometimes, NOT always. To figure out whats going on, I fetched the underlying SQL using this code:
$select = $myCollection->getSelect()->__toString();
Running this in my MySQL client returns all columns, nothing is missing. Any idea what the problem might be?
Thanks!
this is correct, not always full data is included, especially for eav models. You can read about it on
http://www.magentocommerce.com/knowledge-base/entry/magento-for-dev-part-8-varien-data-collections
to load all of them, additionally use:
$collection->addAttributeToSelect('*');
Related
I simply want to save a collection to the database after processing it in my application.
$data = SomeModel::all();
$data = DataProcessor::process($data);
//Now I want to save the result back to the database...
//This doesn't work since it's a collection so you get the error:
//"Method Illuminate\Database\Eloquent\Collection::save does not exist."
$data ->save();
//This works but is not feasible due to the amount (thousands) of records needed to be updated
foreach($data as $dat){
$dat->save();
}
//This doesn't work because I'm updating, not inserting new records.
//So I get integrity constraint violation on id
SomeModel::insert($data);
I feel this should be a common practice with a pretty straightforward solution but I've been looking around and haven't found anything.
According docs, you could try upserts: https://laravel.com/docs/8.x/eloquent#upserts
Im trying to figure out the best way to paginate in laravel when i am working with big datasets, the method i am using now is overworking the memory. this is what i am doing: i query from multiple tables, map the results etc, then merge multiple collections, and only after that do i paginate it, i realized though that this is a really bad way of going about this, because i am querying way to much data for no reason, and as the data grows the slower it will become.
the question is what would be the correct way?
i thought maybe i would paginate and then work with the data, the issue is that for instance if i was trying to sort all the merged data by date i wouldn't be getting the proper results because one table may have less data then the other...
here is some of the code to help with clarifying the question
first i will query two tables orders table and transactions table
$transactions = Transaction::all();
$orders = Order::all();
then i will send this all to a action to map it all and format it the way i need to with laravel resources
return $history->execute(['transactions' => $transactions, 'orders' => $orders]);
what this action does is simply map all the transactions and return a resource, then it will map all the orders and return resources for them as well, after which it will merge it all and return the result.
then i take all of this and run it through a pagination action which uses the Illuminate\Pagination\LengthAwarePaginator class.
the results are exactly the way i want them its simply a memory overload.
i tried paginating first instead of doing
$transactions = Transaction::all();
$orders = Order::all();
i did
$transactions = Transaction::orderBy('created_at' 'desc' )->paginate($request->PerPage);
$orders = Order::orderBy('created_at' 'desc' )->paginate($request->PerPage);
and then run it through the action that returns resources.
there are a few issues doing this, firstly, i will no longer get back the pagination data so the app won't know the current page or how many records there are, secondly, if for example the orders table has 2 records, and the transactions table has 10 records, event though the dates of the orders table is after those of the transactions table they will still get queried first.
Use chunk method or paginate
Read documentation.
Using Laravels Baum package for nested sets I try to fetch the relational model on getAncestorsAndSelf() but does not work
$tree = \App\GroupDeprecator::where('id', $this->team_id)->with('lead')->first();
dd($tree); //this one shows the user object
dd($tree->getAncestorsAndSelf());//this one does not shows the user object
What went wrong in this case?
How you solved it? the code looks fine and should work, unless it has been manually updated in the database, I would do:
GroupDeprecator::rebuild(true)
to update the lft, rgt and depth columns
I'm able to fetch information for whatever table I need the problem here is the update, insert, and delete records, is not working..
I have read the Joomla doc's but even doing the simplest update queries are not working... so here is my code:
UPDATE:
// I'm getting the data from an array
if (!empty($_POST['data'])) {
$getData = json_decode($_POST['data'], true);
}
// after this line I have a foreach for the array
// in the foreach I have a few IF's
// ether if the result from IF is True or False
// in both I have similar queries
// So let say IF return true;
// Lets prepare the Data using OBJECT's
$object = new stdClass();
$object->product_id = $v['product_id'];
$object->product_name = $v['product_name'];
$object->product_price = $v['product_price'];
$object->product_number = $v['product_number'];
$result = JFactory::getDbo()->updateObject('#__tienda_product', $object, 'product_number');
// that should Update my tables but it doesn't ... now my table has about 21 columns
// but I only need to update 4 columns base on the column product_number
You might have notice the $v['product_id'] where $v is from the foreach, the foreach and the first query are working fine, I did a few echo's before moving to the Update part just to make sure I'm getting the correct data in a format that it should be... any way... the Update part is not working... so I thought it may be because of my table I when to use one table from the regular instalation of joomla... but it still the same no result on the update...
Does any one know how to "update" a table using Joomla CMS as framework?...
Remember I want to use the updateObject() method...
Why don't I use Joomla Framework instead of the CMS's library?
Well, I can give you a few examples, let say:
a business man want a simple basic report, he doesn't care about managing the site he has people to do that, he also gets a sales report, but he doesn't trust the crew just yet and he want to able to see and compare the result, and he needs and standalone without all the fancy tools that joomla has and for that he need an standalone app that can give that kind of report... ok I might have gone a bit far, but the idea is to have basic, simple, easy to read reports and updates, and that is why I have started this project.
Again, I already went to joomla and read the docs but following their examples it just not working... now, am I have to declear all of the columns even if they don't need to be update? or am I missing an execution for the query which in joomla doesn't mention any executions when using $object() only when using regular queries SQL... the idea is to use $object() ...
Thank you for taking the time.
You have defined the updateobject as a variable, and are not calling it. try changing this:
$result = JFactory::getDbo()->updateObject('#__tienda_product', $object, 'product_number');
to this:
JFactory::getDbo()->updateObject('#__tienda_product', $object, 'product_number');
Also, on a side note, you should not use $_POST and instead should use JInput
$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')
->setCodeFilter('modellijn')
->addAttributeToFilter('brand', 114)
->getFirstItem();
Hi there. I would like to know the right syntax for the following:
Select the attribute modellijn where the attribute brand=114. The above syntax gives back an error. I have been searching for 2 days now for the right syntax but no result so far unfortunately.
I am hoping someone here is willing to help me!
So the first problem I see here is... that you are attempting to select the attribute for a particular brand. Attributes do not in fact have brands (nor modellijns).
Can you please clarify the use case? Are you trying to get the modellijn (that's fun to type) value of all products with brand 114? What is your expected output?
Or, if you're more comfortable as such, what would be the SQL query you'd expect to see generated?
Thanks,
Joseph Mastey
Okay, based on your update, I just wanted to clarify a few things.
Attribute sets have attributes
Attributes have options (sometimes)
Products have attribute values
Categories have products
If you just need to find the modellijin of a particular product, then you just need to ask for it. If you have a single product, this should do the trick:
$product = Mage::getModel("catalog/product")->load($id); // Magento does this for you in some cases
$product->getModellijn(); // this will return your value
$product->getAttributeText('modellijn'); // IIRC, this works for <select> type attributes
If you're loading products from a collection, you need to make sure to tell Magento that you want to load that attribute too. Selecting everything from EAV is even more expensive than selecting everything from a standard, normalized database. Therefore, Magento expects you to tell it what you need.
$collection = Mage::getModel("catalog/product")->getCollection();
$collection->addAttributeToFilter("brand", 114); // limit for brand
$collection->addAttributeToSelect("modellijn"); // * also works but is slow
foreach($collection as $product) {
$product->getModellijn(); // just as above
$product->getAttributeText('modellijn');
}
Let me know if that does it for you. If it doesn't, please amend your post above to include a more complete SQL statement, and if possible more information about where you are using this data. That will help me understand the context within which you're executing the code.