Using WillCascadeOnDelete on ASP.MVC 5 - asp.net

From the code snippet below :
modelBuilder.Entity<Product>()
.HasMany(e => e.ProductPictures)
.WithRequired(e => e.Product)
.WillCascadeOnDelete(false);
Which is WillCascadeOnDelete referring to?
Product has
-ProductID
-ProductNumber
-ProductDescription
-Cost
-UnitPrice
-OnHandQty
-StartDate
-CreateDate
ProductPicture has
-ProductPictureId
-ProductId
-IsThumb
-SortOrder
-ProductPictureNote
which means that ProductPicture is the dependent one. Is WillCascadeOnDelete referring to Product? meaning that if Product is deleted then the ProductId property of the ProductPicture will be set to null.

"Cascading delete" is a configuration of a relationship, not of an entity/table. Hence WillCascadeOnDelete is a method of ascadableNavigationPropertyConfiguration.
It means that if a Product deleted from the database, it's ProductPictures should not be deleted together with the Product. That's a property of this specific relationship, not of the Product table.

Related

Woocommerce: Translate/change order item meta key before output

There's an action called 'woocommerce_checkout_create_order_line_item' where you can use the method 'add_meta_data(meta_key, meta_value)' to add meta data to the order item.
Every example did use this syntax: add_meta_data(__('Some key name', 'woocommerce'), $value);
This is perfect as long as the admin and the customer have set the same language and the field is only informal.
But I have to process the field programmatically later. So the key must always be the same, no matter which language is used.
So I use a simple string, but it won't be translated in the admin order area, on the Thank-You page, in the shopping cart etc. anymore.
How can I override the automatic output of the order item meta data? I've tried to hook i.e. into 'woocommerce_before_order_itemmeta($item_id, $item, $product)' (Admin order page), but there I can only delete $item->delete_meta_data('my_key'), I cannot add temporary meta keys => $item->add_meta_data(__('Translated key'), $value);
I also tried:
$item->set_meta_data(array('id' => 0, 'key' => __('Translated key'), 'value' => 'test123'));
print_r($item->get_meta_data());
Thanks!
There is no need to do the translation in the meta key.
What you could do is use the hook woocommerce_order_item_display_meta_key. There you can catch your key and output a tranlation for display.
Also you can filter keys to not display all custom meta keys if you have some keys that should not show up in the order details, but are for internal use only. This can be done with woocommerce_hidden_order_itemmeta
Here is a good post on working with order item meta data: https://www.ibenic.com/manage-order-item-meta-woocommerce/

Symfony2 - related entity contains 'isDefault' property how to use in related entity form

I have 2 related entities, e.g. Book and Publisher (Book has one publisher, publisher has many books).
When editing\adding a Book I want to present a select of the Publishers.
Publishers has a property 'isDefault' on of the Publisher records will be marked as isDefault TRUE.
How do I make use of this in my add/edit form to pre-select the default Publisher?
I would recommend injecting publisherRepository as a service into your form.
And then declare a field something like this:
$builder->add('publishers', 'choice', array(
'choices' => $this->publisherRepository->findAll(),
'data' => $this->publisherRepository->findOneBy(['isDefault' => true]),
));

cakephp 3 deeper associations

I'm trying to find the right way to handel the next tabel setup.
Products is my main table and contains an id.
UsersCarts has an field product_id and with belongsTo it also loads the products.
So far no problem. I also have an table ProductsNettoDeals where I store possible netto prices. This table has it's own id an an product_id for the hasOne relation when loading products in the productcontroller.
When I load the UsersChart it loads it belongsTo products, but I also want the association with the table ProductsNettoDeals, but this table is connected with products through the id (products.id = products_netto_deals.product_id). I cant find the right syntax or association for the querybuilder. What I have so far:
// src/Model/Table/UsersCartsTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\ORM\Query;
use Cake\Validation\Validator;
class UsersCartsTable extends Table
{
public function initialize(array $config)
{
$this->addBehavior('Timestamp');
$this->belongsTo('Products');
$this->belongsTo('ProductsNettoDeals');
}
public function validationDefault(Validator $validator)
{
return $validator
->notEmpty('user_id', 'A user id is required')
->notEmpty('product_id', 'A product id is required');
}
public function findCart(Query $query, array $options)
{
$query
->where([
'user_id' => $options['user_id'],
//'active' => '1'
])
->contain(['Products','ProductsNettoDeals'])
//->select(['product_id','created','products.name','products.price','products.delivery_time']);
;
return $query;
}
}
In this setup I always have the problem that the matching of the id's are wrong. With the products it's good. The users_carts.product_id links with the products.id, but the products_netto_deals.product_id should link with product.id and not with the users_carts.id. I tryed foreign key and bind, but both are an part of an solution.
Beside this problem, when connecting the association ProductsNettoDeals I get an error when activating the commented active => '1'. This field indicates if an record is active and should be loaded. The same field exist in the products_netto_deals tabel to indicate if an netto price is active.
Error: SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'active' in where clause is ambiguous
Can anyone point me in the right direction for the correct way of the association and the error warning. Thanks in forward.
CakePHP uses joins for fetching associations, in order to speed ud the information retrieval process. This means that when having column names repeated across different table, you will need to prefix the column names in the query clause"
Instead of:
$query->where(['active' => true]);
Do:
$query->where(['UsersCarts.active' => true])

Sonta Admin Bundle List Url Customization

I want to pass a parameter value to the route with the content of the current cell.
$listMapper->add("parent_id", 'url', array(
'route' => array(
'name'=>'admin_xxxxx_news_news_show',
'absolute'=>true,
'identifier_parameter_name'=>"parentID"
)
);
I have a route admin_xxxxx_news_news_show and have to pass the parameter from the current column. I don't have a relation of the current field with the related entity. I am using two columns for association, one for the entity and second for that entity ID. With my current knowledge of Symfony2 I need to have a customized view.

how to customise search results of apachesolr (drupal 6)

can somebody help me how to customize the search result of a apache solr search. i was only able to access these variables [comment_count] => [created] => [id] => [name] => [nid] => [title] => [type] => [uid] => [url] => [score] => [body].
how can i access other variable like status, vote .... from the index ( i don't want to access the database for retrieving these values, i want to get it from the index itself)
i need to display no of votes for that specific node in the result snippet
i need to understand
1. how to index votes field
2. how to show the vote, status... in result snippet.
Votes are a poor choice for indexing for a couple of reasons:
Votes can change quickly
When a vote is made, the node is not updated. As such, apachesolr won't know to re-index the node to pick up the change.
If by 'status' you mean the node->status value, then the answer is that it will always be 1. Unpublished nodes are never indexed.
Now, if you want to add something else to the index, you want hook_apachesolr_update_index(&$document, $node) - this hook gets called as each node is being indexed, and you can add fields to $document from $node to get the values into the solr index. However, you want to use the pre-defined field prefixes - look at schema.xml to find the list.
Below is example of code to add fields for sorting, and for output.
/**
* Implementation of hook_apachesolr_update_index()
* Here we're adding custom fields to index, so that they available for sorting. To make this work, it's required to re-index content.
*/
function somemodule_apachesolr_update_index(&$document, $node) {
if ($node->type == 'product') {
$document->addField('sm_default_qty', $node->default_qty);
$document->addField('sm_sell_price', $node->sell_price);
$document->addField('sm_model', $node->model);
foreach ($node->field_images AS $image) {
//$imagecached_filepath = imagecache_create_path('product', $image['filepath']);
$document->addField('sm_field_images', $image['filepath']);
}
}
}
/**
* Implementation of hook_apachesolr_modify_query()
* Here we point what additional fields we need to get from solr
*/
function somemodule_apachesolr_modify_query(&$query, &$params, $caller) {
$params['fl'] .= ',sm_default_qty,sm_field_images,sm_sell_price,sm_model';
}
If you want to totally customize output, you should do following:
1) Copy search-results.tpl.php and search-result.tpl.php from /modules/search to your theme's folder.
2) Use the $result object as needed within search-result.tpl.php
3) Don't forget to clear the theme registry by visiting admin/build/themes
Or as mentioned about, you can override using preprocessor hooks.
Regards, Slava
Another option is to create a view(s) of your liking with input argument nid, then create the following preprocess in your template.php file:
function MYTHEME_preprocess_search_result(&$vars) {
$vars['myView'] = views_embed_view('myView', 'default', $vars['result']['node']->nid);
}
Matching the view name 'myView' with the variable name makes sense to me. Then you can use the variable $myView in your search-results.tpl.php file.
Here's a video by the makers of the Solr Search Integration module with an overview on how to customise what nodes and fields are indexed, and what Solr spits out as a search result...
For Drupal 6:
http://sf2010.drupal.org/conference/sessions/apache-solr-search-mastery.html
And Drupal 7:
http://www.acquia.com/resources/acquia-tv/conference/apache-solr-search-mastery
It all looks very customisable!

Resources