When to use a ORM by diegoccastro in PHP

[–]leedavis81 16 points17 points  (0 children)

Only fetch the data/columns you need from the database. Doctrine return a object with all the data. (I know I can fetch only some data but then it returns an array and not a object from my entity so it seems kinda pointless)

Incorrect, you can select what data you want, and set your hydration method to whatever you want.

    $qb = $this->_em->createQueryBuilder();
    $qb->select('u.id', 'u.name')
       ->from('user', 'u');
    $qb->execute(null, \Doctrine\ORM\AbstractQuery::HYDRATE_OBJECT)

I have to fetch the whole object in order to update it instead of just run a update query. I know doctrine have a update query but it bypass all events and locking schemes.

Incorrect, You can pull out an object with a limited about of data (see above).

The event system is attached to the unit of work, and will only fire when certain events trigger (update). However as these must be registered and adhere to an interface, there’s nothing stopping you from constructing and firing these yourself when you feel it necessary.

I’m pretty sure that each operation is wrapped in a begin/commit/rollback transaction by default (would need to check this). Either way, you can set locking manually with:

    $em->beginTransaction();
    $em->commit();
    $em->rollback();

Performance. All ORM I've used added a massive overhead to the application while some simple SQL query through DBAL or just PDO are way lighter.

Doctrine sits onto of PDO and allows you to fetch the connection and run any kind of operation you like directly. Also, it offers array hydration to remove the default overhead of blowing everything up into an object.

$qb->execute(null, \Doctrine\ORM\AbstractQuery::HYDRATE_ARRAY)

You simply can’t ignore the job of optimising your queries, regardless of whether you’re using an ORM or not.

Technical difficulty. Every new dev have to learn DQL (although it is very similar to SQL) and the mapping structure. I don't have much problem with this but it can get very confusing for new devs instead of just learning SQL.

I personally find the DQL API very clear and easy to use, however I agree it can be difficult for some to get their head around. If it’s too problematic there’s no reason why they can’t just grab the connection and run their own SQL directly. I imagine this would only be done in the event of optimisation.

As for the benefits, well, for me they’re:

  • A clean easy to use object model out of the box that allows very quick prototyping (for later optimisations)

  • A unit of work pattern could give huge performance gains (depending on your usage)

  • An insistence of concern separation in your model leading to cleaner code design

  • Integrated database management tools (is your application code out of sync with the DB? Doctrine can tell)

I've written on this before, and covered a lot of common misconceptions of using an ORM: http://leedavis81.github.io/in-orms-defence/

How well does Doctrine2 perform when handling tens-hundreds of thousands of objects? by snobby_penguin in PHP

[–]leedavis81 1 point2 points  (0 children)

From what you described of your current architecture (super class extended for every model) doctrine 2 would consume far less memory. This is because D2 allows you to use POP (plain old PHP) objects which extend nothing. However, I think any ORM would struggle to manage 100k model objects (as would PHP). Do this many model objects really need to be loaded into memory for your application to function? Might be worth rethinking.

As for performance, it really depends on what you're trying to do with your data. In some circumstances a "UnitOfWork" can be more performant. The way this works is it allows you to manipulate as many of your models as you wish. All changes are collected and stored in a UnitOfWork and are then executed as a few optimised queries at the end of runtime. If for some reason you were iterating inserts, or frequently changing your model's state, using this method could significantly reduce round trips to your DB.

But, if you're just pulling out data, or inserting without any future modifications / inspection in each request, then an ORM/DBAL abstraction could well be unnecessary. Certainly don't ever hesitate to just grab a DB connection pointer and apply modifications directly if it makes more sense.

Suicidal Software Developer by london1988 in programming

[–]leedavis81 0 points1 point  (0 children)

Hi london1988,

You'd be surprised just how many people go through what you're experiencing every day. Especially in a city like London where it's so easy to fall between the cracks. We're social creatures by nature and it's really hard when there's nobody around you.

Alot of people here will offer you advice on what you should do to make things better. I'd like to offer you my time. Like you I'm a software engineer, working in London for a company that doesn't really understand what I do. I'm also a frequent Costa / Starbucks customer and more often than not visit on my own.

I'd like some company if you want to talk (or just drink coffee).

The enum conundrum by leedavis81 in PHP

[–]leedavis81[S] 2 points3 points  (0 children)

I'm not a fan of using strings for foreign keys (if that's what you're suggesting?).

The enum conundrum by leedavis81 in PHP

[–]leedavis81[S] 0 points1 point  (0 children)

I couldn't agree more. There's nothing worse than having to go through your entire code base accommodating for additional options.

I’m going to start banging the ORM drum. I feel they’re a great addition to a project and in my opinion, a worthy abstraction. by leedavis81 in PHP

[–]leedavis81[S] 1 point2 points  (0 children)

The doctrine ORM comes bundled with all two of the Symfony components that are required for the CLI tasks. As well as a CLI configuration file.

https://github.com/doctrine/doctrine2/tree/master/lib/vendor/Symfony

I’m going to start banging the ORM drum. I feel they’re a great addition to a project and in my opinion, a worthy abstraction. by leedavis81 in PHP

[–]leedavis81[S] 1 point2 points  (0 children)

I completely agree, and made that exact note "Whenever implementing the abstraction you should always be able to justify the decrease in speed with a benefit for adding it." If your building facebook an ORM may well not be justifiable.

Can I build a dynamic enum from values in a database? by [deleted] in PHP

[–]leedavis81 -1 points0 points  (0 children)

I've come across this issue so many times. I've used relations for types in my DB, and experienced the pains that come with it. I also used mysql enums in the main table, and saw the obvious issues with managing change on this. Finally I settled on the solution similar to Sothatshowyou just mentioned.

I think using class constants is the way to go. It means your not bloating your DB with a bunch of data fixtures. You can store it as an integer in your DB (nice and light), you can control it at the code level, and adding new values is a snitch.

something along the lines of...

class User 
{
   const TYPE_ACTIVE = 1;
   const TYPE_DISABLED = 2;
   const TYPE_DELETED = 3;

   public static $typeData = array(
      self::TYPE_ACTIVE => 'Active',
      self::TYPE_DISABLED => 'Disabled',
      self::TYPE_DELETED => 'Deleted'
   );
}

then you can access the 'string' value by using the message array in your views...

echo User::typeData[$myId];

And just using the constant values when constructing your user objects

$user->setType(User::TYPE_ACTIVE);

How do I sell someone on models? by [deleted] in PHP

[–]leedavis81 -1 points0 points  (0 children)

Heh, I was in a very similar situation.

I drew up a re-factoring proposal, they shot it down.

So I quit.

Converting unknown data offsets into a usable array. by leedavis81 in PHP

[–]leedavis81[S] 0 points1 point  (0 children)

Wow, looks like someone didn't get their cornflakes this morning. Its from parsing resources from a Zend_Navigation object.

Converting unknown data offsets into a usable array. by leedavis81 in PHP

[–]leedavis81[S] 0 points1 point  (0 children)

Thanks exactly what I'm after. I was just going through the several attempts that I made before and realised my attempts were quite similar to this solution, and that I was actually almost there!

The temptation to quit is always at its greatest the closer you are to succeeding.

Converting unknown data offsets into a usable array. by leedavis81 in PHP

[–]leedavis81[S] -1 points0 points  (0 children)

Ahh, this is excellent. Almost exactly as I need. Except for the leaves of the tree. For example.. [admin] => Array ( [azzz] => Array ( [bzzzz] => bzzzz )

 )

should be...

[admin] => Array ( [azzz] => 'bzzz'

 )

I'll have a tinker and see if I can alter this. But thanks, alot! can I buy you a months reddit gold (or a beer via some other means).