How bind POST data to object can save to Doctrine - symfony1

How bind POST data to object can save or edit to Doctrine.
I use Doctrine with annotation entities. I don't figure out as bind array data to entities .
I read about Symfony's components as standalone, too.
Example :
$_POST # form data , array
# this is how doctrine save object
$product = new Product();
$product->setName('product1');
$entityManager->persist($product);
$entityManager->flush();
I want $product = $helper->convert($_POST, ...) # return product object
then i can persist $product to $entityManager.

This will help you how to handle forms:
http://symfony.com/doc/current/book/forms.html
To put data on an Entitiy user the setters.
$entity->setName('Mitchel');
If you want to put a post on a entity you have to create a function like this:
public function setAttributes( array $values )
{
foreach( $values as $attribute => $value ) {
$dateEng = "/^([0-9]{4})-([0-9]{2})-([0-9]{2})$/";
$dateNl = "/^([0-9]{2})-([0-9]{2})-([0-9]{4})$/";
if( preg_match ($dateEng, $value) OR preg_match ($dateNl, $value) ){
$value = date_create( $value );
}
if(is_string( $value )){
if(preg_match( '/^\d+$/', $value )){
$value = (int) $value;
}
}
$this->{'set' . ucfirst( $attribute )}( $value );
}
return $this;
}
This works for me =)
And if I find a case who doesn't works I just add some code.

If you decide to go this way, you should use ClassMetadata class to determine field names first or you will get "PHP Fatal error: Call to undefined method ..." when user post extra data.
Something like this:
<?php
function setAttributes($values) {
$cm = new ClassMetadata(__CLASS__);
foreach ($cm->getFieldNames() as $field) {
if (array_key_exists($values, $field))
$this->{'set' . ucfirst( $attribute )}( $value );
// ...
But I would strongly recommend you to learn Form component. It took me just one day to start and about a week to make complicate forms.

Related

laravel 5.1 trouble with queueing email sending

i'm trying to queue an email sending invoice emails in laravel 5.1, i pass in a variable called invoice, when i dd($invoice->dateString()) in the Job class it's return the correct value but when i pass it in to the view the $invoice variable return empty array (so i get an error about trying to get property from non-object...).
the second problem i have is when i try to add attachment to the job it returns an error : "Serialization of closure failed: Serialization of 'SplFileInfo' is not allowed".
the job class looks like that:
namespace LM2\Jobs;
use Guzzle\Service\Client;
use LM2\Jobs\Job;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldQueue;
use LM2\Models\User as User;
use LM2\Models\Client as LMClient;
class SendInvoiceEmail extends Job implements SelfHandling, ShouldQueue
{
protected $user;
protected $invoice;
protected $attachment;
protected $update;
public function __construct(User $user, LMClient $client, $invoice,$update)
{
$this->user = $user;
$this->client = $client;
$this->invoice = $invoice;
$this->update = $update;
}
public function handle()
{
$attachment = $this->client->invoiceFile($this->invoice->id,['vendor' => 'Test','product' => 'Your Product']);
$invoice = $this->invoice;
$data = [
'invoice' => $this->invoice,
'update'=> $this->update,
];
$user = $this->user;
\Mail::queue('emails.invoices', $data , function($m) use ($user,$invoice,$attachment){
$m->to($user->email)->subject('New payment received')->attach($attachment);
});
}
}
and my controller function looks like that:
public function sendEmailInvoice($update = false){
$client = \Auth::client();
$user = \Auth::user();
$invoices = $client->invoices();
$this->dispatch(new SendInvoiceEmail($user,$client,$invoices[0],$update));
$activity = $data['update'] ? 'updated': 'added';
return ['success', $activity];
}
can someone please tell me what am i doing wrong?
thanks a lot you all :)
Just a guess... but when using Mail::queue() the $data get's converted/cast to an array/you lose your objects inside of the view - hence why you're receiving errors when trying to call methods(), because they don't exist.
Rather than passing invoice + update objects, get what you need from them in the handle method and construct the $data array.
$data = [
'invoice_foo' => $invoice->getFoo(),
'invoice_bar' => $invoice->getBar(),
];
*** Apologies if this doesn't help at all!
so i found the answer thanks to #Michael, i have changed my handle so it's look like this now:
public function handle(Mailer $mailer)
{
$client = $this->client;
$invoice = $this->invoice;
$data = [
'date' => $invoice->dateString(),
'amount' => $invoice->dollars(),
'update'=> $this->update,
];
$user = $this->user;
return $mailer->queue('emails.invoices', $data , function($m) use ($user,$client,$invoice){
$attachment = $client->invoiceFile($invoice->id,['vendor' => 'Infogamy','product' => 'Your Product']);
$m->to($user->email)->subject('New payment received')->attach($attachment);
});
}
The attachment should be processed inside the mailer callback function, and the function called from the $invoice variable (object) should be called inside the handle function and not in the blade view template.

Allow $title argument with HTML code

I work with Laravel 4.
I want to use the HTML::linkAction to provide a link embedded in an image.
The method's prototype is :
linkRoute( string $name, string $title = null, array $parameters = array(), array $attributes = array() )
Then I use the image method to get my <img></img> code :
HTML::linkAction('contact#index', HTML::image('images/contact.png', 'contact'));
It works almost fine. The problem is that the <img></img> code is formatted like htmlspecialchars() do.
Is there a way to allow HTML code in parameters, or to deactivate the htmlspecialchars fonctionality ? Or maybe it's a bad practice ?
Thanks,
Raphaƫl N.
You cannot pass HTML to linkAction(), because it internally calls:
$this->entities()
Here is the code for link(), wich is used by linkAction():
public function link($url, $title = null, $attributes = array(), $secure = null)
{
$url = $this->url->to($url, array(), $secure);
if (is_null($title) or $title === false) $title = $url;
return '<a href="'.$url.'"'.$this->attributes($attributes).'>'.$this->entities($title).'</a>';
}
And there is no options to do it differently.
So if you need to pass an image, you'll have to write HTML for that particular link.

Multiple view strategy in zend 2 [duplicate]

So far, I have figured out how to return a typical JSON response in Zend Framework 2. First, I added the ViewJsonStrategy to the strategies section of the view_manager configuration. Then, instead of returning a ViewModel instance from the controller action, I return a JsonModel instance with all my variables set.
Now that I've figured that piece out, I need to understand how to render a view and return it within that JSON response. In ZF1, I was able to use $this->view->render($scriptName), which returned the HTML as a string. In ZF2, the Zend\View\View::render(...) method returns void.
So... how can I render an HTML view script and return it in a JSON response in one request?
This is what I have right now:
if ($this->getRequest()->isXmlHttpRequest()) {
$jsonModel = new JsonModel(...);
/* #todo Render HTML script into `$html` variable, and add to `JsonModel` */
return $jsonModel;
} else {
return new ViewModel(...);
}
OK, i think i finally understood what you're doing. I've found a solution that i think matches your criteria. Though i am sure that there is room for improvement, as there's some nasty handwork to be done...
public function indexAction()
{
if (!$this->getRequest()->isXmlHttpRequest()) {
return array();
}
$htmlViewPart = new ViewModel();
$htmlViewPart->setTerminal(true)
->setTemplate('module/controller/action')
->setVariables(array(
'key' => 'value'
));
$htmlOutput = $this->getServiceLocator()
->get('viewrenderer')
->render($htmlViewPart);
$jsonModel = new JsonModel();
$jsonModel->setVariables(array(
'html' => $htmlOutput,
'jsonVar1' => 'jsonVal2',
'jsonArray' => array(1,2,3,4,5,6)
));
return $jsonModel;
}
As you can see, the templateMap i create is ... nasty ... it's annoying and i'm sure it can be improved by quite a bit. It's a working solution but just not a clean one. Maybe somehow one would be able to grab the, probably already instantiated, default PhpRenderer from the ServiceLocator with it's template- and path-mapping and then it should be cleaner.
Thanks to the comment ot #DrBeza the work needed to be done could be reduced by a fair amount. Now, as I'd initially wanted, we will grab the viewrenderer with all the template mapping intact and simply render the ViewModel directly. The only important factor is that you need to specify the fully qualified template to render (e.g.: "$module/$controller/$action")
I hope this will get you started though ;)
PS: Response looks like this:
Object:
html: "<h1>Hello World</h1>"
jsonArray: Array[6]
jsonVar1: "jsonVal2"
You can use more easy way to render view for your JSON response.
public function indexAction() {
$partial = $this->getServiceLocator()->get('viewhelpermanager')->get('partial');
$data = array(
'html' => $partial('MyModule/MyPartView.phtml', array("key" => "value")),
'jsonVar1' => 'jsonVal2',
'jsonArray' => array(1, 2, 3, 4, 5, 6));
$isAjax = $this->getRequest()->isXmlHttpRequest());
return isAjax?new JsonModel($data):new ViewModel($data);
}
Please note before use JsonModel class you need to config View Manager in module.config.php file of your module.
'view_manager' => array(
.................
'strategies' => array(
'ViewJsonStrategy',
),
.................
),
it is work for me and hope it help you.
In ZF 3 you can achieve the same result with this code
MyControllerFactory.php
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
{
$renderer = $container->get('ViewRenderer');
return new MyController(
$renderer
);
}
MyController.php
private $renderer;
public function __construct($renderer) {
$this->renderer = $renderer;
}
public function indexAction() {
$htmlViewPart = new ViewModel();
$htmlViewPart
->setTerminal(true)
->setTemplate('module/controller/action')
->setVariables(array('key' => 'value'));
$htmlOutput = $this->renderer->render($htmlViewPart);
$json = \Zend\Json\Json::encode(
array(
'html' => $htmlOutput,
'jsonVar1' => 'jsonVal2',
'jsonArray' => array(1, 2, 3, 4, 5, 6)
)
);
$response = $this->getResponse();
$response->setContent($json);
$response->getHeaders()->addHeaders(array(
'Content-Type' => 'application/json',
));
return $this->response;
}
As usual framework developer mess thing about AJAX following the rule why simple if might be complex Here is simple solution
in controller script
public function checkloginAction()
{
// some hosts need to this some not
//header ("Content-type: application/json"); // this work
// prepare json aray ....
$arr = $array("some" => .....);
echo json_encode($arr); // this works
exit;
}
This works in ZF1 and ZF2 as well
No need of view scrpt at all
If you use advise of ZF2 creator
use Zend\View\Model\JsonModel;
....
$result = new JsonModel($arr);
return $result;
AJAX got null as response at least in zf 2.0.0

symfony - returning JSON from a peer method call in an action

I have some code that checks a parameter and the calls a peer method to get me items from the DB.
What I need to get is these items in JSON.
My peer method is like:
public static function searchFromRequest($word)
{
$c = new Criteria();
$c->addJoin(self::ARTICLE_ID, ArticlePeer::ID);
$c->add(self::TITLE, '%'.$word.'%', Criteria::LIKE);
$c->addAnd(self::ARTICLE_ID, null, Criteria::ISNOTNULL);
$c->addAscendingOrderByColumn(self::TITLE);
return self::doSelect($c);
}
and my action is:
public function executeSearch()
{
$this->word = $this->getRequestParameter('word');
$this->content_type = $this->getRequestParameter('content_type');
if($this->content_type == 'article')
{
$words = ItemPeer::searchFromRequest($this->word);
}
else
{
echo "Nothing here";
}
I can var_dump($words) and I get an array (collection) of items. The problem is, how do I return all of the items in JSON?
I've tried using:
$this->getResponse()->setHttpHeader('Content-type', 'application/json');
$words = ItemPeer::searchFromArticleRequest($this->word);
return $this->renderText(json_encode($words));
But this just returns loads of empty JSON brackets: [{},{},{},{},{},{},{},{},{},{},{},{},{},{}]
Thanks
It seems that json_encode() doesn't like the way Propel Objects are built.
Another solution could be forcing Propel to returnin basic associative objects, using XXXPeer::doSelectStmt()
public static function searchFromRequest($word, $returnPropelObjects = true)
{
$c = new Criteria();
$c->addJoin(self::ARTICLE_ID, ArticlePeer::ID);
$c->add(self::TITLE, '%'.$word.'%', Criteria::LIKE);
$c->addAnd(self::ARTICLE_ID, null, Criteria::ISNOTNULL);
$c->addAscendingOrderByColumn(self::TITLE);
if ($returnPropelObjects)
return self::doSelect($c);
$stmt = self::doSelectStmt($c);
$results = array();
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
$results[] = $row;
}
return $results;
}
Propel 1.6:
object->toJSON();
collection->exportTo('JSON');
The json_encode/json_decode can only encode/decode PHP arrays not objects. The variable
$words
will be an array of Item objects, that's why the output you wrote.
There are basically two solutions. You write your own json encoder that works for objects, like the first comment here:
http://php.net/manual/en/function.json-encode.php
or you write a function that converts your Item objects into PHP arrays like here:
http://www.phpro.org/examples/Convert-Object-To-Array-With-PHP.html
You could also call toArray() on your objects.
$words = ItemPeer::searchFromArticleRequest($this->word);
$wordsArray = array();
foreach ($words as $word)
{
$wordsArray[] = $word->toArray();
}
return $this->renderText(json_encode($wordsArray));
Propel 1.6 will have a toJSON() method for the individual objects or for a whole collection of objects.

return json to ajax in symfony?

In symfony, I call an action and I want this to return json to jQuery frontend.
The Jobeet tutorial teaches how to return a partial but I want to return json, not a partial.
If it's just a normal AJAX action you're returning it from, I think I've used the following somewhere in the past:
return $this->renderText(json_encode($something));
The cheap way:
function executeSomethingThatReturnsJson(){
$M = new Model();
$stuff = $M->getStuff();
echo json_encode($stuff);
die(); //don't do any view stuff
}
The smarter way:
A smarter way is to create a nice subclass of sfActions that helps handling json-stuff.
In a project I did recently, I created a application called 'api' (./symfony generate:application api)
and then created a file like:
api/lib/apiActions.class.php
<?PHP
class apiActions extends sfActions {
public function returnJson($data){
$this->data = $data;
if (sfConfig::get('sf_environment') == 'dev' && !$this->getRequest()->isXmlHttpRequest()){
$this->setLayout('json_debug');
$this->setTemplate('json_debug','main');
}else{
$this->getResponse()->setHttpHeader('Content-type','application/json');
$this->setLayout('json');
$this->setTemplate('json','main');
}
}
}
Notice that I explicitly set the template there.
So my jsonSuccess.php template is simply:
<?PHP echo json_encode($data);
While json_debugSuccess.php makes things prettier:
<?PHP var_dump($data); ?>
Then you can have a controller that extends apiActions (instead of the usual sfActions) that looks like this:
<?php
class myActions extends apiAction {
public function executeList(sfWebRequest $request)
{
$params = array();
if ($request->hasParameter('id')){
$id = $request->getParameter('id');
if (is_numeric($id)){
$params['id'] = $id;
}
}
$data = Doctrine::getTable('SomeTable')->findAll();
$this->returnJson($data);
}
}
Disclaimer: The code above is copy/pasted out of an app I have, but simplified. It's for illustrative purposes only -- but it should get you heading in the right direction.
FYI: In case of Symfony 2.x "quick and dirty" way looks like this:
return new Response(json_encode($data), 200, array('Content-Type', 'text/json'));
Return new JsonResponse(array);

Resources