Symfony doctrine batch processing not working - symfony

Maybe anyone can have a look:
I have a function and using batch to process bulk data to doctrine, but it not seems working, because nothing is inserted to database, but if i flush() every element, everything is working
any ideas why?
private function insertData($linesInFile, $output)
{
$google = $this->getContainer()->get('google.books');
$amazon = $this->getContainer()->get('amazon.books');
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
$number = 1;
$batchSize = 5;
foreach ($linesInFile as $string) {
$string = preg_split('/isbn_13/', $string);
$book = new Book();
$isbn = new Isbn();
if (isset($string[1])) {
$value = str_split($string[1], 23);
$isbnValue = preg_replace('/\D/', '', $value[0]);
$isbn->setIsbn($isbnValue);
$book = $google->getBookByIsbn($isbn);
if (null == $book->getIsbn()) {
$book = $amazon->getBookByIsbn($isbn);
}
$pages = $book->getPages();
$image = $book->getCover();
$about = $book->getAbout();
if ($about !== "") {
if ($image !=="") {
$em->persist($book);
if (($number % $batchSize) === 0) {
$em->flush();
$em->clear();
}
$output->writeln($isbnValue);
$number++;
}
}
}
}
$em->flush();
$em->clear();
return $number;
}
}

So, my code is good, it was some bug in google API and items were not persisted properly.

Related

Symfony - doctrine - change encoding - all entities and rows

I need to change an encoding in symfony. Problem is that there is a production which contains some data. Is there any way to list of all entitites, all rows and do on string / text column change encoding?
I didn't found any better solution, so I did in a migration:
$entityManager = $this->container->get('doctrine.orm.entity_manager');
$entities = $entityManager->getConfiguration()->getMetadataDriverImpl()->getAllClassNames();
foreach ($entities as $className) {
if ((strpos($className, "AppBundle") !== false) && (strpos($className, "Abstract") === false)) { //only my classes and no abstract class
$records = $entityManager->getRepository($className)->findAll();
$metadata = $entityManager->getClassMetadata($className);
$entity_fields = $metadata->getFieldNames();
foreach ($entity_fields as $field) {
$type = $metadata->getTypeOfField($field);
if ($type === "string") {
foreach ($records as $record) {
$oldValue = $metadata->getReflectionProperty($field)->getValue($record);
$metadata->getReflectionProperty($field)->setValue($record, " ");
$entityManager->persist($record);
$entityManager->flush();
$metadata->getReflectionProperty($field)->setValue($record, trim($oldValue));
$entityManager->persist($record);
$entityManager->flush();
}
}
}
}
}

How to test service function without connecting on database

I'm turning again on you guys, because I spend my fair share of hours on this "task" and I still can't figure out how to test my service method, without my function connection on database (I have to mock repository functions)
This is my service function
public function getInfo($history, $name)
{
$requestRepository = $this->em->getRepository(Request::class);
if ($history) {
$requests = [];
foreach ($requestRepository->getRequestsByName($name) as $request) {
$requests[] = $requestRepository->transform($request);
}
return $requests;
} else {
$request = $requestRepository->getCompletedRequestByName($name);
if (!is_null($request)) {
return $requestRepository->transform($request);
} else {
return null;
}
}
}
And this is my test
public function testGetInfo()
{
/* This returns errors, because it tries to connect to DATABASE, but I don't wan't that, that's why I figure out I need to mock this
$requestManager = new RequestManager($this->entityManager);
$test = $requestManager->getInfo('histroy', 'antrax.com');
*/
$requestManager = $this->getMockBuilder(RequestManager::class)->disableOriginalConstructor()->setMethods(['getInfo'])
->getMock();
// And rest of this are just my FAILED attempts to figure out, how to test my methods
$queryBuilder = $this->getMockBuilder(RequestRepository::class)->disableOriginalConstructor()
->setMethods(['getInfo'])->getMock();
$test = $queryBuilder->method('getInfo')->willReturnSelf();
$queryBuilder->method('getInfo')->willReturnCallback(function ($field, $value) use ($queryBuilder, $test){
if ($field == 'newStatus') {
$this->assertSame('EXPIRED', $value);
}
return $queryBuilder;
});
}
Can some one please help me how to write a test for my method getInfo so it will have 100% cover. If you need any additional informations, please let me know and I will provide. Thank you!
This is an answer to my problem
public function testGetInfo()
{
$mockEntity = $this->mockEntityManager;
$name = 'antrax.com';
$requestMock = new RequestEntity();
$transformedRequest = [
'id' => 1
];
$requestRepo = $this->getMockBuilder(RequestRepository::class)->disableOriginalConstructor()
->setMethods(['getRequestsByName', 'transform', 'getCompletedRequestByName'])->getMock();
$requestRepo->method('getRequestsByName')->willReturnCallback(function ($passedName) use ($name, $requestMock) {
$this->assertSame($name, $passedName);
return [$requestMock, $requestMock];
});
$requestRepo->method('transform')->willReturnCallback(function ($request) use ($requestMock, $transformedRequest) {
$this->assertSame($requestMock, $request);
return $transformedRequest;
});
$i = 0;
$requestRepo->method('getCompletedRequestByName')->willReturnCallback(function ($passedName) use ($name, $requestMock, &$i) {
$this->assertSame($name, $passedName);
if ($i == 0) {
$i+=1;
return null;
} else {
return $requestMock;
}
});
$mockEntity->method('getRepository')->willReturnCallback(function ($requestClass) use ($requestRepo) {
$this->assertSame(RequestEntity::class, $requestClass);
return $requestRepo;
});
$requestManager = new RequestManager($mockEntity);
$this->assertSame([$transformedRequest, $transformedRequest], $requestManager->getInfo(true, $name));
$this->assertNull($requestManager->getInfo(false, $name));
$this->assertSame($transformedRequest, $requestManager->getInfo(false, $name));
}

Doctrine not getting newly added record

I have a function that does 2 things.
Saves an existing or a new item/record
Query and re-order items
The code algorithm is like this
public function doSomething($id = 0)
{
$em = $this->getEntityManagerFromSomewhere();
$service = $this->getSomeServiceFromSomewhere();
$repo = $em->getRepository(Item::class);
$conn = $em->getConnection();
$conn->beginTransaction();
if ($id) {
$item = new Item();
} else {
$item = $repo->find($id);
}
$em->persist($item);
$em->flush();
$items = $repo->getAllItems();
// changes are saved to the database
$service->rearrangeItems($items);
$conn->commit();
}
Now the issue I'm having with this is that, if it is a new Item, when we try to get all items, that new item is not included for some odd reason.
I've tried stuff on my own and the one that works for me is to insert
$em->clear(); // after flush
I am not sure if this is the best way to approach this or if I'm misusing this functionality. It works though, when I tested it.
public function doSomething($id = 0)
There is $id is default 0, Actually, your are creating item every time.
if ($id) {
$item = new Item();
$em->persist($item);
} else {
$item = $repo->find($id);
}
also, you don't need to persist if you don't update or create new Item.
Or the way that I understood.
if($id)
{
$item = $em->getRepository(Item::class)->find($id)
}
else
{
$item = new Item();
$em->persist($item);
}
$em->flush();

Drupal and netForum: Creating a User

I am trying to create a user in netForum from a Drupal Webform.
Using a webform hook, I am calling two functions to take a users email address and first and last name, and create a netforum account when a user submits basic webforms.
However, the form times out when I hit submit, and the watchdog error from Netforum is 'could not fetch http headers'. Have I done something wrong in my implementation? I keep getting a timeout.
http://wiki.avectra.com/XWeb:WEBWebUserCreate
function inclind_form_webform_submission_insert($node, $submission) {
// find the email address in the form
$form_fields = $node->webform['components'];
foreach ($form_fields as $key => $value) {
$arguments = array();
$response = '';
if ($value['type'] == 'email') {
$arguments = array(
'emailToMatch' => $submission->data[$key]['value'][0]
);
$response = netforum_xweb_request('WEBWebUserFindUsersByEmail', $arguments, NULL);
if (!isset($response) || $response->{#attributes}['recordResult'] == 0) {
inclind_form_create_netforum_user($form_fields, $submission);
}
}
}
return;
}
/*
* Create a user in netForum based on form data
*
* #param $form_fields
* The form structure passed in from inclind_form_webform_submission_insert
* #param $submission
* The form data passed in from inclind_form_webform_submission_insert
*/
function inclind_form_create_netforum_user($form_fields, $submission) {
$arguments = array();
$arguments['oWebUser']['Individual'] = array();
$arguments['oWebUser']['Email'] = array();
$arguments['oWebUser']['Customer'] = array();
$arguments['oWebUser']['Business_Address'] = array();
$arguments['oWebUser']['Business_Phone'] = array();
$arguments['oWebUser']['Business_Phone_XRef'] = array();
$arguments['oWebUser']['Business_Fax'] = array();
$arguments['oWebUser']['Business_Fax_XRef'] = array();
foreach ($form_fields as $key => $value) {
if ($value['form_key'] == 'ind_first_name') {
$arguments['oWebUser']['Individual']['ind_first_name'] = $submission->data[$key]['value'][0];
}
if ($value['form_key'] == 'ind_last_name') {
$arguments['oWebUser']['Individual']['ind_last_name'] = $submission->data[$key]['value'][0];
}
if (strlen($arguments['oWebUser']['Individual']['ind_first_name']) && strlen($arguments['oWebUser']['Individual']['ind_last_name'])) {
$arguments['oWebUser']['Individual']['ind_full_name'] = $arguments['oWebUser']['Individual']['ind_first_name'] . ' ' . $arguments['oWebUser']['Individual']['ind_last_name'];
}
if ($value['form_key'] == 'eml_address') {
$arguments['oWebUser']['Email']['eml_address'] = $submission->data[$key]['value'][0];
$arguments['oWebUser']['Customer']['cst_web_login'] = $submission->data[$key]['value'][0];
$arguments['oWebUser']['Customer']['cst_new_password'] = user_password(20);
$arguments['oWebUser']['Customer']['cst_new_password_confirm'] = $arguments['oWebUser']['Customer']['cst_new_password'];
}
if ($value['form_key'] == 'adr_post_code') {
$arguments['oWebUser']['Business_Address']['adr_post_code'] = $submission->data[$key]['value'][0];
}
}
if (!isset($arguments['oWebUser']['Business_Address']['adr_city'])) {
$arguments['oWebUser']['Business_Address']['adr_city'] = 'Not Given';
}
if (!isset($arguments['oWebUser']['Business_Address']['adr_state'])) {
$arguments['oWebUser']['Business_Address']['adr_state'] = 'NA';
}
if (!isset($arguments['oWebUser']['Business_Address']['adr_post_code'])) {
$arguments['oWebUser']['Business_Address']['adr_post_code'] = '00000';
}
if (!isset($arguments['oWebUser']['Business_Address']['adr_country'])) {
$arguments['oWebUser']['Business_Address']['adr_country'] = 'Not Given';
}
if (!isset($arguments['oWebUser']['Business_Phone']['phn_number'])) {
$arguments['oWebUser']['Business_Phone']['phn_number'] = '000-000-0000';
}
if (!isset($arguments['oWebUser']['Business_Phone_XRef']['cph_extension'])) {
$arguments['oWebUser']['Business_Phone_XRef']['cph_extension'] = '000';
}
if (!isset($arguments['oWebUser']['Business_Fax']['fax_number'])) {
$arguments['oWebUser']['Business_Fax']['fax_number'] = '000-000-0000';
}
$response = netforum_xweb_request('WEBWebUserCreate', $arguments, '1 min');
watchdog('netforum', 'netforum user #user created', array('#user' => $arguments['oWebUser']['Email']['eml_address']), WATCHDOG_NOTICE);
}
Solved: http://drupal.org/node/866534

wordpress wp_list_categories problem

I need to make something like wp_list_categories that shows categories that are empty but only if they have children categories that have posts in them. Anyone have any ideas?
Thanks
You can probably do this with a Walker, but I tried it the old-fashioned way.
$categories = get_categories();
// First index all categories by parent id, for easy lookup later
$cats_by_parent = array();
foreach ($categories as $cat) {
$parent_id = $cat->category_parent;
if (!array_key_exists($parent_id, $cats_by_parent)) {
$cats_by_parent[$parent_id] = array();
}
$cats_by_parent[$parent_id][] = $cat;
}
// Then build a hierarchical tree
$cat_tree = array();
function add_cats_to_bag(&$child_bag, &$children)
{
global $cats_by_parent;
foreach ($children as $child_cat) {
$child_id = $child_cat->cat_ID;
if (array_key_exists($child_id, $cats_by_parent)) {
$child_cat->children = array();
add_cats_to_bag($child_cat->children, $cats_by_parent[$child_id]);
}
$child_bag[$child_id] = $child_cat;
}
}
add_cats_to_bag($cat_tree, $cats_by_parent[0]);
// With this real tree, this recursive function can check for the cats you need
function has_children_with_posts(&$children)
{
$has_child_with_posts = false;
foreach ($children as $child_cat) {
$has_grandchildren_with_posts = false;
if (isset($child_cat->children)) {
// Here is our recursive call so we don't miss any subcats
if (has_children_with_posts($child_cat->children)) {
$has_grandchildren_with_posts = true;
}
}
if (0 < intval($child_cat->category_count)) {
$has_child_with_posts = true;
} else if ($has_grandchildren_with_posts) {
// This is a category that has no posts, but does have children that do
$child_cat->is_empty_with_children = true;
var_dump($child_cat->name);
}
}
return $has_child_with_posts;
}
has_children_with_posts($cat_tree);

Resources