Posted on 2009-09-11
So, now I have some new classes that use doctrine to acces my DB and the move went quite smoothly.
However, I had some methods that – of course – used propel criteria to create special queries… those times are gone. What’s it like in doctrine?
But first, I had to add a Behaviour Timestampable to my schema to make sure good old updated_at and created_at still work. They have to be removed from the schema.yml, though and be replaced by
Zip:
actAs: [Timestampable]
Hope, that works…
Regarding my criteria: For example, I had the quite simple case:
$c=new Criteria();
$c->setLimit(20);
$this->zip_list = ZipPeer::doSelect($c);
Now, this obviouls needs to become a DQL something.
$q = Doctrine::getTable(‘Zip’)->createQuery(‘z’)
->limit(20);
$this->zip_list = $q->fetchArray();
Honestly, I have no clue right now what the parameter of the create is supposed to do – the documentation does not really explain it… and the fetchArray() returns an array – and all my getMethods fail… *hmpf* I thought there was object hydration?
Aaaha!
$user = new User(); $user->username = 'jwage'; // Object $user['username'] = 'jwage'; // Array $user->set('username', 'jwage'); // Function $user->setUsername('jwage'); // Propel access $user->save();
But why does “getId() fail then?
Maan, it took quite some reading through the docs to find out that the doctrine examples like the array representation and always use fetchArray, while for retrieveing the results as hydrated objects, you need to simply ->execute()
so:
$q = Doctrine::getTable(‘Zip’)->createQuery(‘z’)
->limit(20);
$this->zip_list = $q->execute();
Now, it looks quite good…
As far as standard methods go, I had to replace
$zip = ZipPeer::retrieveByPk($request->getParameter(‘id’))
by
$zip = Doctrine::getTable(‘Zip’)->findOneById($request->getParameter(‘id’)
using magic finders, which seems to be quite easy – but I remember John Wage say, that magic finders should not be used in final production environments… well, for the time being I’ll keep it…