Nested insert in Linq to SQL? - asp.net

How do i put a nested insert into database using Linq to SQL
my Table is like this:
[Post]
--Post ID
--Post Title
[Attachment]
--Post ID (PFK)
--AttachmentID (PK)
--IMGID (FK)
--VIDID (FK)
[IMG]
--IMGID (PK)
it's a M:N table
basically when i submit a form , it go together with the attachments within the form. Im ok with insert Post, but strugling with the inserting attachments since i dont have any POST ID yet...
so what do you think is the best solution for this kind of problem?
Thank you

If you create a two-way Association between Post and Attachment, so you can do the following:
Post post = new Post { /* Set some fields perhaps */ };
Attachment attachment = new Attachment { /* Same here */ };
attachment.Post = post; // That is the association you need to create
dataContext.Posts.InsertOnSubmit(post);
dataContext.SubmitChanges();
LINQ-to-SQL will then handle the insertion of the Attachment(s) automatically.
I haven't used the DBML designer in a while, but I'm fairly sure you can say that a Post has Attachments and that an Attachment has a Post. And when you say attachment.Post = post, the setter for Post internally will do _post.Attachments.Add(attachment) where _post is a private member of the Attachment entity. Then, when you submit the Post, it will loop through the Attachments and insert them too.

Related

wordpress: let subscribers submit content to post ( yes / no, radio buttons )

i'm currently struggeling with finding the right way for a function in wordpress.
there are many subscribers who should be able to say "yes" or "no" to a post.
until now, i added some more fields to the comments template.
but its not the right way because of empty comments, its like raping the comments function.
the point is: how to let subscribers (or any other role) submit content (like a comment) to a post?
is there any other way than the_content & the_comment, user specific?
You can create custom form and after submission add data in database in wp_postmeta table.
For example `
get data from form
$data = array();
$data['user_id'] = $user->ID; // current user id
$data['post_id'] = $_POST['post_id']; // post id which you will receive after form submission
$data['answer'] = $_POST['answer']; // this is your radio button value yes or now
You can serialize $data or you can keep it as json in database.
INSERT INTO wp_postmeta (meta_key,meta_value) VALUES ('user_vote', $serialized_data);
Best Regards,
Davit.

Prevent execution of $wpdb->get_var if post is in a specific taxonomy

in a site of mine I Bulk import data (published as posts) from a json file, and to prevent post duplication, I do the following query on the wp db:
$identificatore = $deals->uuid;
$output = $wpdb->get_var($wpdb->prepare("SELECT count(id)
FROM $wpdb->posts wpo, $wpdb->postmeta wpm
WHERE wpo.ID = wpm.post_id
AND wpm.meta_key = 'deal_id'
AND wpm.meta_value = '$identificatore'"));
if(empty($output)) {
// retrieve data from json and publish as post
}
The problem:
When the blog have 1500/2000 posts published no matter, but when it have 10000/15000, the db query are longer then 15/20 seconds
In my mind what I think to do is do a preliminary check of the post, and check if is under a term "sold out" in the taxonomy "status", and if in that taxonomy jump the query to check if there is a custom fields with the value "$identificatore".
However if you know another way most light to prevent the posts duplication with "wp_insert_post" I'm here and I'm waiting for your help
Thanks

Doctrine Translatable how to only fetch records with a translation

I'm using Symfony2 + Doctrine + Translatable from DoctrineExtensions.
I have an entity article which has a couple of translatable fields like Title and Content. What is happening now is that I've created a bunch of articles in Chinese language, but have not added a translation for English. This is giving me some problems when I try to display a top 8 most recent articles on my homepage... When I view my homepage in English, it will show a bunch of title-less articles there (the Chinese articles that dont have a translation in English).
I found a solution for this in the Translatable extension by using ORM query Hint:
"\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER".
This lets you directly query the translated fields of your entity which are actually stored in ext_translations and not in the entity's table.
As a result the code I use to fetch the articles for the homepage looks like this:
public function getNewestArticles($limit = 1){
$dql =
"SELECT a FROM NewsBundle:Article a
WHERE a.published = 1
AND a.title != ''
ORDER BY a.created_at DESC";
$query = $this->_em->createQuery($dql);
$query->setMaxResults($limit);
$query->setHint(
\Doctrine\ORM\Query::HINT_CUSTOM_OUTPUT_WALKER,
'Gedmo\\Translatable\\Query\\TreeWalker\\TranslationWalker'
);
$result = $query->getResult();
return $result;
}
This actually works, but the generated query is so inefficient that it slows down the page a lot... I'm talking 1800 ms for that query. I've also tried to just put the fields I need in the select statement, which improves the performance, but still not enough.
Any ideas?
you can try to optimize the query manually. use locale as the parameter in your repository
public function getNewestArticles($locale, $limit = 1){
//your own superfast query
}
in the controller then set the locale with
$locale = $request->getLocale();
$em->getNewestArticles($locale, $limit);
additional question: have you set indexes on your tables?

Fetcing dynamically added fields in forms - Symfony2

i have some data that will be added in hidden input fields inside a form.
Now since i'm using symfony2 forms those fields don't get submitted.
i.e. $form->getData() does not get data from those fields.
how do i get data from those dynamically added (hidden) input fields as well?
Fetch post values in context of a form type:
$postData = $request->request->get('form_name');
$name_value = $postData['title'];
Fetch post values not in context of a form type (maybe this is what you need):
$data = $request->request->all();
$name = $data['input_name'];

How to join two Drupal CCK content types in view without a node reference field?

Users uploaded two csv files to create nodes for two CCK content types (Delivery Note and Payment), i.e.
Delivery Payment
---------- ---------
Order No. Order No.
Recipient Charge
Address
I would like to create a view as a report for the boss:
Order No. Recipient Charge
--------- --------- -------
... ... $...
... ... $...
... ... $...
Order No. field is unique and both content types.
How's it possible to do it in Drupal's view?
i believe you are looking for view relationships. check out drupal views relationships
for a video tutorial.
I ended up using View Custom Field with a PHP code field to retrive linked result via another view:
<?php
$order_no = $data->node_data_field_order_no_field_order_no_value;
if ($order_no) {
$view = views_get_view('find_cck_node');
$view->set_arguments(array('PAYMENT', $order_no));
$view->execute();
if (count($view->result)) {
$value = $view->result[0]->node_data_field_payment_amount_field_payment_amount_value;
print($value);
}
}
?>

Resources