How to delete a db row in LINQPAD - linqpad

Total NOOB question. I have been using the new linqpad for about 20 minutes now. Great!
But now i want to delete a row in the db. I am using a EF5.0 connection. I cant seem to find anything in the help files or on the net. The only thing I can find is DeleteOnSubmit which doesnt work with EF (as far as I can see). I have also tried DeleteObject which doesnt work either. This is what I have tried.
var co = Companies.First();
co.Dump();
Companies.DeleteObject(co);

This is old... and I don't know if/when this was added (probably in response to this exact scenario)… but you can accomplish this (in your given example) as follows:
//test the following line to ensure the context doesn't complain about the .First() reference
Companies.DeleteOnSubmit(Companies.First());
Companies.Context.SubmitChanges();

You need to SaveChanges on your context (Companies) for your row to be deleted.
Companies.SaveChanges();

Related

Symfony3 Doctrine2 Column cannot be null

[PROBLEM SOLVED]
Hey folks I need your help on a symfony project.
I have two entities, "article" and "image", article can have several images so I made a relationship OneToMany on the article side and ManyToOne on the image side. Doctrine generates for me an "article_id" column on the image table.
I made a form to create an article with one or more images... however when I execute it I have an error that is thrown :
An exception occurred while executing 'INSERT INTO image (url, alt, article_id) VALUES (?,?,?) 'with params ["jpeg", "untitled.jpg", null]: SQLSTATE [23000]: Integrity constraint violation: 1048 Column' article_id 'can not be null.
When I look to the profiler logs debug, I can see that I have an "INSERT INTO article[...]" (so the article is created with an ID ?) and just after this I have the famous "INSERT INTO image[...]" where the error occurs.
Thank you for your help.
EDIT In bold you will find the lines I added to solve this problem.
I added into the ArticleType form, in the images collection options this :
'by_reference' => false
And I added to the addImage method into the Article entity this :
$image->setArticle($this);
$this->images->add($image);
This is a very common question but surprisingly difficult to search for. In fact the first ten or so search results had incorrect answers.
In any event, you need to make sure that image is linked to the article.
class Article
public function addImage($image) {
$this->images[] = $image;
$image->setArticle($this); // ADD THIS
I think #Cerad's answer is very wrong, although I can't blame him because OP hasn't posted enough code, don't be shy with it, we're here to help. It also helps to briefly mention if you're just starting out (as I suspect you are) as it will help us gauge whether its a novice problem or something deeper.
Since I'm assuming it's a novice problem here is the novice solution.
With they way you've mapped (which I believe is correct) you are basically telling Doctrine to Tell your RDBMS (MySql/MariaDB/PostgreSQL) that an article CAN have many images, but an image MUST belong to an article.
This means that if you attempt to save an image without specifying the owning article, it will throw an error.
This is what I think you are doing evident in the line
(url, alt, article_id) VALUES (?,?,?) 'with params ["jpeg", "untitled.jpg", null]
the value "null" is where the issue is coming about because you are not specifying the owning article.
You have 2 options
1.) When saving image, if you have the article object at hand just do
$image->setArticle($article);
2.) When saving the image, if you have the article_id at hand just do
$image->setArticle($em->getReference('AppBundle\Entity\Articles', $article_id));
You must follow one of the above when saving an image directly.
My general experience has been that Doctrine does a really good job of generating entities from command line that you really don't need to change much unless adding some advanced functionality like second_level_cache or MySQL point type.
Cheers.
The problem is that when you submit your form, the first image is attached to the article but the others are not, so you get an SQL error.
You have to do a "foreach loop" for every image and attched the article to each one.
Look here at the addAction function
https://github.com/ismail1432/TheGoodLoc/blob/master/Symfony/src/VTC/AnnonceBundle/Controller/AdvertController.php

TextControl Images/Watermarks

In version 18 of TextControl (http://www.textcontrol.com) there is supposed to be the ability to add background images/watermarks to the document, however I find that the behavior of the various overloads, etc is not working correctly and that the documentation is rather scarce on examples. Does any one have a working example of adding watermarks to a Word document through the ServerTextControl object?
This functionality didn't exist in previous versions, so I recognize it's still rather new, I just find it weird that doing something like
tx.Images.Add(draftImage, pageNumber, location, ImageInsertionMode.BelowTheText);
doesn't actually add the images to the document, but if I use another overload beforehand, it adds both?
I just need a loop along the lines of
foreach (TXTextControl.Page page in pages){
page.Select();
var location = tx.InputPosition.Location;
var pageNumber = page.Number;
tx.Images.Add(draftImage, pageNumber, location, ImageInsertionMode.BelowTheText);
}
where location is supposed to be the beginning of the page.
Any help would be appreciated, this should be simple!
Thank you
Finally got a response to this on their forum, it's not a complete solution, but it works for the most part. Figured I would follow up here, so that anyone who ends up on this page would get the help I wanted.
http://forums.textcontrol.com/showthread.php?325522-Watermarking-Background-Image-on-Saved-Documents-in-X8&p=41815#post41815

What are the Aweber API Variables $account_id and $list_id?

You can check here:
https://labs.aweber.com/docs/code_samples/subs/create
The script to add a new subscriber to the list via api requires those two pieces info...only I cannot figure out for the life of me what those two variables are!! I've beaten through every little aspect of my Aweber Subscriber Account, AND my Aweber Labs account...and I can't find any reference to either of those variables anywhere. I've submitted some tickets to them, and haven't gotten any response yet.
Does anyone have any ideas here? I've tried my account names, my list names, to no avail!
~~~~~~~~~~~~~~~~~~~~~~~~~~~
Okay, I've got it! You can get the values of both of these variables by dumping some other variables in the aweber api after making certain api calls.
get the account id first:
$account = $aweber->getAccount($accessKey, $accessSecret);
then vardump or print_r $account.
next we get the list id:
$account = $aweber->getAccount($accessKey, $accessSecret);
$list_url = 'https://api.aweber.com/1.0/accounts/<id>/lists';
$lists = $account->loadFromUrl($list_url);
then vardump or print_r $lists.
And you are all set! I'm so happy I figured this out, it freakin took long enough. Hopefully this saves some one a bit of time.
I too have agonized over finding the $list_ID, so went to deactivate the list, and create a new one, and "discovered" that if you hover over the Deactivate button, you get a url you can copy, and this gives both %account and %list Ids
https://www.aweber.com/users/lists/deactivate/$accountID/$lisID
like this....
https://www.aweber.com/users/lists/deactivate/123456/123456
Hopefully this will help make someone as it is a super easy solution
The proper answer is Anne Allen's one, but...
Check the return of the /accounts endpoint. It should return the same account id as you detected in the link, but I had cases they were different (strange, isn't it?). Use the account id returned by the /accounts endpoint and other endpoints to retrieve lists, subscribers, etc. will start to work. It's like if some accounts have two ids, one partially works and the other fully works.
Let me tell you how to get $list_id value... login into your AWeber account and then create a new list copy only integer value from list's name.
At first, login.
1) click Reports>Settings. Your account ID will be displayed in the box,example: ?id=XXXXX
2) click List Options>List Settings. There you will see the list ID under the name.
p.s. To add subscriber, you can use this - Automatically add into aweber list

mongokit remove all items from collection

I would like to remove all items from a test collection. My setup is
connection = Connection(app.config['MONGODB_HOST'], app.config['MONGODB_PORT'])
db = connection.test_database.tutorial
I have a document model class Test which maps to the tests collection. I've tried deleting the collection with both
connection.test_database.drop_collection('tutorial.tests')
db.tests.remove()
However querying something like
list(db.Test.find())
still gives me the old data. Something like
list(db.tests.find())
returns an empty list. However if I add new entries into tests the previous query also doesn't reflect the changes, so I don't think thats accurate either.
Problem was with this line:
db = connection.test_database.tutorial
Since it was saying test database and the tutorial collection
removing worked when I changed it to
db = connection.tutorial

Does anyone know why Take() isn't working here

i have the following code using Nhibernate.Linq
var apps = Session.Linq<History>().OrderByDescending(r => r.LastUpdated).Take(50);
Console.Write(apps.Count());
the count returns 1000 (NOT 50 which is what i would have expected)
any ideas why the .Take() is not working?
It looks like a bug in the Linq provider (you are using the old one, I tried the new one too and it still doesn't work).
You should open an issue in http://jira.nhforge.org/
As a workaround, use .ToList() in the assignment to apps.