Read XML data and save them to mysql database in symfony 4 - mysql

I have a simple XML file. I need to read the data and save them to mysql database table (1 or 2 tables). The file is like following :
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0" xmlns:g="http://">
<myfile>
<title><![CDATA[All data]]></title>
<stock>
<name><![CDATA[my name]]></name>
<qty><![CDATA[0]]></qty>
<price><![CDATA[4.99]]></price>
<image><![CDATA[http://fashiondropshippers.com/media/catalog/product/i/m/image_463.jpg]]></image>
</stock>
</myfile>
</rss>
I am trying to do that in Symfony 4 using crawler. my codes in my controller are following
$crawler = new Crawler();
$crawler->addContent(file_get_contents('http://localhost/XML/myxml.xml'));
foreach ($crawler as $domElement) {
var_dump($domElement->nodeValue);
}
return new JsonResponse($domElement->nodeValue);
It displays data with errors. Now I need to save those data in mysql database tables. Could you please tell me how to proceed further ?
Many thanks in advance !

Ok. If you want to save data to DB you have to do the following:
Add Doctrine to your project: https://symfony.com/doc/current/doctrine.html#installing-doctrine
Create an entity: https://symfony.com/doc/current/doctrine.html#creating-an-entity-class
Make migrations: https://symfony.com/doc/current/doctrine.html#migrations-creating-the-database-tables-schema and execute them
Save your data to DB https://symfony.com/doc/current/doctrine.html#persisting-objects-to-the-database

Related

How to get rid of Liquibase includeAll tag with formatted .sql changesets error: Cannot find the declaration of element 'databaseChangeLog'

I am using a Gradle Spring project with liquibase. To run liquibase I am running the jar created by compiling the project. I am trying to use the liquibase "includeAll" tag in an xml changelog to run all formatted sql changelog scripts inside a directory I've called includeAllScriptsTest (currently contains only one .sql file named test.sql with one changeset).
If I try to use includeAll in my master db-changelog file, at run time liquibase returns the error: file:///.../conf/db/db.changelog-master.xml/ is not a recognized file type.
In an attempt to get around this, I reference from my db-changelog-master.xml another xml called includeAll-changelog.xml. In this file I have the includeAll tag, below are the contents of this file.
<?xml version="1.0" encoding="utf-8"?>
<databaseChangeLog
xmlns="https://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog conf/xsd/dbchangelog-3.8.xsd">
<includeAll path="***/includeAllScriptsTest/" relativeToChangelogFile="true"/>
</databaseChangeLog>
Inside the folder includeAllScriptsTest is a single file called test.sql, with the contents as below:
--liquibase formatted sql
--changeset author:1 dbms:MySQL splitStatements:true endDelimiter://
DROP PROCEDURE IF EXISTS `TESTINCLUDEALL`;//
CREATE PROCEDURE `TESTINCLUDEALL`()
BEGIN
SELECT * FROM TABLE;
END;//
However, this gives me a different error at run time: cvc-elt.1.a: Cannot find the declaration of element 'databaseChangeLog'. I've found online conflicting information regarding whether it is possible to use includeAll with the version of liquibase used by my project. I'm currently using 3.4.1.
cvc-elt.1.a: Cannot find the declaration of element
'databaseChangeLog'
This error is an XML Parsing error, pointing databaseChangeLog is not declared in the xml schema.
May be you can try using following XSD in your changelog:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.8.xsd">
<includeAll path="***/includeAllScriptsTest/" relativeToChangelogFile="true"/>
</databaseChangeLog>
If this still doesn't help, please have a look at few points in answer on this post.

Load XML into mySQL with diffrent rows ID

I have the following XML file and I need load into mysql using LOAD XML LOCAL INFILE.
I´m using the following line:
LOAD XML LOCAL INFILE 'C:/_BORBA_/xml/importxml.xml' INTO TABLE importxml ROWS IDENTIFIED BY '<ide>'
If I declare "ide" as delimiter, I can import with sucess, but I have to import all tags (ide, info and proc) as one record row.
How can I explicit that I have 3 groups of information into my XML file?
<?xml version="1.0" encoding="utf-8"?>
<infNFe versao="3.10" Id="NFe351710006">
<ide>
<cUF>35</cUF>
<cNF>99999</cNF>
<natOp>Venda de terceiros</natOp>
<indPag>1</indPag>
<mod>55</mod>
<serie>1</serie>
<nNF>888888</nNF>
<dhEmi>2017-10-26T16:50:52-02:00</dhEmi>
</ide>
<info>
<tpNF>1</tpNF>
<idDest>2</idDest>
<cMunFG>3525904</cMunFG>
<tpImp>1</tpImp>
<tpEmis>1</tpEmis>
</info>
<proc>
<cDV>8</cDV>
<tpAmb>1</tpAmb>
<finNFe>1</finNFe>
<indFinal>0</indFinal>
<indPres>9</indPres>
<procEmi>3</procEmi>
<verProc>008</verProc>
<CNPJ>99999888844</CNPJ>
</proc>
</infNFe>
Thank you for any help.

EntityFramework on MySql changing connection string does not change results data

I'm working with EntityFramework 5.0 and MySql. I have generated model from database, and my application now have to connect on multiple database with same structred data.
So i have to dynamic change connection string based on some info.
I try to change database name even from config section of connection string, and with EntityConnectionStringBuilder, but i had the same result: my new connection is stored correctly, but data returned are of the first database.
From WebConfig:
add name="dbIncassiEntities" connectionString="metadata=res:///DAL.Modelincassi.csdl|res:///DAL.Modelincassi.ssdl|res://*/DAL.Modelincassi.msl;provider=Devart.Data.MySql;provider connection string="user id=root ... database=dbname2"" providerName="System.Data.EntityClient" />
From code:
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.Provider = providerName;
entityBuilder.ProviderConnectionString = "user id=...database=dbname2";
entityBuilder.Metadata = #"res://*/DAL.Modelincassi.csdl|res://*/DAL.Modelincassi.ssdl|res://*/DAL.Modelincassi.msl";
var context = new dbIncassiEntities(entityBuilder.ToString());
My constructor:
public dbIncassiEntities(string conn)
: base(conn)
{
}
What am i missing?
UPDATE
I can see that calling a query directly from SqlQuery, results returned are correct,
while using the generated entities i retrieve wrong data.
var test = context.Database.SqlQuery<string>(
"SELECT cognomenome FROM addetto limit 0,1").ToList();
But calling..
var oAddetto = from c in context.addettoes select c;
So my problem is only on the model itsself, and manually changing the generated schema
<EntitySet Name="addetto" EntityType="dbIncassiModel.Store.addetto" store:Type="Tables" Schema="dbname2" />
..i'll get the right information.
My question now is: how can i change in code these informations??
Any help is really appreciated!!
Thanks, David
Ok, i've found a workaround for now.
I simply clear the shema name on the designer, and now i can call the generated entities succesfully. Hope this can help anyone else.
David
While I could not remove the Schema in the designer, I removed it directly in the .edmx file. Do a full text search for Schema="YourSchema" in an XML editor of your choice and remove the entries. After that, changing the connection string is enough.
Downside is, the Visual Studio designer and mapping explorer won't work properly anymore.
This seems to be more of a dotConnect issue rather than MySQL, since the problem also exists for the Oracle adapter:
http://forums.devart.com/viewtopic.php?t=17427

How to execute large sql query in magento?

I want to store huge content in db and my sample text is 16129 characters in length when i tried to execute this query it is showing "error:The requested URL could not be retrieved" in firefox and "no-data received" in chrome.
Moreover I use LONGTEXT as datatype for text in DB.
I also tried to execute the query directly in phpmyadmin it is working correctly.
The code is shown below.
public function _getConnection($type = 'core_write') {
return Mage::getSingleton('core/resource')->getConnection($type);
}
public function testdbAction(){
$db = $this->_getConnection();
$current_time=now();
$text="The European languages are members of the same family...... ...Europe uses the same vocabulary. The ";//text is 16129 characters in length
$sql = "INSERT into test(`usercontent_id`,`app_id`,`module_id`,`customer_id`,`content`,`created_time`,`updated_time`,`item_id`,`index_id`,`position_id`) VALUES (NULL, 15, 9,2,'" .$text. "','" . $current_time . "','" . $current_time . "',1003,5,4)";
$db->query($sql);
}
How do i handle this? any suggestions or help..
Try using $db->exec($sql) instead of $db->query($sql)
For manipulating with database structure and data Magento has a dedicated place. It is called install / upgrade scripts. It was made to keep track of modules version for easy updates. So you should use a script to add new data.
Here's an example.
config.xml of your module:
<modules>
<My_Module>
<version>0.0.1</version>
</My_Module>
</modules>
<global>
<resources>
<my_module_setup>
<setup>
<module>My_Module</module>
<class>Mage_Core_Model_Resource_Setup</class>
</setup>
</my_module_setup>
</resources>
</global>
Now, you need to create following file:
My_Module/sql/my_module_setup/install-0.0.1.php
Now, depending on your Magento version, file would need to have a different name. If you're using Magento CE 1.4 or lower (EE 1.8) then you should name it mysql4-install-0.0.1.php.
This script will be launched at next website request. Class Mage_Core_Model_Resource_Setup will execute code inside install-0.0.1.php. From within the install script you will have access to the Setup class by using $this object;
So now, you can write in script following code:
$this->startSetup();
$text = <<<TEXT
YOUR LONG TEXT GOES HERE
TEXT;
$this->getConnection()
->insert(
$this->getTable('test'),
array(
'usercontent_id' => null,
'app_id' => 15,
'content' => $text,
//other fields in the same fashion
)
);
$this->endSetup();
And that's it. It's a clean and appropriate way of adding custom data to the database in Magento.
If you want to save a user input on a regular basis using forms, then I recommend creating w model, resource model and collection, define entities in config.xml. For more information please refer to Alan Storm's articles, like this one.
I hope that I understood your question correctly.

Updating an attribute in XML with MySQL

I have a chunk of XML stored as a string in a MySQL database, and need to update one of the attributes using a query.
Given the following string:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<town>
<road name="Main" direction="north"/>
</town>
I would need to change it to update the attribute direction to a different value:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<town>
<road name="Main" direction="east"/>
</town>
Is there an easy way to accomplish this? Thanks in advance!
EDIT: The query would be ran in a SQL script file containing various other upgrade queries, which is called by a piece of code in Java.
Checkout PHP's simple XML
http://www.php.net/manual/en/class.simplexmlelement.php
$xml=new SimpleXMLElement($xml);
print $xml->road->attributes('direction') = 'east';
I ended up overriding the upgrade process for that version. Through Java and JDBC I looped through every row and used JAXB to gain access to the attribute that needed to be changed. I would have preferred to do it with full SQL queries. :(