-5

In a controller's action I load a PDO object from the database and send it to the twig template in order to render a data grid:

/**
 * @Route("/action1", name="action1")
 * @Template()
 */
public function action1Action(Request $request)
{
    $mydata = $this->daoClass->getData();
    return array(
        'mydata' => $mydata
    );
}

$myData is loaded using Doctrine which returns me a PDO object with an (small) array of results (PDO Object). I render this like this:

<table>
    {% for data in mydata %}
        <tr>
            <td>{{ data.foo }}</td>
            <td>{{ data.bar }}</td>
        </tr>
    {% endfor %}
</table>

So in the twig template, below the table with the data, I got this form that sends data to the next action:

<form action="{{ path('action2') }}" method="post">
    <!-- some javascript populated fields -->
    <input type="hidden" id="field1" name="field1">
   ...
</form>

How can I make it send also the PDO object that rendered the grid (mydata object) ?

I tried to insert it in the session in the first action so I can retrieve it in the next action but got the message: "You cannot serialize or unserialize PDO instances"

Is there any better way to do this ?

I'm not using symfony forms in this module because this is an overly complex screen with lots of validations and calculated fields and it would be much more complicated to use the forms.

Nelson Teixeira
  • 6,297
  • 5
  • 36
  • 73
  • 1
    Why not just select it once again from the database? – zerkms Jan 29 '15 at 20:07
  • When you say 'PDO object', do you mean an entity or a result set of entities? – DevDonkey Jan 29 '15 at 20:09
  • isn't the opposite more meaningful ? why select the same data twice ? – Nelson Teixeira Jan 29 '15 at 20:09
  • I mean a result set, but it's not a large set. It's small enough for a web request. – Nelson Teixeira Jan 29 '15 at 20:10
  • "isn't the opposite more meaningful ? why select the same data twice ?" --- isn't it more meaningful to not store the same data twice and use database for what it was designed for: to store and serve data. – zerkms Jan 29 '15 at 20:12
  • store the same data ? this result set isn't changed between actions. – Nelson Teixeira Jan 29 '15 at 20:13
  • That's right - it's not changed and it's available in database. Why do you want to persist it somewhere else? It already **is available** in database - select it from there. – zerkms Jan 29 '15 at 20:14
  • Persist it ? I guess I wasn't clear. This result set isn't changed between actions. In my view database reads are more costly that memory and network operations. Don't you think ? – Nelson Teixeira Jan 29 '15 at 20:15
  • Do you pay for every `SELECT` from database? It's not clear **WHY** you want to do that. What is the **objective technical reason** for that? – zerkms Jan 29 '15 at 20:16
  • performance cost.... I thought you had understand it – Nelson Teixeira Jan 29 '15 at 20:17
  • 1
    Performance? Do you have any *real* performance issues? If so - what are those issues? Where are any numbers? If it's a question about a performance issue, why there is no even a single word about it in the question text? – zerkms Jan 29 '15 at 20:17
  • Not yet, but if I keep adding lots of redundant calls to the database loading the same data, over and over again, for sure will have in the near future. – Nelson Teixeira Jan 29 '15 at 20:19
  • 1
    So you're solving the issue you don't have yet and you think that this solution will magically match the load pattern that you potentially might have? A thing for you to think: even facebook and twitter engineers cannot predict all performance bottlenecks. So they just evolve their architecture as per new load requirements. Do you seriously think you can do better than they? – zerkms Jan 29 '15 at 20:20
  • Well, ok... your view. I made the question because, to me, It's better to send the object than reloading the data from the database and like this I learn a bit more of Symfony. If you don't like this way of doing things or have other tecnical viewpoint. OK. Absolutely no hardfellings :) Hope someone else can help with the question. – Nelson Teixeira Jan 29 '15 at 20:25
  • Well, I tried to tell you that you're wasting your time doing something that makes no sense. But seems like you're fine with that. Good luck. – zerkms Jan 29 '15 at 20:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69896/discussion-between-nelson-teixeira-and-zerkms). – Nelson Teixeira Jan 30 '15 at 14:29

2 Answers2

3

Is there any better way to do this ?

Yes with symfony forms.

I'm not using symfony forms in this module because this is an overly complex screen with lots of validations and calculated fields and it would be much more complicated to use the forms.

As complex as you want. Using symfony forms for mapping, the view and with the symfony validation are very very powerful tools. Writing your own forms and mapping and validations is unnecessary.

What's the best way of sending a PDO between actions in Symfony2?

The answer is definitely not to save Doctrine entities to a session or Memcache and the reason is:

// copied from the comment

Think in scopes. PHP starts, does something, terminates. You can only hold data whose scope is not limited. For example a simple string has no scope. You can only store such type of data, but no smart PDO objects. For example doctrine entities are actually only some attributes with getters and setters, but doctrine is doing some magic to implement a proxy-pattern for them. Thus they extend some accessing logic saying if any setter or getter is invoked, load the object from the database first. If you would store them and reload them, they would have dead references to the objects.

Aitch
  • 1,617
  • 13
  • 24
  • Actually I got some bad experiences using forms for more complex tasks. What I feel is a bit of lack of control. For example: not allways the javascript messages appear, sometimes the form is submitted and not validated, among other errors. The code is large and overly complex to ask for help in the forums and I'm the only person programming with symfony in my company's local office. So... get I and Symfony Forms are out of luck at this time. :) – Nelson Teixeira Feb 07 '15 at 02:27
  • @NelsonTeixeira Don't use Symfony for production if you don't know how to do the complex tasks yet. It's not possible to do what you want and for good reason. – Tek Feb 07 '15 at 03:29
  • and the reason is... ? – Nelson Teixeira Feb 07 '15 at 03:30
  • @NelsonTeixeira Symfony2 doesn't let you save PDO to a session. If they don't allow it, it most likely means you're doing something wrong. – Tek Feb 07 '15 at 03:38
  • ok.. but you know the reason they don't allow it ? because not necessarly I have to agree with them... – Nelson Teixeira Feb 07 '15 at 03:41
  • When you send a request to any PHP Application, there is always a complete restart of the application. Mostly the database adapter is implemented as singleton, so if you don't need the db, there will be no connection. so for example with doctrine you will always have your connector and there is no need to persist it, respectively in the MVC domain, do not persist any V oder C data. – Aitch Feb 07 '15 at 22:17
  • @Aitch sorry I didn't understand what you meant. Can you please reformulate ? I understood only the first phrase. :) – Nelson Teixeira Feb 09 '15 at 18:36
  • 1
    Think in scopes. PHP starts, does something, terminates. You can only hold data whose scope is not limited. For example a simple string has no scope. You can only store such type of data, but no smart PDO objects. For example doctrine entities are actutally only some attributes with getters and setters, but doctrine is doing some magic to implement a proxy-pattern for them. Thus they extend some accessing logic saying if any setter or getter is invoked, load the object from the database first. If you would store them and reload them, they would have dead references to the objects. – Aitch Feb 10 '15 at 21:29
  • Now I understand, and that's a really good reason. That's the way to explain something, describing the real reason not to do something instead of blindly say not to do it. Don't you want to put this in form of an answer in order for you to receive the bounty ? :) – Nelson Teixeira Feb 11 '15 at 15:37
  • @NelsonTeixeira I've appended my comment to the answer. Hope it's okay that way, and thank you. In my opinion, in the PHP world you're mostly doing it in a cleaner way if you're not thinking of any PHP optimizations. The way PHP is processing the requests is sometimes a bit doubtful considering performance. For example Java can work with DB-ConnectionPools to have a connection already setup when running the first query to a database. PHP performance depends very much on file cache and op-cache to speed up. Those are completely different worlds with different advantages. – Aitch Feb 11 '15 at 22:27
  • Oh... sorry I didn't see that it was the same people answering and commenting. You have won the bounty. :) – Nelson Teixeira Feb 12 '15 at 03:18
0

First of all, don't do what you're trying to do for performance reasons.

What you're trying to do is called premature optimization, and it is not good.

Someone here tried to do a very similar thing to what you're doing and they are having a hard time.

However, if you ever want to store information the user temporarily to another page use Symfony Sessions

In case you skip what I said, let me repeat:

Warning: This code is NOT recommended for PDO results

use Symfony\Component\HttpFoundation\Session\Session;

$session = new Session();
$session->start();

// set and get session attributes
//Controller ExampleMethodAction1()
$Person = new Person();
$Person->setName('John');
$session->set('PersonObject', $Person);


//Then in another controller method
//Controller ExampleMethodAction2()
$Person = $session->get('PersonObject');
$Person->getName(); //returns John

Another thing about performance is, it's cheaper to buy more hardware than hire a new developer to optimize the code.

Community
  • 1
  • 1
Tek
  • 2,888
  • 5
  • 45
  • 73
  • Yes... that's what I tried to do, but Symfony won't allow that for a PDO object. That's why I asked. If you want to know more about my views on doing it this way, please take a look a my discussion with @zerkms. :) – Nelson Teixeira Feb 06 '15 at 22:45
  • @NelsonTeixeira Exactly, there's a reason Symfony2 won't allow it. So you know you're doing something wrong when Symfony2 is telling you not to do it. Just call the database again. – Tek Feb 06 '15 at 22:47
  • @NelsonTeixeira If you really MUST and you ignore all these warnings. Make a class such as `new Person` and populate it with the data from the database. Then pass `Person` to another controller method using sessions like my example above. – Tek Feb 06 '15 at 22:48
  • What I want is to send the PDO object without treating it, exactly because I want to simplify the code NOT doing another database call. If I have to make a loop recreating the array of objects it will be pointless. – Nelson Teixeira Feb 07 '15 at 01:47
  • @NelsonTeixeira That's the only way to get what you want. Either way you shouldn't be using PDO directly, instead you should map your `Entity` objects with another class or use `Doctrine`. So that you only have to map it once and have the mapping class be reusable while also making your objects reusable. There's a reason `Entities` exist in Symfony2, if you're not going to use the tools you shouldn't use Symfony. You should be using [Symfony Components](http://symfony.com/components) with vanilla PHP. – Tek Feb 07 '15 at 03:33
  • But the entities are already inside the PDO object. Of course I'm already using them... I told you that I load them from the database using Doctrine. – Nelson Teixeira Feb 07 '15 at 03:39
  • about the links you put on the answer, have you read the next answer near to the one you pointed out ? This one: http://bit.ly/18Wk1jX and have you read this ? http://bit.ly/16sHKGw it's in the question's comments. – Nelson Teixeira Feb 07 '15 at 03:47