I'm using Laravel and trying to grab info from a foreign relationship of a member of a pivot. ERD is below. I have set up a many to many relationship so that I can grab property restrictions with $property->restriction and the other way around. What I'm struggling with is how I can add in the restriction type description when I grab this info.
$property->restriction gives me the type_id
array(
'id' => '1',
'description' => 'Fish only',
'restriction_types_id' => '1',
'pivot' => array(
'property_id' => '17',
'restriction_id' => '1'
)
)
but I want something like this with the restriction_type description instead of its id.
array(
'id' => '1',
'description' => 'Fish only',
'restriction_type' => 'Animals',
'pivot' => array(
'property_id' => '17',
'restriction_id' => '1'
)
)
My DB tables
╔═════════════╗ ╔════════════════════╗ ╔════════════════════╗
║ property ║ ║property_restriction║ ║restriction ║
╟─────────────╢ ╟────────────────────╢ ╟────────────────────╢
║id ║——————║property_id ║ ┌──║restriction_id ║
║description ║ ║restriction_id ║───┘ ║desctiption ║
╚═════════════╝ ╚════════════════════╝ ┌──║restriction_type_id ║
│ ╚════════════════════╝
│
│
╔═══════════════════╗ │
║restriction_type ║ │
╟───────────────────╢ │
║id ║────┘
║description ║
╚═══════════════════╝
I believe the way you are currently trying to access the data is through a dynamic property. It may be possible to just type out $property->restriction->restriction_type.
You can also try to eager load it in one of the ways below:
$property->with('restriction','restriction.restriction_type')->get();
or
$property
->with('restriction')
->with('restriction.restriction_type')
->get();
If neither of these ways work I would research eager loading and the dynamic properties of Laravel and see if there is something that needs to get altered in your relationships.
Related
In Laravel I'm running the following
PlateOrder::firstOrCreate(
['internal_order' => $internalOrder],
['internal_order' => $internalOrder,
'order_number' => $order->order_number, 'product' => $line_item->name,
'img_url' => $plate->template, 'template' => 1,
'order_date' => $fixedDate,
'qty' => $line_item->quantity,
'custom1' => $firstLineTrimmed,
'custom2' => $secondLineTrimmed,
'created_at' => $order->created_at,
'r' => $plate->r, 'g' => $plate->g, 'b' => $plate->b,
'spacing' => $plate->spacing, 'top' => $plate->top ]
);
Using firstOrCreate the first parameter internal_order will check if it exists. If it exists it'll update it if it doesn't it should create it.
However in my database it's causing it to write duplicates.
84826 142544 1425441HIGHMARK2 08-25-2020 HIGHMARK VINYLS NULL NULL Colorado Plate Hat - Flex Fit / L/XL (7 1/4" - 7 5/8") / Black FF prt_Colorado.ai NULL 2 NULL 2020-08-25T05:47:14-07:00 1 0 77 41 38 86
84827 142544 1425441HIGHMARK2 08-25-2020 HIGHMARK VINYLS NULL NULL Colorado Plate Hat - Flex Fit / L/XL (7 1/4" - 7 5/8") / Black FF prt_Colorado.ai NULL 2 NULL 2020-08-25T05:47:14-07:00 1 0 77 41 38 86
It creates an identical record except for the id. Is this a bug or am I missing something?
Thanks to #Derek Pollard, I dug into this and it indeed is a mysql race condition.
To fix this I ended up making internal_order column a unique id. Any attempt to create a double will error out.
Here are some references to:
https://github.com/spatie/first-or-create-racing-condition-demo
https://gist.github.com/troatie/def0fba42fcfb70f873b7f033fbe255f
If you want the record to be updated if it doesn't already exist you should use updateOrCreate as firstOrCreate will not update the existing record but rather will return it. As for it inserting the duplicate records, it must not be finding the record that has the same internal_order value. Is it possible that there is trailing or leading whitespace in the existing entry that is causing it not to be found? Also, there is no reason to repeat the internal_order values in the second array as it will use the values provided in the first array automagically.
PlateOrder::updateOrCreate(
['internal_order' => $internalOrder],
[
'order_number' => $order->order_number,
'product' => $line_item->name,
'img_url' => $plate->template,
'template' => 1,
'order_date' => $fixedDate,
'qty' => $line_item->quantity,
'custom1' => $firstLineTrimmed,
'custom2' => $secondLineTrimmed,
'created_at' => $order->created_at,
'r' => $plate->r,
'g' => $plate->g,
'b' => $plate->b,
'spacing' => $plate->spacing,
'top' => $plate->top ]
);
[id] => 1
[name] => About us
[type] => page_link
[link] =>
[page_id] => 1
[parent_id] =>
[lft] => 2
[rgt] => 3
[depth] => 1
[created_at] => 2017-02-05 07:25:22
[updated_at] => 2017-02-05 07:25:38
[deleted_at] =>
When adding 'Type' as 'Page Link' and selecting page from dropdown. Page link is not being saved in database.
In Backpack\NewsCRUD, when a link to a page is stored, we don't store the actual link, as that might change it time. We store the page_id. So yes, link will be empty.
When you output your menu, for page links get the link when rendering. If you're using Backpack\PageManager, for example, instead of echoing $menu_item->link, echo $menu_item->page->getPageLink().
Hope it helps.
I'm using Magento EE 1.14.2. I'm trying to export all attributes with its type in web service. All is working fine but weight attribute. Its a system attribute. The attribute type should be text but it's showing me weight. I checked fresh magento also. My code is,
$attribute = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'weight');
print_r($attribute);
My o/p is like,
[attribute_id] => 80
[entity_type_id] => 4
[attribute_code] => weight
[attribute_model] =>
[backend_model] =>
[backend_type] => decimal
[backend_table] =>
[frontend_model] =>
[frontend_input] => weight
[frontend_label] => Weight
[frontend_class] =>
[source_model] =>
Any hints or Ideas ,
Thanks.
In the ContentModelCategory class, in the getItems() method, I have the following settings for the pagination object:
JPagination Object(
[limitstart] => 0
[limit] => 5
[total] => 14
[prefix] =>
[_viewall:protected] =>
[_additionalUrlParams:protected] => Array()
[_errors:protected] => Array()
[pages.total] => 3
[pages.current] => 1
[pages.start] => 1
[pages.stop] => 3
)
After which I return the articles.
When I look at the page, I indeed have pagination with 3 pages.
Problem is that every page has the same 5 articles.
How can I fix this?
Regards,
Matthijs
I am trying to organize the category page by dividing the products into subcategories. When I loop through the subcategories (on catalog\product\list.phtml) with the code:
$child_cat = Mage::getModel('catalog/category')->load($child_id);
$_productCollection = Mage::getResourceModel('catalog/product_collection')->addCategoryFilter($child_cat);
and then proceed to loop through the products...
foreach ($_productCollection as $_product):
It does not seem to differentiate between products that are set to be visible and products that are not (both are shown). It also does not show images, prices, or any other information. The only correct information I get is the product URL.
Why is this happening and how do I fix it?
By default, when you load a product collection you'll get a bare amount of information about the products. Ex:
Array
(
[entity_id] => 9
[entity_type_id] => 4
[attribute_set_id] => 4
[type_id] => simple
[sku] => DLKJFER343
[has_options] => 0
[required_options] => 0
[created_at] => 2012-12-07 16:04:58
[updated_at] => 2012-12-11 16:21:37
[cat_index_position] => 0
[stock_item (Varien_Object)] => Array
(
)
)
You need to explicitly tell Magento to load extra information or to filter by a value like this:
$_productCollection = Mage::getResourceModel( 'catalog/product_collection' );
// Filter by enabled products
$_productCollection->addAttributeToFilter( 'status', 1 );
// Load all product information
$_productCollection->addAttributeToSelect( '*' );
$_productCollection->addCategoryFilter( $category );
Now you'll see something like this (using $_product->debug() to dump out some information):
Array
(
[entity_id] => 3
[entity_type_id] => 4
[attribute_set_id] => 4
[type_id] => simple
[sku] => DLKJFER343
[has_options] => 0
[required_options] => 0
[created_at] => 2012-12-05 18:47:39
[updated_at] => 2012-12-11 16:20:25
[cat_index_position] => 0
[status] => 1
[visibility] => 4
[enable_googlecheckout] => 1
[tax_class_id] => 2
[is_recurring] => 0
[weight] => 3.0000
[price] => 534.2500
[name] => Sample Product
[url_key] => some-product
[is_returnable] => 2
[msrp_enabled] => 2
[msrp_display_actual_price_type] => 4
[image] => /w/r/something.png
[small_image] => /w/r/something_sm.png
[thumbnail] => /w/r/something_th.png
[options_container] => container2
[url_path] => some-product.html
[image_label] => One image label
[small_image_label] => Another image label
[thumbnail_label] => An image label
[description] => Long winded blah, blah, blah.
[short_description] => Blah, blah, blah.
[stock_item (Varien_Object)] => Array
(
)
)
The media gallery information (labels, etc) is a different beast and must be specifically requested through the getMediaGalleryImages() method of the Mage_Catalog_Model_Product object.
HOWEVER this method will return NULL if called while looping through a product collection. Oddly, you cannot access this data without explicitly loading the product model (for reasons I won't go into in this response). So, we need to try something like this:
$product = Mage::getModel( 'catalog/product' )->load( $_product->getId() );
$images = $product->getMediaGalleryImages();
There's a performance hit doing this in your collection loop though, so be careful.
EDIT:
Mage_Catalog_Block_Product->getLoadedProductCollection()
Long, long, long story short (this method digs deep)... as far as I can tell getLoadedProductCollection() will only show attributes which have Used In Product Listing set to Yes.
The reason resides in Mage_Catalog_Model_Resource_Config...
public function getAttributesUsedInListing()
{
// ... some code above omitted...
->where('main_table.entity_type_id = ?', (int)$this->getEntityTypeId())
// THIS RIGHT HERE IS WHY
->where('additional_table.used_in_product_listing = ?', 1);
return $adapter->fetchAll($select);
}