Reputation: 788
I am using Symfony and creating a soft with 3 tiers (client, apache, mysql). So I don't know how to get the statut when the symfony application persists and flushs something?
When I add something in database I display an alert like "Add done!" but if my database is down I will display "Add done" despite the fail...
So how can I get the statut of these functions (flush/persist)? How can I change my alert switch the statut?
Best regards,
Upvotes: 6
Views: 17494
Reputation: 2516
My issue was that I was passing a complex object into a field that expected a string even though I had set that column up to be a ManyToOne. Apparently @ORM\Column overrode that.
However! It's not what the error was that's interesting, it's how I fixed it.
I was having a PDOException. I had to deep dive into the code with xdebug and PHPStorm. xdebug is supported by many PHP IDEs. xdebug is tough to set up the first several times that you set it up. There's always some firewall hassle or hassle getting it to show up in your phpinfo() or xdebug.enable_remote or xdebug.remote_host.
Get used to it. JUST DO IT! No seriously. Don't write another line of code in PHP until you have got xdebug working; even if you run into 8 million issues. You will save years of your life in coding.
I don't know about Symfony, but ZF3 now comes with a Vagrantfile. You install vagrant and Oracle VirtualBox, type vangrant up
in your Zf3 project directory, and you have a fully-functional local web server running your ZF3 application at http://localhost:8080. From there all I had to do was:
... Well first, before I called vagrant up
for the first time, I added the following line to the Vagrantfile
config.vm.network "private_network", ip: "192.168.33.10"
... then I called vagrant up
vangrant ssh
sudo -s
apt-get install php-xdebug
echo "xdebug.remote_enable = 1" >> /etc/php/7.0/apache2/php.ini
echo "xdebug.remote_host = 192.168.33.1" >> /etc/php/7.0/apache2/php.ini
apachectl restart
In your phpinfo(), you should see xdebug somwhere. Just ctrl + f
for it. If it's there, you're mostly in business.
Then I added a rule for port 9000 on my firewall. Then I started an xdebug session. If it doesn't work You can temporarily shut down Windows Firewall just to check if it works, but remember to enable it right away and make sure it's not just the firewall. Setup a firewall rule for incoming on port 9000 and allow ip 192.168.33.10. If you don't know how to do this ... why are you trying to develop web software?
Learn to use xdebug!!!
Upvotes: 0
Reputation: 1245
Use a try & catch block:
try {
$article = new Article(); //Example entity
$em = $this->getDoctrine()->getEntityManager();
$em->persist($article);
$em->flush();
$this->get('session')->setFlash('flash_key',"Add done!");
} catch (Exception $e) {
$this->get('session')->setFlash('flash_key',"Add not done: " . $e->getMessage());
}
In case you get errors try using "\Doctrine\ORM\ORMException $e", \Doctrine\DBAL\DBALException $e" or "\Exception $e" inside catch()
Upvotes: 15