Chapters
31 Chapters
|
3:19:40
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.1.3
Subscribe to download the code!Compatible PHP versions: ^7.1.3
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
06.
Email Context & the Magic "email" Variable
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
This tutorial is built on Symfony 4.3, but will work well with Symfony 4.4 or 5.
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.1.3",
"ext-iconv": "*",
"aws/aws-sdk-php": "^3.87", // 3.110.11
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"doctrine/annotations": "^1.0", // 1.10.1
"doctrine/doctrine-bundle": "^1.6.10", // 1.11.2
"doctrine/doctrine-migrations-bundle": "^1.3|^2.0", // v2.0.0
"doctrine/orm": "^2.5.11", // v2.7.2
"knplabs/knp-markdown-bundle": "^1.7", // 1.7.1
"knplabs/knp-paginator-bundle": "^2.7", // v2.8.0
"knplabs/knp-snappy-bundle": "^1.6", // v1.6.0
"knplabs/knp-time-bundle": "^1.8", // v1.9.1
"league/flysystem-aws-s3-v3": "^1.0", // 1.0.23
"league/flysystem-cached-adapter": "^1.0", // 1.0.9
"league/html-to-markdown": "^4.8", // 4.8.2
"liip/imagine-bundle": "^2.1", // 2.1.0
"nexylan/slack-bundle": "^2.1,<2.2.0", // v2.1.0
"oneup/flysystem-bundle": "^3.0", // 3.1.0
"php-http/guzzle6-adapter": "^1.1", // v1.1.1
"phpdocumentor/reflection-docblock": "^3.0|^4.0", // 4.3.1
"sensio/framework-extra-bundle": "^5.1", // v5.4.1
"stof/doctrine-extensions-bundle": "^1.3", // v1.3.0
"symfony/asset": "^4.0", // v4.3.4
"symfony/console": "^4.0", // v4.3.4
"symfony/flex": "^1.9", // v1.21.6
"symfony/form": "^4.0", // v4.3.4
"symfony/framework-bundle": "^4.0", // v4.3.4
"symfony/mailer": "4.3.*", // v4.3.4
"symfony/messenger": "4.3.*", // v4.3.4
"symfony/property-access": "4.3.*", // v4.3.4
"symfony/property-info": "4.3.*", // v4.3.4
"symfony/security-bundle": "^4.0", // v4.3.4
"symfony/sendgrid-mailer": "4.3.*", // v4.3.4
"symfony/serializer": "4.3.*", // v4.3.4
"symfony/twig-bundle": "^4.0", // v4.3.4
"symfony/validator": "^4.0", // v4.3.4
"symfony/web-server-bundle": "^4.0", // v4.3.4
"symfony/webpack-encore-bundle": "^1.4", // v1.6.2
"symfony/yaml": "^4.0", // v4.3.4
"twig/cssinliner-extra": "^2.12", // v2.12.0
"twig/extensions": "^1.5", // v1.5.4
"twig/extra-bundle": "^2.12|^3.0", // v2.12.1
"twig/inky-extra": "^2.12", // v2.12.0
"twig/twig": "^2.12|^3.0" // v2.13.1
},
"require-dev": {
"doctrine/doctrine-fixtures-bundle": "^3.0", // 3.2.2
"easycorp/easy-log-handler": "^1.0.2", // v1.0.7
"fzaninotto/faker": "^1.7", // v1.8.0
"symfony/browser-kit": "4.3.*", // v4.3.5
"symfony/debug-bundle": "^3.3|^4.0", // v4.3.4
"symfony/dotenv": "^4.0", // v4.3.4
"symfony/maker-bundle": "^1.0", // v1.13.0
"symfony/monolog-bundle": "^3.0", // v3.4.0
"symfony/phpunit-bridge": "^3.3|^4.0", // v4.3.4
"symfony/stopwatch": "4.3.*", // v4.3.4
"symfony/var-dumper": "^3.3|^4.0", // v4.3.4
"symfony/web-profiler-bundle": "4.3.*" // v4.3.4
}
}
9 Comments
On a form I have an EntityType, ManyToMany relationship where on the form the user can select multiple check boxes to e-mail an order too. How to I get that into the ->to() on the e-mail script?
->to($orders->getOrderstoo()) is giving me an error "An address can be an instance of Address or a string ("Doctrine\ORM\PersistentCollection") given)."
Yo Brandon!
Ah yes! So basically,
$order->getOrdersTo()gives you an array (well, it's a PersistentCollection, which is an object, but acts like an array) of your related object (e.g.Email- I'm not sure what your's is really called). You need to "convert" these into Address objects. The simplest algorithm would be:You can also get fancier and use
$addresses = $orders->getOrdersTo()->map(function($orderTo) { return new Address($orderTo->getEmail()) }... but that's up to you - that's a fancier syntax for the same thing. You could also add a helper method to your Order entity - e.g.$order->getOrdersToAddresses()and you would put the converting code inside of that.Let me know if that helps!
Cheers!
Ryan,
With your first suggestion I now get the error "An address can be an instance of Address or a string ("array") given)"
Which confuses me because I thought in the foreach loop they were being converted to an instance of Address. Did I miss something?
The fancier way I like, and I haven't seen it before but I get the error
An address can be an instance of Address or a string ("Doctrine\Common\Collections\ArrayCollection") given).
In my orders class I do have in a construct
$this->orderstoo = new ArrayCollection();
I didn't add that but Symfony must have when I made the entity from the command line. Any thoughts?
Yo Brandon !
Bah, I was sloppy - my fault! The
->to()method on the Email object does (of course) accept an Address object, but it does not support an array of Address objects, which is what we created. We can simplify:No need to create an array at all :). If you DO want to use the fancier way, here's how. Two things to know:
1) The
addTo()andto()methods actually allow for an unlimited number of arguments. So technically, you could say->to(new Address(...), new Address(...), new Address(...))- this is called a variadic argument.2) The problem with my original comment (and what caused the "Doctrine\Common\Collections\ArrayCollection given" error) was that I forgot that when you call
->mapon a Doctrine collection, it returns another Doctrine collection (ArrayCollection specifically), not a normal PHP array.If you put these facts together, you can find a solution like this:
The
...syntax "expands" the addresses ArrayCollection (this works also with normal arrays) so that it is equivalent to writing->to(new Address(...), new Address(...), new Address(...)).Let me know if that works!
Cheers!
Ryan, Thing of beauty, works perfect, thank you so much!
The NamedAddress class was removed in V4.4. You can use Symfony\Component\Mime\Address instead
https://github.com/symfony/...
Hey Med
You are totally right! And of course we have a note about it in the script code, and it's mentioned in video!
Cheers!
For some reason this video does not have HD version. :)
PS. The "project files" README md is a bit wrong in the beginning (copy-pasted from the Upload files tutorial)
Hey mr_d,
Thanks for the feedback! Video should be already fixed, it should be HD now! And we'll check out README file, this course is based on Upload course, so probably that's the reason!
Cheers!
"Houston: no signs of life"
Start the conversation!