Archive for programming

Bzr curl connection error when accessing using HTTPS

// June 10th, 2010 // No Comments » // programming

Note to self:

If using bzr to checkout svn repo and it spits out bzr: ERROR: Connection error: curl connection error, use https+urllib instead of only https. Refer http://doc.bazaar.canonical.com/bzr.dev/en/user-guide/http_smart_server.html

Load model dynamically in CakePHP

// April 3rd, 2010 // 2 Comments » // programming

Sometimes I may need a model that’s only going to be used in an action, not the whole controller to lighten the load. Or in that rare occasion when I have to load a model that’s not what the controller meant to work with.

I’ve used this for quite a while now, based on discussion in CakePHP mailing list here.

1. Controller::loadModel(’yourmodel’); (preferred)

// in app/controllers/car_controllers.php
Controller::loadModel('Book');
$this->Book->find('all'); // the object initiated and added as a property to the controller, persistModel is enabled

or
2. ClassRegistry::init(’yourmodel’);

// in app/controllers/car_controllers.php
$book = ClassRegistry::init('Book'); // returns the object
$book->find('all');

Custom filter query in symfony 1.4

// March 21st, 2010 // No Comments » // programming

This cracked my head in this wee hour. Thankfully I got it working.

# add the custom field in myapp/modules/client/config/generator.yml
filter:
  display: [ existing_fields, paid_up_cap_more]
  fields: 
    paid_up_cap_more: { label: Paid up capital (RM) - more than }
// in lib/filter/doctrine
class CrmClientFormFilter extends BaseCrmClientFormFilter
{
  public function configure() {
    $this->widgetSchema['paid_up_cap_more'] =  new sfWidgetFormInput();
    $this->validatorSchema['paid_up_cap_more'] = new  sfValidatorPass(array ('required' => false));
  }
 
  public function getFields() {
    $fields = parent::getFields();
    $fields['paid_up_cap_more'] = 'paid_up_cap_more';
    return $fields;
  }
 
  // must be in this format addXXXXXColumnQuery
  protected function addPaidUpCapMoreColumnQuery($q, $element, $value) {
    if (!empty($value)){
         $q->andWhere('paid_up_capital >= ?', $value);
         return $q;
    }
  }
}

virtualenv

// March 2nd, 2010 // 1 Comment » // open source, programming

I kept getting back to virtualenv and every time I would totally forget how to go about it and had to start googling. Every time. Frustrating. This is usually when Plone is involved.

So.

$ sudo apt-get install python-virtualenv
$ virtualenv -p /usr/bin/python2.4 /home/cawanpink/python2.4
$ source /home/cawanpink/python2.4/bin/activate
(python2.4)$ deactivate

That’s

  • install
  • create the Python 2.4 virtual environment
  • activate the environment
  • to get out of the environment

CakePHP oh CakePHP

// December 8th, 2009 // 1 Comment » // programming

I’ve seen alot of people with PHP experience of 2-3 years, when being presented with CakePHP, they say something along the line of “Oh this is hard”, “Why is it so different?”, “What’s this arrow and arrays inside array thingie?”, etc.

Lots of young programmers working with PHP that I’ve seen in Malaysia hardly understand the OO (object oriented) concept. When you look at their codes, it’s alot of functions and alot of includes. Classes? Nowhere to be seen. It’s probably the most hardest concept to grab while they were in college when actually it’s not that hard at all. If you don’t understand OO then it’s likely you don’t know why it’s good. And of course the MVC (Model-View-Controller) concept in many programming language frameworks you will find it very very hard to digest. Those frameworks are for RAD (Rapid Application Development) – just look at the name, don’t you think it will have something to do with your development speed? It’s like you have a bicycle and a car. You know you can get there faster by car, you only have to learn how to use it. It may takes time but for the next 20 years you’re going to need the car so you can get anywhere faster. So would you or would you not learn how to use the car?? If you still picks bicycle, that’s not a wrong answer. It only shows your preference.

Having MVC, for example, gives you the advantage of having multiple people working on the same page at the same time. A can work on the controller part, B can work on its layout and C can totally change the database engine from MySQL to Postgresql (for example) without having A or B amending their codes at all after that. Also I believe, it’s easier to take over an MVC-ed application than a non-MVC ones. I can’t imagine having to wade through all those includes/requires/imports. All I need to know is where the models, controllers and views are.

Want to try CakePHP? Just one thing you MUST have – the discipline to put your codes to where it belongs, follow the standards. Don’t make your queries in the layouts – it just doesn’t make sense. It’s likely that you’re going to forget about it in a week or two, you’re going to curse your way through it when you can’t find it later for maintenance work (not to mention the other new programmer working on the same app). Just plan for the future. The time that it takes you to do it properly now will save you a heck of a time later. That includes proper comments in your codes.

The learning curve is a bit steep – yes it is if you don’t understand OO. So get to know your OO stuff. And then go for CakePHP, or any framework you fancy. You’ll be surprised how much you’ll like it.