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();
}
}
}
}
}
Related
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));
}
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();
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.
I have a form, PhotoForm, which has an emebedded BlobDataForm.
I can save the blbo data fine, my problem comes, with the blob_data table.
I have 2 fields, image_width and image_height.
I'd like to save these details as well, when the blob is saved.
I have overridden doSave();
protected function doSave($con = null)
{
if (null === $con)
{
$con = $this->getConnection();
}
$this->updateObject();
$blobData = new BlobData();
$this->saveEmbeddedForms($con);
$this->getObject()->setBlobData($this->getEmbeddedForm('blob_data')->getObject());
$this->getObject()->save($con);
}
Would I need to override saveEmbeddedForms() as well?
Thanks
EDIT:
Ok, so it seems i need to override:
processValues()
I'm just having trouble getting the images width and height attributes.
Does anyone know how I'd do that?
Thanks
If you can get this 2 informations from your blob_data field, you can override the preSave method of your BlobData class which is called just before saving object :
public function preSave($event)
{
//get the information from the blob_data
$this->image_width = ... ;
$this->image_height = ... ;
}
Right, so after all that, I had to override saveEmbeddedForms:
public function saveEmbeddedForms($con = null, $forms = null)
{
if (null === $con)
{
$con = $this->getConnection();
}
if (null === $forms)
{
$photos = $this->getValue('blob_data');
$forms = $this->embeddedForms;
foreach ($this->embeddedForms['blob_data'] as $name => $form)
{
if (!isset($photos[$name]))
{
unset($forms['blob_data'][$name]);
}
}
}
foreach ($forms as $form)
{
if ($form instanceof sfFormObject)
{
$form->saveEmbeddedForms($con);
$blobData = $form->getObject()->getBlobData();
$imageStream = stream_get_contents($blobData);
$image = imagecreatefromstring($imageStream);
$form->getObject()->setImageWidth(imagesx($image));
$form->getObject()->setImageHeight(imagesy($image));
$form->getObject()->setFileExtension('jpg');
//return parent::preSave($con);
$form->getObject()->save($con);
}
else
{
$this->saveEmbeddedForms($con, $form->getEmbeddedForms());
}
}
}
This seemed to work for me
Thanks
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