It is necessary for me to get a list of all the meanings an attribute of “color”. when i use this code
$name='color';
$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter($name)->getFirstItem();
$attributeId = $attributeInfo->getAttributeId();
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
$attributeOptions = $attribute ->getSource()->getAllOptions(false);
In that case I get that kind of list:
(
[0] => Array
(
[value] => 6
[label] => blueAdmin
)
[1] => Array
(
[value] => 5
[label] => coralAdmin
)
[2] => Array
(
[value] => 3
[label] => redAdmin
)
[3] => Array
(
[value] => 4
[label] => limeAdmin
)
)
It is the list of all meanings which are displayed in administration's part of website. How can I get a list of all meanings of attributes which are displayed in the shop not in administration's part of website?
Thank you.
You can get the attribute option values for a particular store by setting the store ID on the attribute prior to calling getAllOptions(), e.g.,
$attributeOptions = $attribute->setStoreId(1)->getSource()->getAllOptions(false);
gets the option values for the store with ID 1. You can get the ID of the current store with
Mage::app()->getStore()->getId();
So something like this should get you what you want:
$storeId = Mage::app()->getStore()->getId();
$attributeOptions = $attribute->setStoreId($storeId)->getSource()->getAllOptions(false);
Related
Current xpath: Product/ProductMultimediaObject/MultimediaObject
Returns:
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[ContentType] => application/pdf
[Date] => 2014-11-01 01:20:35
[Description] => Leaflet
)
)
)
But I'm currently just hoping that the found MultimediaObject is the correct one - which is not. I need to get the MultimediaObject where Description has a specific value.
What I've tried:
Product/ProductMultimediaObject/MultimediaObject[Description/text() = 'WhatIWant']
Product/ProductMultimediaObject/MultimediaObject/Description[text() = 'WhatIWant']
Your Desciption is an attribute. Try
Product/ProductMultimediaObject/MultimediaObject[#Description = 'WhatIWant']
or, for short,
//MultimediaObject[#Description = 'WhatIWant']
how to get the values of multidimensional array.
code//
Array
(
[0] => Array
(
[City] => Array
(
[title] => Nagpur
[id] => 20299
)
[Branch] => Array
(
[0] => Array
(
[id] => 8
[country_id] => 41
[state_id] => 102
[city_id] => 20299
[title] => Geotech Services Ltd.
)
)
)
[1] => Array
(
[City] => Array
(
[title] => kolapur
[id] => 20300
)
)
)
Here I want to get the only values if cities(title) so plz tel me how to fetch in cakephp..
Thanks in advance
Thus you finding as recursive so all related data is fetched with City.
To disable this recursion, you need to set $recursive = -1;
You can do this by many ways--
place $this->City->recursive = -1; before find operation
place recursive => -1 on find overation
Globally set public $recursive = -1; on AppModel.php
And if you read from this array its simple and basic programming operation...
foreach($cities as $city){
echo $city['City']['title']
}
using cakePHP what you're trying to do is usually achieved using Hash
Assuming $your_array contains the data, then you have to do:
$cities = Hash::extract($your_array, '{n}.City.title');
I am having problems with working with multidimensional array data.
I have 2 tables (maincategories and categories) which I joined.
Table Maincategories:
id, maincategory
Table Categories:
id, maincategory_id, category
$this->db->select('m.id, m.name_en AS maincategory')
->select('c.name_en AS category')
->from('categories AS c')
->join('maincategories AS m', 'c.maincategory_id=m.id', 'left');
$query = $this->db->get();
This results in all 4 categories being listed as below.
[3] => stdClass Object
(
[id] => 7
[maincategory] => Career
[category] => Business Sales
)
[4] => stdClass Object
(
[id] => 1
[maincategory] => Accommodation
[category] => Camping
)
[5] => stdClass Object
(
[id] => 6
[maincategory] => Accommodation
[category] => Hostels
)
[6] => stdClass Object
(
[id] => 7
[maincategory] => Career
[category] => Career Events
)
4 unique categories are in the list while 2 of them belong to maincategory: Accommodaton and the other 2 categories belong to maincategory: Career. What I wanted to achieve is that each maincategory is only listed ONCE and the categories shall fall under that main_category. Something like this:
[6] => stdClass Object
(
[id] => 7
[maincategory] => Career
[category] => array(Career Events, Business Sales, .....)
)
[7] => stdClass Object
(
[id] => 7
[maincategory] => Accommodation
[category] => array(Hostels, Camping, .....)
)
The array dump will maybe/probably look different in its end result but I hope you understand what I am trying to achieve. I read quite a bit about transforming multidimensional arrays. I just can't get my head around it yet. Thanks a lot for any direction!
Try like this:
function fetch_all(){
$data = array();
$first = $this->db->select('id, maincategory')->get('maincategory')->result_array();
if( is_array( $first ) && count( $first ) > 0 ){
foreach( $first as $key => $each ){
$data[$key]['category_id'] = $each['id']; #main category id
$data[$key]['maincategory'] = $each['id']; #main category name
$second = $this->db->select('category')->where_in('maincategory_id', $each['id'])->get('categories')->result_array();
$data[$key]['category'] = $second; #all sub-category(s) array
}
}
}
instead of using so many db calls, try using the underscore.php library http://brianhaveri.github.io/Underscore.php/
I've outlined a few examples of how to use it for this case here: http://codebyjeff.com/blog/2012/08/no-more-machine-gunning-use-underscore-php
Edit
I got a Joomla ticketing module, that displays at a certain part of the page, the current tickets of the current user. I want to create user groups, in which the group members can view all of the tickets belongin to that group. I thought the easiest way is to create Joomla groups, assign the users to those, and to when a user loges in, it can see all of the tickets in its group. I added my code to the start of the function, but something is wrong... For every user (currently the "Registered" ones) displays the same result, that of the last users tickets, and I don't know why: Here is the code:
function gTickets()
{
$user =& JFactory::getUser();
$user_id = (int) $user->get('id');
//get user_group_id from db based on current users id
{...}
//get all users with that user_group_id
$db1->setQuery($db1->getQuery(true)
->select('*')
->from("#__user_usergroup_map")
->where("group_id = '$groupss'")
);
$groupss1=$db1->loadRowList();
$return1=array();
// for every user_id
foreach ($groupss1 as $keya)
{
$user_id = $keya[0]; // the id of users
$where = "";
if ($this->is_staff)
$where .= " AND t.`staff_id`='".$user_id."'";
else
$where .= " AND t.`customer_id`='".$user_id."'";
$tickets = $this->_getList(
"SELECT t.id, t.subject, t.last_reply_customer, s.name AS status_name FROM
#__rsticketspro_tickets t LEFT JOIN #__rsticketspro_statuses s ON
(t.status_id=s.id) WHERE 1 $where ORDER BY `last_reply` DESC", 0,
$this->params->get('tickets_limit', 3));
print_r($tickets);
return $tickets;
}
I got some questions, that I didn't know how to seach for...
what is the letter dot fieldname in the sql query? eg:
SELECT m.ticket_id, m.message FROM #__ticket_messages m WHERE m.user_id !='".$user_id."
what does the "m" mean before the WHERE?
what does the "1" do here: WHERE 1 $where
Also, I looked in the ACL managers, but could not make it work with this code.
Edit: Thanks for the fast answers! I got 1 more, and I think it's an easy one, but I can't get it to work...
If I print the content of the $ticket array into another array, I get an array with multiple arrays. That is why my code is not working... The array i'm getting is:
Array (
[0] => stdClass Object (
[id] => 1
[subject] => use1
[last_reply_customer] => 1
[status_name] => open
)
)
Array (
[0] => stdClass Object (
[id] => 3
[subject] => use2
[last_reply_customer] => 1
[status_name] => open
)
[1] => stdClass Object (
[id] => 2
[subject] => use2
[last_reply_customer] => 1
[status_name] => open
)
)
I would like the array to look like this:
Array (
[0] => stdClass Object (
[id] => 1
[subject] => use1
[last_reply_customer] => 1
[status_name] => open
)
[1] => stdClass Object (
[id] => 3
[subject] => use2
[last_reply_customer] => 1
[status_name] => open
)
[2] => stdClass Object (
[id] => 2
[subject] => use2
[last_reply_customer] => 1
[status_name] => open
)
)
Thanks!
Edit: is all of this complicated to achieve?
Look into ACL to get that part working, but to answer the 3 questions you posted at the bottom of your question:
1) the letter.field_name in the query represents the table alias.field_name in the query. The alias (m) makes it easier so you don't have to keep typing the full table name each time.
2) The m before the WHERE is actually setting the table alias, but with lazy syntax. It should say:
SELECT m.ticket_id, m.message FROM #__ticket_messages AS m
3) The "WHERE 1" is the SQL way of saying "if (true)". Since they are immediately appending the $where clause afterwards "WHERE 1 $where", the resulting query looks like this:
WHERE 1 AND t.staff_id = '" . $user_id . "'
Did you code this originally? or is it a component that you picked up somewhere? It should be using the query builder instead of direct SQL like it currently is, to make it more portable and more clear as to what it is doing.
How can I get a list of store groups under a website in Magento and then a list of stores from that store group?
Try this to get the objects directly
Mage::app()->getWebsites(); < in file > app/code/core/Mage/Core/Model/App.php:920
Mage::app()->getStores(); < in file > app/code/core/Mage/Core/Model/App.php:834
iterate over to get the needed scope of one specific website or store
foreach (Mage::app()->getWebsites() as $website) {
foreach ($website->getGroups() as $group) {
$stores = $group->getStores();
foreach ($stores as $store) {
//$store is a store object
}
}
}
For the future if you have similar questions here's how i discovered those answers within 60 seconds. First i grep for method names or similar method names with space before method name to see where the methods are defined
grep ' getStores' app/code -rsn
grep ' getWebsites' app/code -rsn
Second step is grep for usage samples to see how they are meant to use by core developers. For that i add >methodName to grep and this gives me list of files where this method is called and this will give us place to look for examples:
grep '>getWebsites' app/code -rsn
Anton's answer, while correct, may be re-inventing the wheel just a bit. There is already a facility in the Magento Core to retrieve this sort of data.
You can retrieve a list of all websites, and their "children" using this:
Mage::getSingleton('adminhtml/system_store')->getStoresStructure()
You can also pass an array of websiteIds, storeIds, or storeGroupIds to the function, to filter the list:
public function getStoresStructure($isAll = false, $storeIds = array(), $groupIds = array(), $websiteIds = array())
Example output:
Array
(
[1] => Array
(
[value] => 1
[label] => Main Website
[children] => Array
(
[1] => Array
(
[value] => 1
[label] => Madison Island
[children] => Array
(
[1] => Array
(
[value] => 1
[label] => English
)
[2] => Array
(
[value] => 2
[label] => French
)
[3] => Array
(
[value] => 3
[label] => German
)
)
)
)
)
)
There is a similar one used to populate the "Store Scope" dropdowns and multi-selects all across the admin section.
Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(false, true)
Array
(
[0] => Array
(
[label] => All Store Views
[value] => 0
)
[1] => Array
(
[label] => Main Website
[value] => Array
(
)
)
[2] => Array
(
[label] => Madison Island
[value] => Array
(
[0] => Array
(
[label] => English
[value] => 1
)
[1] => Array
(
[label] => French
[value] => 2
)
[2] => Array
(
[label] => German
[value] => 3
)
)
)
)
To discover this, I located a multi-select on the Admin that has the data I wanted, then I turned on template hints to find out which block class was responsible for rendering it: Mage_Adminhtml_Block_Cms_Page_Edit_Form. Knowing this, I found the class in the codebase,(app/code/core/Mage/Adminhtml/Block/Cms/Block/Edit/Form.php) and located the part that creates the input by searching for its label ("Store View"). This showed me how the input's values were being provided:
$field =$fieldset->addField('store_id', 'multiselect', array(
'name' => 'stores[]',
'label' => Mage::helper('cms')->__('Store View'),
'title' => Mage::helper('cms')->__('Store View'),
'required' => true,
'values' => Mage::getSingleton('adminhtml/system_store')->getStoreValuesForForm(false, true),
));
The Mage::getSingleton('adminhtml/system_store') points to the class Mage_Adminhtml_Model_System_Store, where I found a variety of similar methods that can also be useful. Have a look for yourself.